android高德地图定位

android高德地图定位
今天刚学的高德地图定位,本篇博客在上一篇高德地图基础上加的定位,分享给大家!
直接上代码:

  1. //终于找到报错“10: 定位服务启动失败”的问题
    //这个MapActivity放置的位置有关系,当Activity放在TabSpec中就有问题
    //如果单独作为一个activity则没有问题
  2. 代码:
    public class SecondActivity extends AppCompatActivity implements LocationSource, AMapLocationListener {

  3. private MapView mapView; private AMap map; //定位功能 private OnLocationChangedListener mListener; private AMapLocationClient mlocationClient; private AMapLocationClientOption mLocationOption; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_second); mapView = (MapView) findViewById(R.id.map); //找到地图控件 //在activity执行onCreate时执行mMapView.onCreate(savedInstanceState),创建地图 mapView.onCreate(savedInstanceState); // AMap aMap = mapView.getMap(); //初始化地图控制器对象\ map = mapView.getMap(); //修改地图的中心点位置 CameraPosition cp = map.getCameraPosition(); CameraPosition cpNew = CameraPosition.fromLatLngZoom(new LatLng(31.22, 121.48), cp.zoom); CameraUpdate cu = CameraUpdateFactory.newCameraPosition(cpNew); map.moveCamera(cu); //初始化定位服务 initLocationService(); } //初始化定位服务,这个地方有错误,这个地方map已经初始化 //因此定位的初始化代码就进不去了。 private void initLocationService() { if (map != null) { MyLocationStyle locationStyle = new MyLocationStyle(); locationStyle.myLocationIcon(BitmapDescriptorFactory.fromResource(R.drawable.a_a)); locationStyle.strokeColor(Color.BLACK); locationStyle.radiusFillColor(Color.argb(100, 0, 0, 180)); locationStyle.strokeWidth(1.0f); map.setMyLocationStyle(locationStyle); map.setLocationSource(this); map.getUiSettings().setMyLocationButtonEnabled(true); map.setMyLocationEnabled(true); } }/** * 方法必须重写 */ @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) { // TODO Auto-generated method stub if (amaplocation != null && mListener != null) { if (amaplocation != null && amaplocation.getErrorCode() == 0) { mListener.onLocationChanged(amaplocation); } else { String errText = "failed to locate," + amaplocation.getErrorCode()+ ": " + amaplocation.getErrorInfo(); Log.e("error",errText); }} }@Override public void activate(OnLocationChangedListener listener) { // TODO Auto-generated method stub mListener = listener; if (mlocationClient == null) { mlocationClient = new AMapLocationClient(getApplicationContext()); mLocationOption = new AMapLocationClientOption(); mlocationClient.setLocationListener(this); mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy); mlocationClient.setLocationOption(mLocationOption); mlocationClient.startLocation(); } }@Override public void deactivate() { // TODO Auto-generated method stub mListener = null; if (mlocationClient != null) { mlocationClient.stopLocation(); mlocationClient.onDestroy(); } mlocationClient = null; mLocationOption = null; } }






【android高德地图定位】

    推荐阅读