Android :高德地图demo

在这里插入图片描述
高德的账号去改的开房平台进行注册,注册成功后进入个人中心;
在这里插入图片描述
进入个人中心后点击应供管理
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
//这里可以随便写
在这里插入图片描述
在这里插入图片描述

在这里插入图片描述
获取SHA1码
在这里插入图片描述

在这里插入图片描述

需要的权限有

    <!-- Normal Permissions 不需要运行时注册 -->
    <!-- 获取运营商信息,用于支持提供运营商信息相关的接口 -->
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <!-- 用于访问wifi网络信息,wifi信息会用于进行网络定位 -->
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <!-- 这个权限用于获取wifi的获取权限,wifi信息会用来进行网络定位 -->
    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
    <uses-permission
        android:name="android.permission.CHANGE_CONFIGURATION"
        tools:ignore="ProtectedPermissions" />

    <!-- 请求网络 -->
    <uses-permission android:name="android.permission.INTERNET" />

    <!-- 不是SDK需要的权限,是示例中的后台唤醒定位需要的权限 -->
    <uses-permission android:name="android.permission.WAKE_LOCK" />

    <!-- 需要运行时注册的权限 -->
    <!-- 用于进行网络定位 -->
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <!-- 用于访问GPS定位 -->
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <!-- 用于提高GPS定位速度 -->
    <uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
    <!-- 写入扩展存储,向扩展卡写入数据,用于写入缓存定位数据 -->
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <!-- 读取缓存数据 -->
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

    <!-- 用于读取手机当前的状态 -->
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />

    <!-- 更改设置 -->
    <uses-permission
        android:name="android.permission.WRITE_SETTINGS"
        tools:ignore="ProtectedPermissions" />

    <!-- 3.2.0版本增加 -->
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
    <!-- 3.2.0版本增加 -->
    <uses-permission android:name="android.permission.BLUETOOTH" />

清单文件中

 <meta-data
            android:name="com.amap.api.v2.apikey"
            android:value="你的key值"></meta-data>
        <!-- 定位需要的服务 -->
        <service android:name="com.amap.api.location.APSService"></service>

在这里插入图片描述

写入一个工具类

public class LocationUtils implements AMapLocationListener {
    private AMapLocationClient aMapLocationClient;
    private AMapLocationClientOption clientOption;
    private ILocationCallBack callBack;

    public void startLocate(Context context) {
        aMapLocationClient = new AMapLocationClient(context);

        //设置监听回调
        aMapLocationClient.setLocationListener(this);

        //初始化定位参数
        clientOption = new AMapLocationClientOption();
        clientOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Battery_Saving);
        clientOption.setNeedAddress(true);
        clientOption.setOnceLocation(false);
        //设置是否强制刷新WIFI,默认为强制刷新
        clientOption.setWifiActiveScan(true);
        //设置是否允许模拟位置,默认为false,不允许模拟位置
        clientOption.setMockEnable(false);
        //设置定位间隔
        clientOption.setInterval(2000);
        aMapLocationClient.setLocationOption(clientOption);

        aMapLocationClient.startLocation();
    }

    @Override
    public void onLocationChanged(AMapLocation aMapLocation) {
        if (aMapLocation != null) {
            if (aMapLocation.getErrorCode() == 0) {
                //定位成功完成回调
                String country = aMapLocation.getCountry();
                String province = aMapLocation.getProvince();
                String city = aMapLocation.getCity();
                String district = aMapLocation.getDistrict();
                String street = aMapLocation.getStreet();
                double lat = aMapLocation.getLatitude();
                double lgt = aMapLocation.getLongitude();

                callBack.callBack(country + province + city + district + street, lat, lgt, aMapLocation);
            } else {
                //显示错误信息ErrCode是错误码,errInfo是错误信息,详见错误码表。
                Log.e("AmapError", "location Error, ErrCode:"
                        + aMapLocation.getErrorCode() + ", errInfo:"
                        + aMapLocation.getErrorInfo());
            }
        }
    }

    /**
     * 自定义图标
     *
     * @return
     */
    public MarkerOptions getMarkerOption(String str, double lat, double lgt) {
        MarkerOptions markerOptions = new MarkerOptions();
        markerOptions.icon(BitmapDescriptorFactory.fromResource(R.mipmap.ic_launcher));
        markerOptions.position(new LatLng(lat, lgt));
        markerOptions.title(str);
        markerOptions.snippet("纬度:" + lat + "   经度:" + lgt);
        markerOptions.period(100);

        return markerOptions;
    }

    public interface ILocationCallBack {
        void callBack(String str, double lat, double lgt, AMapLocation aMapLocation);
    }

    public void setLocationCallBack(ILocationCallBack callBack) {
        this.callBack = callBack;
    }
}

