zoukankan      html  css  js  c++  java
  • 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>

     

  • 相关阅读:
    查找->静态查找表->顺序查找(顺序表)
    查找(总结篇)
    树和二叉树->其他(待完善)
    树和二叉树->最优二叉树
    树和二叉树->线索二叉树
    树和二叉树->遍历
    树和二叉树->相互转化
    树和二叉树->存储结构
    树和二叉树->基础知识
    P2921-[USACO08DEC]在农场万圣节Trick or Treat on the Farm
  • 原文地址:https://www.cnblogs.com/YyyyQ/p/12573001.html
Copyright © 2011-2022 走看看