public class WebViewActivity extends Activity implements AMapLocationListener, LocationSource, AMap.OnMapTouchListener, AMap.OnMarkerClickListener {
private AMap aMap;
private MapView mapView;
private AMapLocationClient mlocationClient;
private AMapLocationClientOption mLocationOption;
// private RadioGroup mGPSModeGroup;
private RelativeLayout tab_back_button;
MyLocationStyle myLocationStyle;
OnLocationChangedListener mListeners;
private boolean canMove = true;//变量控制拖动不返回中心点
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);// 不显示程序的标题栏
setContentView(R.layout.activity_map);
mapView = (MapView) findViewById(R.id.map);
mapView.onCreate(savedInstanceState);// 此方法必须重写
if (aMap == null) {
aMap = mapView.getMap();
}
aMap.setLocationSource(this);
myLocationStyle = new MyLocationStyle();
// myLocationStyle.interval(2000);
myLocationStyle.myLocationIcon(null);
//不显示Marker外的圆圈
myLocationStyle.strokeColor(Color.argb(00, 255, 255, 255));
myLocationStyle.radiusFillColor(Color.argb(00, 255, 255, 255));
myLocationStyle.myLocationType(MyLocationStyle.LOCATION_TYPE_MAP_ROTATE);
aMap.getUiSettings().setMyLocationButtonEnabled(true);
aMap.setMyLocationStyle(myLocationStyle);
aMap.setOnMapTouchListener(this);
aMap.setMyLocationEnabled(true);
aMap.setOnMarkerClickListener(this);
@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() {
mapView.onDestroy();
super.onDestroy();
}
@Override
public void onLocationChanged(AMapLocation aMapLocation) {
Log.d("activateactivate", "onLocationChanged");
//Log.d("activateactivate","aMapLocation.getErrorCode()="+aMapLocation.getErrorCode() );
if (mListeners != null && aMapLocation != null) {
if (aMapLocation != null
&& aMapLocation.getErrorCode() == 0) {
// Log.d("activateactivate","onLocationChanged"+aMapLocation.getAddress()+",,,"+aMapLocation.getCity());
if (canMove) {
mListeners.onLocationChanged(aMapLocation);
Log.d("activateactivate", aMapLocation.getPoiName());
}
} else {
String errText = "定位失败," + aMapLocation.getErrorCode() + ": " + aMapLocation.getErrorInfo();
Log.e("activateactivate", errText);
}
}
}
@Override
public void activate(OnLocationChangedListener onLocationChangedListener) {
canMove = true;
mListeners = onLocationChangedListener;
Log.d("activateactivate", "activateactivate");
if (mlocationClient == null) {
mlocationClient = new AMapLocationClient(getApplicationContext());
mLocationOption = new AMapLocationClientOption();
mlocationClient.setLocationListener(this);
// mLocationOption.setLocationPurpose(AMapLocationClientOption.AMapLocationPurpose.SignIn);
mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
mlocationClient.setLocationOption(mLocationOption);
mlocationClient.stopLocation();
mlocationClient.startLocation();
}
}
@Override
public void deactivate() {
Log.d("activateactivate", "deactivatedeactivate");
mListeners = null;
if (mlocationClient != null) {
mlocationClient.stopLocation();
mlocationClient.onDestroy();
}
mlocationClient = null;
}
@Override
public void onTouch(MotionEvent motionEvent) {
if (canMove) {
//用户拖动地图后,不再跟随移动,直到用户点击定位按钮
canMove = false;
}
}
}