欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android高德获取当前定位,点击地图获取位置信息

程序员文章站 2022-04-03 21:29:16
效果图(GIF压缩问题请忽略) 1.Activity代码(Android6.0以上别忘记添加动态权限) /** * Created by YyyyQ on 2020/3/26 * 获取当前定位,地图选点,获取当前和选择的位置信息 */ public class LocationActivity ex ......

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>