(1)获取高德地图android SDK的key
具体过程可以参考:《申请高德地图android SDK的Key》
(2)配置andriod中的manifest文件
(2.1)配置key
android:icon="@drawable/logo"
android:label="@string/app_name"
android:theme="@style/NormalPageTheme" >
android:value="你的key" />
(2.2)配置权限
(3)Acitivity中使用定位功能
(3.1)Layout中加载MapView对象,在Acitivity中获取MapView对象和Map对象
(3.2)设置定位来源
if (map != null) {
MyLocationStyle locationStyle = new MyLocationStyle();
locationStyle.myLocationIcon(BitmapDescriptorFactory.fromResource(R.drawable.location_marker));
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);
}
将Activity设置为的定位来源,因此该Activity需要实现接口LocationSource(包括active和disactive)
(3.3)设置位置监听器
public void activate(OnLocationChangedListener listener) {
// TODO Auto-generated method stub
mListener = listener;
if (mlocationClient == null) {
mlocationClient = new AMapLocationClient(this);
mLocationOption = new AMapLocationClientOption();
mlocationClient.setLocationListener(this);
mLocationOption.setLocationMode(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;
}
上面也是将Activity设置为监听对象,因此需要在activity中实现位置监听器的onLocationChanged函数
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);
}
}
}
(3.5)定位成功
(4)如果出现“无法启动定位服务的错误”
此时,errorCode返回值为10,这个因为你把该Activity放在TabHost中进行渲染。你需要将创建LocationClient
mlocationClient = new AMapLocationClient(this);
这句代码改成
mlocationClient = new AMapLocationClient(getApplicationContext());
如果你的Activity不是放在TabHost进行渲染,则不需要进行上面的改动。
(5)完整的代码
package com.example.allsamples;
import android.R.color;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.location.Location;
import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationClientOption.AMapLocationMode;
import com.amap.api.location.AMapLocationListener;
import com.amap.api.maps2d.AMap;
import com.amap.api.maps2d.CameraUpdate;
import com.amap.api.maps2d.CameraUpdateFactory;
import com.amap.api.maps2d.LocationSource;
import com.amap.api.maps2d.MapView;
import com.amap.api.maps2d.model.BitmapDescriptorFactory;
import com.amap.api.maps2d.model.CameraPosition;
import com.amap.api.maps2d.model.LatLng;
import com.amap.api.maps2d.model.MyLocationStyle;
//终于找到报错“10: 定位服务启动失败”的问题
//这个MapActivity放置的位置有关系,当Activity放在TabSpec中就有问题
//如果单独作为一个activity则没有问题
public class MapActivity extends Activity implements LocationSource,
AMapLocationListener{
private MapView mapView;
private AMap map;
//定位功能
private OnLocationChangedListener mListener;
private AMapLocationClient mlocationClient;
private AMapLocationClientOption mLocationOption;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.forum_home_page);
mapView = (MapView)findViewById(R.id.mapview);
mapView.onCreate(savedInstanceState);
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.location_marker));
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() {
// TODO Auto-generated method stub
mapView.onResume();
super.onResume();
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
mapView.onPause();
deactivate();
super.onPause();
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
mapView.onDestroy();
super.onDestroy();
}
@Override
protected void onSaveInstanceState(Bundle outState) {
// TODO Auto-generated method stub
mapView.onSaveInstanceState(outState);
super.onSaveInstanceState(outState);
}
@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(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;
}
}