public class LocationActivity extends Activity implements LocationSource,
AMapLocationListener {
private MapView mapView;
private AMap aMap;
private OnLocationChangedListener listener;
private LocationManagerProxy proxy;
private UiSettings uiSettings;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_location);
mapView = (MapView) findViewById(R.id.mapview);
mapView.onCreate(savedInstanceState);
init();
}
private void init() {
if (aMap == null) {
aMap = mapView.getMap();
aMap.setLocationSource(this);// 设置定位监听
// 设置为true表示显示定位层并可触发定位,false表示隐藏定位层并不可触发定位,默认是false
aMap.setMyLocationEnabled(true);
// 设置定位的类型为定位模式:定位(AMap.LOCATION_TYPE_LOCATE)、跟随(AMap.LOCATION_TYPE_MAP_FOLLOW)
// 地图根据面向方向旋转(AMap.LOCATION_TYPE_MAP_ROTATE)三种模式
aMap.setMyLocationType(AMap.LOCATION_TYPE_LOCATE);
}
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
mapView.onDestroy();
deactivate();
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
mapView.onPause();
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
mapView.onResume();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
/*
*
* 定位成功后回掉 显示小蓝点
*/
@Override
public void onLocationChanged(AMapLocation arg0) {
// TODO Auto-generated method stub
if (listener != null && arg0 != null) {
if (arg0.getAMapException().getErrorCode() == 0) {
listener.onLocationChanged(arg0);
double latitude = arg0.getLatitude();
double longitude = arg0.getLongitude();
LatLng pos = new LatLng(latitude, longitude);
CameraUpdate cu2 = CameraUpdateFactory.changeLatLng(pos);
aMap.moveCamera(cu2);
MarkerOptions markerOptions = new MarkerOptions();
markerOptions.position(pos);
markerOptions.title(arg0.getAddress());
/*
* markerOptions.icon(BitmapDescriptorFactory.defaultMarker
* (BitmapDescriptorFactory.HUE_RED));
*/
// markerOptions.draggable(true);
Marker marker = aMap.addMarker(markerOptions);
marker.showInfoWindow();
Toast.makeText(LocationActivity.this, arg0.getAddress(), 0)
.show();
CameraUpdate cu1 = CameraUpdateFactory.zoomTo(14);
aMap.moveCamera(cu1);
CameraUpdate tilt = CameraUpdateFactory.changeTilt(30);
aMap.moveCamera(tilt);
}
}
}
/*
* 激活定位
*/
@Override
public void activate(OnLocationChangedListener arg0) {
// TODO Auto-generated method stub
listener = arg0;
if (proxy == null) {
proxy = LocationManagerProxy.getInstance(this);
// 此方法为每隔固定时间会发起一次定位请求,为了减少电量消耗或网络流量消耗,
// 注意设置合适的定位时间的间隔,并且在合适时间调用removeUpdates()方法来取消定位请求
// 在定位结束后,在合适的生命周期调用destroy()方法
// 其中如果间隔时间为-1,则定位只定一次
proxy.requestLocationData(LocationProviderProxy.AMapNetwork, 200,
-1, this);
}
}
@Override
public void deactivate() {
// TODO Auto-generated method stub
}
}