标签:
最近在写天气预报的app,写完以后会将源码开源,今天先写app中用到的定位问题,现在定位的SDK有很多高德地图、百度地图、腾讯地图等,腾讯地图没有用过不予评价,高德地图和百度地图对比我觉得从开发平台的给的demo来说,高德地图给的demo中的代码可读性更强,模块整理的也很好,从准确性上说个人觉得高德地图的准确性比百度地图要好很多,以前做的项目中有个导航功能,刚开始老大说用百度地图说用户量大,精度应该也更大,可是事实却不是如此,我记得当时用到了地理编码和反地理编码,误差有时候有1-2公里(不知道现在是否已经优化),后来果断改成高德地图圆满完成导航功能。
废话不说了直接进入正题,需要用高德地图实现定位功能我们首先需要到高德地图开发平台上去下载定位SDK,然后减压以后复制到app目录下libs目录下:
复制完成以后需要到build.gradle中配置如下图所示:
定义AMapLocationListener
public interface AMapLocationListener {
void onLocationChanged(AMapLocation var1);
}
用Service来实现定位功能:
public class LocationService extends Service implements
AMapLocationListener {
//声明AMapLocationClient类对象
public AMapLocationClient mLocationClient = null;
//声明mLocationOption对象
public AMapLocationClientOption mLocationOption = null;
private LocationBinder binder = new LocationBinder();
private ChangeLocation changeLocation;
@Override
public void onCreate() {
initLocation();
}
/**
* 注册回调接口的方法,供外部调用
*/
public void setChangeLocation(ChangeLocation changeLocationr) {
this.changeLocation = changeLocationr;
}
private void initLocation() {
mLocationClient = new AMapLocationClient(this.getApplicationContext());
mLocationOption = new AMapLocationClientOption();
// 设置定位模式为高精度模式
mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
// 设置定位监听
mLocationClient.setLocationListener(LocationService.this);
//设置是否返回地址信息(默认返回地址信息)
mLocationOption.setNeedAddress(true);
//设置是否只定位一次,默认为false
mLocationOption.setOnceLocation(false);
//设置是否强制刷新WIFI,默认为强制刷新
mLocationOption.setWifiActiveScan(true);
//设置是否允许模拟位置,默认为false,不允许模拟位置
mLocationOption.setMockEnable(false);
//设置定位间隔,单位毫秒,默认为2000ms
mLocationOption.setInterval(10000);
//给定位客户端对象设置定位参数
mLocationClient.setLocationOption(mLocationOption);
//启动定位
mLocationClient.startLocation();
}
@Override
public IBinder onBind(Intent intent) {
return binder;
}
public class LocationBinder extends Binder {
public LocationService getLocationService() {
return LocationService.this;
}
}
@Override
public void onDestroy() {
mLocationClient.stopLocation();//停止定位
mLocationClient.onDestroy();//销毁定位客户端。
}
public void onLocationChanged(AMapLocation amapLocation) {
if (amapLocation != null) {
changeLocation.getLocation(amapLocation);
}
}
}
在Activity中绑定LocationService来接收位置信息
private ServiceConnection serviceConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
LocationService.LocationBinder binder = (LocationService.LocationBinder) service;
locationService = binder.getLocationService();
locationService.setChangeLocation(new ChangeLocation() {
@Override
public void getLocation(AMapLocation amapLocation) {
if (amapLocation.getErrorCode() == 0) {
Toast.makeText(MainActivity.this, amapLocation.getCity(), Toast.LENGTH_SHORT).show();
PreferenceUtil.getInstance().init(MainActivity.this);
PreferenceUtil.getInstance().putString("location", amapLocation.getCity());
} else {
Toast.makeText(MainActivity.this, "location Error, ErrCode:"
+ amapLocation.getErrorCode() + ", errInfo:"
+ amapLocation.getErrorInfo(), Toast.LENGTH_SHORT).show();
}
}
});
}
mIntent = new Intent(MainActivity.this, LocationService.class);
bindService(mIntent, serviceConnection, Context.BIND_AUTO_CREATE);
@Override
public void onServiceDisconnected(ComponentName name) {
}
};
@Override
protected void onDestroy() {
unbindService(serviceConnection);
super.onDestroy();
}
好了,这样定位功能就ok了,看下运行的图
附上AMapLocation类中相关方法:
标签:
原文地址:http://blog.csdn.net/luyouxin/article/details/51356303