高德地图在Fragment中展示,并实现定位功能
注:
Fragment之间进行切换的时候,会出现不能定位
在Fragment生命周期方法,onDestroy中这样写
mLocationOption = null;
mLocationClient = null;
mMap = null;
mapView.onDestroy()
示例:
public class CurrentLocationFragment extends Fragment implements AMapLocationListener, LocationSource {
private Activity mActivity;
private AMap mMap;
private MapView mapView;
private OnLocationChangedListener mListener;
private AMapLocationClient mLocationClient;
private AMapLocationClientOption mLocationOption;
private LatLng myLocation;
private BitmapDescriptor successDescripter;
private EditText etAddress;
private TextView tvAddressName;
private LinearLayout llOk;
private View view;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
LogUtils.d("zh", "onCreateView进来了");
mActivity = getActivity();
view = View.inflate(mActivity, R.layout.fragment_current_location, null);
mapView = (MapView) view.findViewById(R.id.map_view);
etAddress = (EditText) view.findViewById(R.id.et_address);
tvAddressName = (TextView) view.findViewById(R.id.tv_address_name);
llOk = (LinearLayout) view.findViewById(R.id.ll_ok);
mapView.onCreate(savedInstanceState);
initMap();
setUpLocationStyle();
initListener();
return view;
}
private void initListener() {
llOk.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String signedAddress = etAddress.getText().toString().trim();
String addressName = tvAddressName.getText().toString().trim();
if (TextUtils.isEmpty(signedAddress)){
ToastUtils.showToast("请输入签到地点");
return;
}
}
});
}
private void initMap() {
if (mMap == null) {
mMap = mapView.getMap();
}
mMap.setLocationSource(this);
mMap.setMyLocationEnabled(true);
UiSettings uiSettings = mMap.getUiSettings();
uiSettings.setZoomControlsEnabled(true);
CameraUpdate cameraUpdate = CameraUpdateFactory.zoomTo(15);
mMap.moveCamera(cameraUpdate);
successDescripter = BitmapDescriptorFactory.fromResource(R.drawable.gps_point);
}
private void setUpLocationStyle() {
MyLocationStyle myLocationStyle = new MyLocationStyle();
myLocationStyle.myLocationIcon(BitmapDescriptorFactory.fromResource(R.drawable.gps_point));
myLocationStyle.strokeWidth(0);
myLocationStyle.radiusFillColor(Color.TRANSPARENT);
mMap.setMyLocationStyle(myLocationStyle);
}
@Override
public void onResume() {
super.onResume();
LogUtils.d("zh", "onResume进来了");
mapView.onResume();
}
@Override
public void onPause() {
super.onPause();
LogUtils.d("zh", mapView + ":onPause");
mapView.onPause();
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
LogUtils.d("zh", mapView + ":onSaveInstanceState");
mapView.onSaveInstanceState(outState);
}
@Override
public void onDestroy() {
super.onDestroy();
LogUtils.d("zh", mapView + ":onDestroy进来了");
mLocationOption = null;
mLocationClient = null;
mMap = null;
mapView.onDestroy();
}
@Override
public void onLocationChanged(AMapLocation aMapLocation) {
LogUtils.d("zh", "onLocationChanged进来了");
LogUtils.d("zh", aMapLocation + "");
LogUtils.d("zh", aMapLocation.getErrorCode() + "");
if (aMapLocation != null && aMapLocation.getErrorCode() == 0) {
if (mListener != null) {
mListener.onLocationChanged(aMapLocation);
}
String address = aMapLocation.getAddress();
LogUtils.d("zh",address);
myLocation = new LatLng(aMapLocation.getLatitude(), aMapLocation.getLongitude());
tvAddressName.setText(address);
}
}
@Override
public void activate(OnLocationChangedListener onLocationChangedListener) {
LogUtils.d("zh", "activate进来了");
mListener = onLocationChangedListener;
if (mLocationClient == null) {
mLocationClient = new AMapLocationClient(mActivity);
mLocationOption = new AMapLocationClientOption();
mLocationClient.setLocationListener(this);
mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
mLocationOption.setOnceLocation(true);
mLocationOption.setHttpTimeOut(2000);
mLocationClient.setLocationOption(mLocationOption);
mLocationClient.startLocation();
}
}
@Override
public void deactivate() {
LogUtils.d("zh", "deactivate我是什么时候进来的");
mListener = null;
if (mLocationClient != null) {
mLocationClient.stopLocation();
mLocationClient.onDestroy();
}
mLocationClient = null;
}
}