在上一篇已经写了嵌入高德地图,这里来说一下怎么实现定位,并显示定位的图标。
public class FragmentMap extends Fragment implements LocationSource, AMapLocationListener{ private static FragmentMap fragment = null; @ViewInject(R.id.map) private MapView mapView; private AMap aMap; private View mapLayout; private OnLocationChangedListener mListener; private LocationManagerProxy mAMapLocationManager; public static Fragment newInstance() { if (fragment == null) { synchronized (FragmentMap.class) { if (fragment == null) { fragment = new FragmentMap(); } } } return fragment; } public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { if (mapLayout == null) { mapLayout = inflater.inflate(R.layout.fragment_map, null); ViewUtils.inject(this, mapLayout); mapView.onCreate(savedInstanceState); if (aMap == null) { aMap = mapView.getMap(); aMap.setLocationSource(this);// 设置定位监听 aMap.getUiSettings().setMyLocationButtonEnabled(true);// 设置默认定位按钮是否显示 aMap.setMyLocationEnabled(true);// 设置为true表示显示定位层并可触发定位,false表示隐藏定位层并不可触发定位,默认是false // 自定义系统定位蓝点 MyLocationStyle myLocationStyle = new MyLocationStyle(); // 自定义定位蓝点图标 myLocationStyle.myLocationIcon(BitmapDescriptorFactory.fromResource(R.mipmap.content_btn_location)); // 将自定义的 myLocationStyle 对象添加到地图上 aMap.setMyLocationStyle(myLocationStyle); // 构造 LocationManagerProxy 对象 mAMapLocationManager = LocationManagerProxy.getInstance(getActivity()); } } else { if (mapLayout.getParent() != null) { ((ViewGroup) mapLayout.getParent()).removeView(mapLayout); } } return mapLayout; } @Override public void onAttach(Activity activity) { super.onAttach(activity); } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); } @Override public void onResume() { super.onResume(); mapView.onResume(); } /** * 方法必须重写 * map的生命周期方法 */ @Override public void onPause() { super.onPause(); mapView.onPause(); deactivate(); } /** * 方法必须重写 * map的生命周期方法 */ @Override public void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); mapView.onSaveInstanceState(outState); } /** * 方法必须重写 * map的生命周期方法 */ @Override public void onDestroy() { super.onDestroy(); mapView.onDestroy(); } @Override public void onLocationChanged(AMapLocation aMapLocation) { if (mListener != null && aMapLocation != null) { if (aMapLocation.getAMapException().getErrorCode() == 0) { mListener.onLocationChanged(aMapLocation);// 显示系统小蓝点 //获取位置信息 geoLat = aMapLocation.getLatitude(); geoLng = aMapLocation.getLongitude(); aMap.moveCamera(CameraUpdateFactory.zoomTo(14)); } } } @Override public void onLocationChanged(Location location) { } @Override public void onStatusChanged(String provider, int status, Bundle extras) { } @Override public void onProviderEnabled(String provider) { } @Override public void onProviderDisabled(String provider) { } //激活定位 @Override public void activate(OnLocationChangedListener listener) { mListener = listener; if (mAMapLocationManager == null) { mAMapLocationManager = LocationManagerProxy.getInstance(getActivity()); //此方法为每隔固定时间会发起一次定位请求,为了减少电量消耗或网络流量消耗, //注意设置合适的定位时间的间隔,并且在合适时间调用removeUpdates()方法来取消定位请求 //在定位结束后,在合适的生命周期调用destroy()方法 //其中如果间隔时间为-1,则定位只定一次 mAMapLocationManager.requestLocationData(LocationProviderProxy.AMapNetwork, 60 * 1000, 10, this); } } //停止定位 @Override public void deactivate() { mListener = null; if (mAMapLocationManager != null) { mAMapLocationManager.removeUpdates(this); mAMapLocationManager.destroy(); } mAMapLocationManager = null; } }
以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索this
, null
, 时间
蓝点
高德地图fragment定位、android 嵌入fragment、android 嵌入高德地图、android 高德地图定位、android 高德定位,以便于您获取更多的相关知识。
时间: 2024-10-25 14:35:18