高德地图中央大头针实时位置信息获取

                       今天项目需要要在地图上选择位置,中央有个定位图标然后移动地图实时返回指标的位置


              在网上查了很久的资料  发现资料并不多 特意写这篇博客方便各位骚年查找

             

            图标是我自己自定义的,直接添加在Activity中央,然后通过回调接口获取正中央的位置信息返回  下面贴出代码

    public class AddressMapActivity extends Activity implements LocationSource,
AMapLocationListener,OnMapClickListener,OnCameraChangeListener{
private MapView mapView;
   private AMap aMap;
   private String place;

//这个是显示地图位置信息的文本和确定按钮
   private TextView tv_local,tv_true;
   private OnLocationChangedListener mListener;
   private AMapLocationClient mlocationClient;
   private AMapLocationClientOption mLocationOption;
   double latitude;
   double longitude;
 
 




   @Override
   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.address_map);
       mapView = (MapView) findViewById(R.id.map);
       mapView.onCreate(savedInstanceState);// 此方法必须重写
      
       init();
   }


   /**
    * 初始化AMap对象
    */
   private void init() {
       if (aMap == null) {
           aMap = mapView.getMap();
           setUpMap();
           try {
            aMap.setOnMapClickListener(AddressMapActivity.this);
            aMap.setOnCameraChangeListener(this); 
} catch (Exception e) {
Log.e("222", e.getMessage());
}
           
       }
       tv_local=(TextView) findViewById(R.id.tv_local);
       tv_true=(TextView) findViewById(R.id.tv_true);
       tv_true.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
Intent intent=getIntent();
        intent.putExtra("local", latitude+","+longitude);
        intent.putExtra("latitude", latitude);
        intent.putExtra("longitude", longitude);
        intent.putExtra("place", place);
        setResult(RESULT_OK, intent); //intent为A传来的带有Bundle的intent,当然也可以自己定义新的Bundle
        finish();
}
});
   }
   /**
    * 设置一些amap的属性
    */
   private void setUpMap() {
      
           // 自定义系统定位小蓝点
    MyLocationStyle myLocationStyle;
myLocationStyle = new MyLocationStyle();// 初始化定位蓝点样式类
myLocationStyle.myLocationType(MyLocationStyle.LOCATION_TYPE_LOCATE);// 连续定位、且将视角移动到地图中心点,定位点依照设备方向旋转,并且会跟随设备移动。(1秒1次定位)如果不设置myLocationType,默认也会执行此种模式。
myLocationStyle.interval(2000); // 设置连续定位模式下的定位间隔,只在连续定位模式下生效,单次定位模式下不会生效。单位为毫秒。
aMap.setMyLocationStyle(myLocationStyle);// 设置定位蓝点的Style
aMap.getUiSettings().setMyLocationButtonEnabled(true);// 设置默认定位按钮是否显示,非必需设置。
aMap.setMyLocationEnabled(true);//
      
}
   /**
    * 方法必须重写
    */
   @Override
   protected void onResume() {
       super.onResume();
       mapView.onResume();
   }


   /**
    * 方法必须重写
    */
   @Override
   protected void onPause() {
       super.onPause();
       mapView.onPause();
   }


   /**
    * 方法必须重写
    */
   @Override
   protected void onSaveInstanceState(Bundle outState) {
       super.onSaveInstanceState(outState);
       mapView.onSaveInstanceState(outState);
   }


   /**
    * 方法必须重写
    */
   @Override
   protected void onDestroy() {
       super.onDestroy();
       mapView.onDestroy();
   }


   /**
    * 定位成功后回调函数
    */
   @Override
   public void onLocationChanged(AMapLocation amapLocation) {
      
           if (mListener != null && amapLocation != null) {
               if (amapLocation != null
                   /*开启定位时*/
                       && amapLocation.getErrorCode() == 0) {
                   mListener.onLocationChanged(amapLocation);// 显示系统小蓝点
                   latitude = amapLocation.getLatitude();
                   longitude = amapLocation.getLongitude();
                   place=amapLocation.getAddress();
                   mlocationClient.stopLocation();
               } else {
               /*没开启定位时*/
                   aMap.getUiSettings().setCompassEnabled(false);
                   aMap.moveCamera(CameraUpdateFactory.zoomTo(16));
                   aMap.getUiSettings().setScaleControlsEnabled(true);// 设置比例尺
                   MarkerOptions otMarkerOptions = new MarkerOptions();
                   mlocationClient.stopLocation();
               }
           }
       
   }
   /**
    * 激活定位
    */
   @Override
   public void activate(OnLocationChangedListener listener) {
       mListener = listener;
       if (mlocationClient == null) {
           mlocationClient = new AMapLocationClient(this);
           mLocationOption = new AMapLocationClientOption();
           //设置定位监听
           mlocationClient.setLocationListener(this);
           //设置为高精度定位模式
           mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
           //设置定位参数
           mlocationClient.setLocationOption(mLocationOption);
           // 此方法为每隔固定时间会发起一次定位请求,为了减少电量消耗或网络流量消耗,
           // 注意设置合适的定位时间的间隔(最小间隔支持为2000ms),并且在合适时间调用stopLocation()方法来取消定位请求
           // 在定位结束后,在合适的生命周期调用onDestroy()方法
           // 在单次定位情况下,定位无论成功与否,都无需调用stopLocation()方法移除请求,定位sdk内部会移除
           mlocationClient.startLocation();
       }


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


//地图点击事件
   @Override
   public void onMapClick(LatLng latLng) {
//点击地图后清理图层插上图标,在将其移动到中心位置
       aMap.clear();
//        latitude = latLng.latitude;
//        longitude = latLng.longitude;
//        MarkerOptions otMarkerOptions = new MarkerOptions();
//        otMarkerOptions.position(latLng);
//        aMap.addMarker(otMarkerOptions);
//        aMap.moveCamera(CameraUpdateFactory.changeLatLng(latLng));
       
   }
//这是重点方法

@Override
public void onCameraChange(CameraPosition arg0) {
// TODO 自动生成的方法存根
LatLng target = arg0.target;
   latitude=target.latitude;
   longitude=target.longitude;
GeocodeSearch geocoderSearch = new GeocodeSearch(this);   
geocoderSearch.setOnGeocodeSearchListener(new OnGeocodeSearchListener(){  
 
   @Override  
   public void onGeocodeSearched(GeocodeResult result, int rCode) {  
       // TODO Auto-generated method stub  
         
   }  
 
   @Override  
   public void onRegeocodeSearched(RegeocodeResult result, int rCode) {  
         
       String formatAddress = result.getRegeocodeAddress().getFormatAddress();  
       Log.e("formatAddress", "formatAddress:"+formatAddress);  
       place=formatAddress;
       tv_local.setText(place);
       Log.e("formatAddress", "rCode:"+rCode);  
         
   }});   
LatLonPoint   lp = new LatLonPoint(latitude,longitude);  
RegeocodeQuery query = new RegeocodeQuery(lp, 200,GeocodeSearch.AMAP);   
geocoderSearch.getFromLocationAsyn(query);   
 
}


@Override
public void onCameraChangeFinish(CameraPosition arg0) {
// TODO 自动生成的方法存根

}
}

猜你喜欢

转载自blog.csdn.net/lap602651857/article/details/71673536