Android高德獲取當前定位,點擊地圖獲取位置信息


效果圖(GIF壓縮問題請忽略)

 

1.Activity代碼(Android6.0以上別忘記添加動態權限)

 

/**
 * Created by YyyyQ on 2020/3/26
 * 獲取當前定位,地圖選點,獲取當前和選擇的位置信息
 */
public class LocationActivity extends AppCompatActivity implements LocationSource, AMapLocationListener, GeocodeSearch.OnGeocodeSearchListener,
        AMap.OnMapClickListener {

    private MapView mapView;
    private AMap aMap;
    private UiSettings uiSettings;
    //定位服務
    private LocationSource.OnLocationChangedListener onLocationChangedListener;
    private AMapLocationClient locationClient;
    private AMapLocationClientOption locationClientOption;
    //地理編碼
    private GeocodeSearch geocodeSearch;
    //回顯位置信息的TextView
    private TextView locationCoordinate;
    private TextView locationInfo;
    //當前地圖上的marker
    private Marker marker;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_location);
        mapView = findViewById(R.id.location_map);
        mapView.onCreate(savedInstanceState);
        locationCoordinate = findViewById(R.id.location_coordinate);
        locationInfo = findViewById(R.id.location_info);
        if (aMap == null) {
            aMap = mapView.getMap();
            uiSettings = aMap.getUiSettings();
            //設置地圖屬性
            setMapAttribute();
        }
    }

    private void setMapAttribute() {
        //設置默認縮放級別
        aMap.animateCamera(CameraUpdateFactory.zoomTo(15));
        //隱藏的右下角縮放按鈕
        uiSettings.setZoomControlsEnabled(false);
        //顯示右上角定位按鈕
        uiSettings.setMyLocationButtonEnabled(false);
        //設置定位監聽
        aMap.setLocationSource(this);
        //可觸發定位並顯示當前位置
        aMap.setMyLocationEnabled(true);
        //定位一次,且將視角移動到地圖中心點
        MyLocationStyle myLocationStyle = new MyLocationStyle();
        myLocationStyle.myLocationType(MyLocationStyle.LOCATION_TYPE_LOCATE);
        //隱藏定位點外圈圓的顏色
        myLocationStyle.strokeColor(Color.argb(0, 0, 0, 0));
        myLocationStyle.radiusFillColor(Color.argb(0, 0, 0, 0));
        aMap.setMyLocationStyle(myLocationStyle);
        //設置地理編碼查詢
        geocodeSearch = new GeocodeSearch(this);
        geocodeSearch.setOnGeocodeSearchListener(this);
        //設置地圖點擊事件
        aMap.setOnMapClickListener(this);
    }

    /**
     * 激活定位
     */
    @Override
    public void activate(OnLocationChangedListener onLocationChangedListener) {
        this.onLocationChangedListener = onLocationChangedListener;
        if (locationClient == null) {
            //初始化定位
            locationClient = new AMapLocationClient(this);
            //初始化定位參數
            locationClientOption = new AMapLocationClientOption();
            //設置定位回調監聽
            locationClient.setLocationListener(this);
            //高精度定位模式
            locationClientOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
            //單定位模式
            locationClientOption.setOnceLocation(true);
            //設置定位參數
            locationClient.setLocationOption(locationClientOption);
            //啟動定位
            locationClient.startLocation();
        }
    }

    /**
     * 定位成功后回調函數
     */
    @Override
    public void onLocationChanged(AMapLocation aMapLocation) {
        if (onLocationChangedListener != null && aMapLocation != null) {
            if (aMapLocation.getErrorCode() == 0) {
                //顯示定位圓點
                onLocationChangedListener.onLocationChanged(aMapLocation);
                locationCoordinate.setText("當前緯度:" + aMapLocation.getLatitude() + "當前經度" + aMapLocation.getLongitude());
                //根據當前經緯度查詢地址
                LatLonPoint latLonPoint = new LatLonPoint(aMapLocation.getLatitude(), aMapLocation.getLongitude());
                RegeocodeQuery query = new RegeocodeQuery(latLonPoint, 200, GeocodeSearch.AMAP);
                geocodeSearch.getFromLocationAsyn(query);
            } else {
                Log.e("YyyyQ", "定位失敗" + aMapLocation.getErrorCode() + ":" + aMapLocation.getErrorInfo());
                Toast.makeText(getApplication(), "定位失敗", Toast.LENGTH_SHORT).show();
            }
        }
    }

    /**
     * 停止定位
     */
    @Override
    public void deactivate() {
        onLocationChangedListener = null;
        if (locationClient != null) {
            locationClient.stopLocation();
            locationClient.onDestroy();
        }
    }

    /**
     * 根據坐標轉換地址信息
     */
    @Override
    public void onRegeocodeSearched(RegeocodeResult regeocodeResult, int i) {
        if (i == AMapException.CODE_AMAP_SUCCESS) {
            locationInfo.setText("當前位置信息:" + regeocodeResult.getRegeocodeAddress().getFormatAddress());
        } else {
            Toast.makeText(getApplication(), "獲取當前位置信息失敗", Toast.LENGTH_SHORT).show();
        }
    }

    /**
     * 地址轉坐標
     */
    @Override
    public void onGeocodeSearched(GeocodeResult geocodeResult, int i) {

    }

    /**
     * 地圖點擊事件
     */
    @Override
    public void onMapClick(LatLng latLng) {
        if (marker != null) {
            marker.remove();
        }
        MarkerOptions markerOptions = new MarkerOptions();
        markerOptions.icon(BitmapDescriptorFactory.fromResource(R.drawable.map_icon));
        markerOptions.position(latLng);
        marker = aMap.addMarker(markerOptions);
        //根據點擊地圖的點位獲取詳細信息
        locationCoordinate.setText("當前緯度:" + latLng.latitude + "當前經度" + latLng.longitude);
        //根據當前經緯度查詢地址
        LatLonPoint latLonPoint = new LatLonPoint(latLng.latitude, latLng.longitude);
        RegeocodeQuery query = new RegeocodeQuery(latLonPoint, 200, GeocodeSearch.AMAP);
        geocodeSearch.getFromLocationAsyn(query);
    }


}

 

 

2.xml布局文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <!--地圖o-->
    <com.amap.api.maps.MapView
        android:id="@+id/location_map"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <!--回顯位置信息的布局-->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="140dp"
        android:layout_alignParentBottom="true"
        android:layout_margin="15dp"
        android:alpha="0.85"
        android:background="@drawable/layout_background"
        android:orientation="vertical">

        <TextView
            android:id="@+id/location_coordinate"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1.4"
            android:gravity="center|left"
            android:textColor="#1A91B0"
            android:textSize="16sp" />

        <TextView
            android:id="@+id/location_info"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="2"
            android:gravity="center|left|top"
            android:paddingTop="10dp"
            android:textColor="#F05554"
            android:textSize="16sp" />
    </LinearLayout>


</RelativeLayout>

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM