高德地图实现逻辑及代码

注册权限














    
    
    
    
    
    
    
    
    


//还需要在activity下写


android:name=“com.amap.api.v2.apikey”
//value里面是对应的key值
android:value=“bca4fcbedc4025903cff128fc6cff617”>

//还要导入 一个lib包高德开放平台找
//新建一个包,名为jniLibs,放在main包里也是高德开放平台找.
//具体代码逻辑
private MapView map=null;
private MyLocationStyle myLocationStyle;
private AMap aMap;
AMapLocationClientOption mLocationOption;
AMapLocationClient mlocationClient;
OnLocationChangedListener mListener;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    initView();
    if (aMap == null) {
        aMap = map.getMap();
    }
    map.onCreate(savedInstanceState);
    Fest();
}


public void Fest() {
    //初始化定位蓝点样式类
    myLocationStyle = new MyLocationStyle();
    aMap.getUiSettings().setMyLocationButtonEnabled(true);//设置默认定位按钮是否显示,非必需设置。
    myLocationStyle.strokeColor(Color.parseColor("#2c78c2"));
    myLocationStyle.radiusFillColor(Color.parseColor("#A6FFFFFF"));
    aMap.setMyLocationStyle(myLocationStyle);//设置定位蓝点的Style
    CameraUpdate cameraUpdate = CameraUpdateFactory.zoomTo(18);//缩放级别
    aMap.moveCamera(cameraUpdate);
    // 设置定位监听
    aMap.setLocationSource(this);
    // 设置为true表示显示定位层并可触发定位,false表示隐藏定位层并不可触发定位,默认是false
    aMap.setMyLocationEnabled(true);
    // 设置定位的类型为定位模式,有定位、跟随或地图根据面向方向旋转几种
    aMap.setMyLocationType(AMap.LOCATION_TYPE_LOCATE);
}
@Override
public void onLocationChanged(AMapLocation aMapLocation) {
    if (mListener != null && aMapLocation != null) {
        if (aMapLocation != null
                && aMapLocation.getErrorCode() == 0) {
            mListener.onLocationChanged(aMapLocation);// 显示系统小蓝点
            String country = aMapLocation.getCountry();//国家信息
            String city = aMapLocation.getCity();//获取城市
            double tude = aMapLocation.getLatitude();//维度
            double longitude = aMapLocation.getLongitude();//经度
            String city_code = aMapLocation.getCityCode();
            String dis = aMapLocation.getDistrict();//城区信息
            String street = aMapLocation.getStreet();//街道信息
            String num = aMapLocation.getStreetNum();//街道门牌号信息
            aMapLocation.getLocationType();//获取当前定位结果来源,如网络定位结果,详见定位类型表
            aMapLocation.getLatitude();//获取纬度
            aMapLocation.getLongitude();//获取经度
            aMapLocation.getAccuracy();//获取精度信息
            aMapLocation.getAddress();//地址,如果option中设置isNeedAddress为false,则没有此结果,网络定位结果中会有地址信息,GPS定位不返回地址信息。
            aMapLocation.getCountry();//国家信息
            aMapLocation.getProvince();//省信息
            aMapLocation.getCity();//城市信息
            aMapLocation.getDistrict();//城区信息
            aMapLocation.getStreet();//街道信息
            aMapLocation.getStreetNum();//街道门牌号信息
            aMapLocation.getCityCode();//城市编码
            aMapLocation.getAdCode();//地区编码


            Toast.makeText(MainActivity.this, country + "==" + city + "==" + street + "===" + dis + "==" + num + city_code + "==" + tude + "===" + longitude, Toast.LENGTH_SHORT).show();
        } else {
            String errText = "定位失败," + aMapLocation.getErrorCode() + ": " + aMapLocation.getErrorInfo();
            Log.e("AmapErr", errText);
        }
    }
}
@Override
public void activate(OnLocationChangedListener onLocationChangedListener) {
    mListener = onLocationChangedListener;
    if (mlocationClient == null) {
        //初始化定位
        mlocationClient = new AMapLocationClient(this);
        //初始化定位参数
        mLocationOption = new AMapLocationClientOption();
        //设置定位回调监听
        mlocationClient.setLocationListener(this);
        //设置为高精度定位模式
        mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
        //设置定位参数
        mlocationClient.setLocationOption(mLocationOption);
        // 此方法为每隔固定时间会发起一次定位请求,为了减少电量消耗或网络流量消耗,
        // 注意设置合适的定位时间的间隔(最小间隔支持为2000ms),并且在合适时间调用stopLocation()方法来取消定位请求
        mLocationOption.setInterval(1000L);
        // 在定位结束后,在合适的生命周期调用onDestroy()方法
        // 在单次定位情况下,定位无论成功与否,都无需调用stopLocation()方法移除请求,定位sdk内部会移除
        mlocationClient.startLocation();//启动定位

    }
}

@Override
public void deactivate() {
    mListener = null;
    if (mlocationClient != null) {
        mlocationClient.stopLocation();
        mlocationClient.onDestroy();
    }
    mlocationClient = null;
}
@Override
protected void onDestroy() {
    super.onDestroy();
    map.onDestroy();
}

private void initView() {
    map = (MapView) findViewById(R.id.map);
}

你可能感兴趣的:(高德地图实现逻辑及代码)