Android 高德地图点击地图获取经纬度
16lz
2021-01-23
public class AddressMapActivity extends BaseActivity implements LocationSource, AMapLocationListener, AMap.OnMapClickListener{ private MapView mapView; private AMap aMap; 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_lba_map); mapView = (MapView) findViewById(R.id.map); mapView.onCreate(savedInstanceState);// 此方法必须重写 } /** * 初始化AMap对象 */ private void init() { if (aMap == null) { aMap = mapView.getMap(); setUpMap(); aMap.setOnMapClickListener(this); } } /** * 设置一些amap的属性 */ private void setUpMap() { // 自定义系统定位小蓝点 MyLocationStyle myLocationStyle = new MyLocationStyle(); myLocationStyle.myLocationIcon(BitmapDescriptorFactory .fromResource(R.drawable.icon_gcoding));// 设置小蓝点的图标 myLocationStyle.strokeColor(Color.argb(0, 0, 0, 0));// 设置圆形的边框颜色 myLocationStyle.radiusFillColor(Color.argb(0, 0, 0, 0));// 设置圆形的填充颜色 myLocationStyle.strokeWidth(0f);// 设置圆形的边框粗细 // myLocationStyle.anchor(int,int)//设置小蓝点的锚点// getMap().setLatLonQuanVisible(false); aMap.getUiSettings().setCompassEnabled(false); aMap.moveCamera(CameraUpdateFactory.zoomTo(16)); aMap.setMyLocationStyle(myLocationStyle); aMap.setLocationSource(this);// 设置定位监听 aMap.getUiSettings().setMyLocationButtonEnabled(false);// 设置默认定位按钮是否显示 aMap.setMyLocationEnabled(true);// 设置为true表示显示定位层并可触发定位,false表示隐藏定位层并不可触发定位,默认是false // aMap.setMyLocationType() } /** * 方法必须重写 */ @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);// 显示系统小蓝点 LogUtils.e("DWlatlog", amapLocation.getLatitude() + ""); latitude = amapLocation.getLatitude(); longitude = amapLocation.getLongitude(); city = amapLocation.getCity(); mlocationClient.stopLocation(); select = 1; listView.removeFooterView(listNot); doSearchQuery(); } else { /*没开启定位时*/ String notLat = MobclickAgent.getConfigParams(this, "defaultLatitude"); String notLog = MobclickAgent.getConfigParams(this, "defaultLongitude"); LatLng latLng = new LatLng(Double.valueOf(notLat), Double.valueOf(notLog)); aMap.getUiSettings().setCompassEnabled(false); aMap.moveCamera(CameraUpdateFactory.zoomTo(16)); aMap.getUiSettings().setScaleControlsEnabled(true);// 设置比例尺 MarkerOptions otMarkerOptions = new MarkerOptions(); otMarkerOptions.position(latLng); otMarkerOptions.draggable(true); //下面这个是标记上面这个经纬度在地图的位置是 otMarkerOptions.icon(BitmapDescriptorFactory.fromResource(R.drawable.icon_gcoding)); aMap.moveCamera(CameraUpdateFactory.changeLatLng(latLng)); aMap.addMarker(otMarkerOptions); city = amapLocation.getCity(); latitude = latLng.latitude; longitude = latLng.longitude; select = 1; listView.removeFooterView(listNot); doSearchQuery(); 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.icon(BitmapDescriptorFactory.fromResource(R.drawable.icon_gcoding)); otMarkerOptions.position(latLng); aMap.addMarker(otMarkerOptions); aMap.moveCamera(CameraUpdateFactory.changeLatLng(latLng));
}
}
更多相关文章
- android学习——android 常见的错误 和 解决方法
- Android详细的对话框AlertDialog.Builder使用方法
- Android Studio 3.0 gradle提示太老 解决方法
- Android 中自定义控件和属性(attr.xml,declare-styleable,TypedA
- [Android]LayoutInflater的inflate方法半详解
- Android与Unity交互调用mUnityPlayer.quit()方法退出返回上一个A
- Unity3D调用android方法(非插件方式)
- Android事件处理方法总结-Handler消息处理