然后就是高德的代码

public class MainActivity extends AppCompatActivity implements LocationSource {

    private MapView myMapView;
    private AMap aMap;
    private LocationSource.OnLocationChangedListener mListener = null;//定位监听器
    private LocationUtils locationUtils;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        myMapView = findViewById(R.id.MapView);
        myMapView.onCreate(savedInstanceState);

        initView();
    }

    private void initView() {
        if(aMap == null){
            aMap = myMapView.getMap();
        }

        setLocationCallBack();

        //设置定位监听
        aMap.setLocationSource(this);
        //设置缩放级别
        aMap.moveCamera(CameraUpdateFactory.zoomTo(15));
        //显示定位层并可触发,默认false
        aMap.setMyLocationEnabled(true);
    }

    private void setLocationCallBack(){
        locationUtils = new LocationUtils();
        locationUtils.setLocationCallBack(new LocationUtils.ILocationCallBack() {
            @Override
            public void callBack(String str,double lat,double lgt,AMapLocation aMapLocation) {

                //根据获取的经纬度,将地图移动到定位位置
                aMap.moveCamera(CameraUpdateFactory.changeLatLng(new LatLng(lat,lgt)));
                mListener.onLocationChanged(aMapLocation);
                //添加定位图标
                aMap.addMarker(locationUtils.getMarkerOption(str,lat,lgt));
            }
        });
    }

    @Override
    public void activate(OnLocationChangedListener onLocationChangedListener) {
        mListener = onLocationChangedListener;

        locationUtils.startLocate(getApplicationContext());
    }

    @Override
    public void deactivate() {
        mListener = null;
    }

    @Override
    protected void onPause() {
        super.onPause();
        //暂停地图的绘制
        myMapView.onPause();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //销毁地图
        myMapView.onDestroy();
    }

    @Override
    protected void onResume() {
        super.onResume();
        //重新绘制加载地图
        myMapView.onResume();
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        myMapView.onSaveInstanceState(outState);
    }
}

xml中只需要写入一个MapView就ok

<com.amap.api.maps.MapView
        android:id="@+id/MapView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"></com.amap.api.maps.MapView>

还有一个反编译地理位置的代码,效果图就不上了

public class AddrChangeActivity extends AppCompatActivity implements View.OnClickListener, GeocodeSearch.OnGeocodeSearchListener {

    private EditText Addr_Edit;
    private Button Zheng_Btn;
    private EditText JinDU_Edit;
    private EditText WeiDu_Edit;
    private Button Fan_Btn;
    private TextView Get_Text;
    private GeocodeSearch geocodeSearch;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_addr_change);
        initView();
        //构造 GeocodeSearch 对象,并设置监听。
        geocodeSearch = new GeocodeSearch(this);
        geocodeSearch.setOnGeocodeSearchListener(this);
//通过GeocodeQuery设置查询参数,调用getFromLocationNameAsyn(GeocodeQuery geocodeQuery) 方法发起请求。
//address表示地址,第二个参数表示查询城市,中文或者中文全拼,citycode、adcode都ok
//        GeocodeQuery query = new GeocodeQuery(address, "010");
//        geocoderSearch.getFromLocationNameAsyn(query);

    }

    private void initView() {
        Addr_Edit = (EditText) findViewById(R.id.Addr_Edit);
        Zheng_Btn = (Button) findViewById(R.id.Zheng_Btn);
        JinDU_Edit = (EditText) findViewById(R.id.JinDU_Edit);
        WeiDu_Edit = (EditText) findViewById(R.id.WeiDu_Edit);
        Fan_Btn = (Button) findViewById(R.id.Fan_Btn);
        Get_Text = (TextView) findViewById(R.id.Get_Text);

        Zheng_Btn.setOnClickListener(this);
        Fan_Btn.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.Zheng_Btn:
                String addr = Addr_Edit.getText().toString().trim();
                if (addr.isEmpty()) {
                    Toast.makeText(this, "请输入地址", Toast.LENGTH_SHORT).show();
                    return;
                }
                //参数1:addr 地址值 参数2:规定一个区域
                GeocodeQuery query = new GeocodeQuery(addr, null);
                geocodeSearch.getFromLocationNameAsyn(query);
                break;
            case R.id.Fan_Btn:
                String jingdu = JinDU_Edit.getText().toString().trim();
                String weidu = WeiDu_Edit.getText().toString().trim();
                if (jingdu.isEmpty() || weidu.isEmpty()) {
                    Toast.makeText(this, "请输入经纬度", Toast.LENGTH_SHORT).show();
                    return;
                }
                //这个是经纬度查询的类
                LatLonPoint point = new LatLonPoint(Double.parseDouble(jingdu), Double.parseDouble(weidu));
                RegeocodeQuery regeocodeQuery = new RegeocodeQuery(point, 2000000000, GeocodeSearch.AMAP);
                GeocodeSearch reSe = new GeocodeSearch(this);
                reSe.setOnGeocodeSearchListener(new GeocodeSearch.OnGeocodeSearchListener() {
                    @Override
                    public void onRegeocodeSearched(RegeocodeResult regeocodeResult, int i) {
                        Log.e("onRegeocodeSearched", "onRegeocodeSearched");
                        RegeocodeAddress address = regeocodeResult.getRegeocodeAddress();
                        Get_Text.setText(address.getFormatAddress()+"地址");
                    }

                    @Override
                    public void onGeocodeSearched(GeocodeResult geocodeResult, int i) {

                    }
                });
                geocodeSearch.getFromLocationAsyn(regeocodeQuery);
                break;
        }
    }

    //把经纬度转换成地址
    @Override
    public void onRegeocodeSearched(RegeocodeResult regeocodeResult, int i) {
        Log.e("onRegeocodeSearched", "onRegeocodeSearched");
        RegeocodeAddress address = regeocodeResult.getRegeocodeAddress();
        Get_Text.setText(address.getFormatAddress());
    }

    //是吧地址转换成经度纬度
    @Override
    public void onGeocodeSearched(GeocodeResult geocodeResult, int i) {
        Log.e("onGeocodeSearched", "onGeocodeSearched");
        //从查出来的结果集 得到地址对象
        GeocodeAddress address = geocodeResult.getGeocodeAddressList().get(0);
        //从地址对象里面得到 经纬度的类
        LatLonPoint latLonPoint = address.getLatLonPoint();
        //从这个point取经纬度即可
        Get_Text.setText("经度是:" + latLonPoint.getLongitude() + ",纬度是:" + latLonPoint.getLatitude());

    }
}

反编译的xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".AddrChangeActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:weightSum="4">

        <EditText
            android:id="@+id/Addr_Edit"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="2.5"
            android:hint="请输入地址" />

        <Button
            android:id="@+id/Zheng_Btn"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1.5"
            android:text="解析" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:weightSum="4">

        <EditText
            android:id="@+id/JinDU_Edit"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1.5"
            android:hint="输入经度" />

        <EditText
            android:id="@+id/WeiDu_Edit"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1.5"
            android:hint="输入纬度" />

        <Button
            android:id="@+id/Fan_Btn"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="反向解析" />
    </LinearLayout>

    <TextView
        android:id="@+id/Get_Text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="memeda" />
</LinearLayout>

以上;

猜你喜欢

转载自blog.csdn.net/weixin_43603192/article/details/85114133