Android定位开发之百度定位、高德定位、腾讯定位,三足鼎立一起为我所用!

这几天的项目不是很紧,于是想为未来可能要做的项目做一些技术储备。
下一个项目很有可能是定位开发,需要用到手机定位功能,于是查了查现在比较流行的第三方定位,最火的基本上就是百度定位>高德定位>腾讯定位了。
想了想不如做一个DEMO把三种定位方式混合一下试试。

BaiduLocTool.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package com.dhcc.mixlocation;  

import android.content.Context;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.location.LocationClientOption.LocationMode;

public class BaiduLocTool implements BDLocationListener{

public LocationClient mLocationClient;

public LocationClient getmLocationClient() {
return mLocationClient;
}

private Context mContext;

public BaiduLocTool(Context context){

mContext=context;
}


public void init(){

mLocationClient = new LocationClient(mContext);

InitLocation();

mLocationClient.registerLocationListener(this);

mLocationClient.start();
}

private void InitLocation(){
LocationClientOption option = new LocationClientOption();
option.setLocationMode(LocationMode.Hight_Accuracy);//设置定位模式
option.setScanSpan(1000);//设置发起定位请求的间隔时间为5000ms
option.setIsNeedAddress(true);
mLocationClient.setLocOption(option);
}

@Override
public void onReceiveLocation(BDLocation location) {
// TODO Auto-generated method stub
((MainActivity)mContext).phoneCall("百度定位:"+"纬度:"+location.getLatitude()+" | "+"经度:"+location.getLongitude()+" | "+"反地理编码:"+location.getAddrStr());
mLocationClient.stop();
}

public void destroyLocManager(){
if(mLocationClient!=null)
mLocationClient.stop();
mLocationClient=null;
}

}

GaodeLocTool.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80

package com.dhcc.mixlocation;

import android.content.Context;
import android.location.Location;
import android.os.Bundle;
import android.util.Log;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationListener;
import com.amap.api.location.LocationManagerProxy;
import com.amap.api.location.LocationProviderProxy;

public class GaodeLocTool implements AMapLocationListener{

Context mContext;
private LocationManagerProxy mLocationManagerProxy;

public GaodeLocTool(Context context){

this.mContext=context;

}

public void init(){


mLocationManagerProxy = LocationManagerProxy.getInstance(mContext);

mLocationManagerProxy.requestLocationData(
LocationProviderProxy.AMapNetwork, 1000, 15, this);
mLocationManagerProxy.setGpsEnable(true);

}

@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
Log.e("onLocationChanged", "Location");
}

@Override
public void onProviderDisabled(String arg0) {
// TODO Auto-generated method stub
Log.e("onProviderDisabled", "onProviderDisabled");
}

@Override
public void onProviderEnabled(String arg0) {
// TODO Auto-generated method stub
Log.e("onProviderEnabled", "onProviderEnabled");
}

@Override
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
// TODO Auto-generated method stub
Log.e("onStatusChanged", "onStatusChanged");
}

@Override
public void onLocationChanged(AMapLocation location) {
// TODO Auto-generated method stub
Log.e("onLocationChanged", "AMapLocation");
((MainActivity)mContext).phoneCall("高德定位:"+"混合定位方式:"+location.getProvider()+" | "+"纬度:"+location.getLatitude()+" | "+"经度:"+location.getLongitude()+" | "+"反地理编码:"+location.getAddress());
mLocationManagerProxy.removeUpdates(this);
}



public void destroyLocManager() {

if (mLocationManagerProxy != null) {
mLocationManagerProxy.removeUpdates(this);
mLocationManagerProxy.destroy();
}
mLocationManagerProxy = null;

}

}

TencentLocTool.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package com.dhcc.mixlocation;

import android.content.Context;
import android.util.Log;

import com.tencent.map.geolocation.TencentLocation;
import com.tencent.map.geolocation.TencentLocationListener;
import com.tencent.map.geolocation.TencentLocationManager;
import com.tencent.map.geolocation.TencentLocationRequest;

public class TecentLocTool implements TencentLocationListener {

String LocInfo;
String task;
Context main;
TencentLocationManager mLocationManager;

public String getLocInfo() {
return LocInfo;
}

public void setLocInfo(String locInfo) {
LocInfo = locInfo;
}

public TecentLocTool(Context main) {
this.main = main;
mLocationManager=TencentLocationManager.getInstance(main);

}


public void init(){
int error = TencentLocationManager.getInstance(main)
.requestLocationUpdates(
TencentLocationRequest
.create().setInterval(5000)
.setRequestLevel(
TencentLocationRequest.REQUEST_LEVEL_NAME), this);
if (error == 0) {

Log.e("监听状态:", "监听成功!");

} else if (error == 1) {

Log.e("监听状态:", "设备缺少使用腾讯定位SDK需要的基本条件");

} else if (error == 2) {

Log.e("监听状态:", "配置的 key 不正确");

}


}

/**
* @param location
* 新的位置
* @param error
* 错误码
* @param reason
* 错误描述
*/

@Override
public void onLocationChanged(TencentLocation location, int error,
String reason)
{

// TODO Auto-generated method stub
if (TencentLocation.ERROR_OK == error) {

double lat = location.getLatitude();// 纬度
double lot = location.getLongitude();// 经度
double altitude = location.getAltitude();// 海拔
float accuracy = location.getAccuracy();// 精度
String nation = location.getNation();// 国家
String province = location.getProvince();// 省份
String city = location.getCity();// 城市
String district = location.getDistrict();// 区
String town = location.getTown();// 镇
String village = location.getVillage();// 村
String street = location.getStreet();// 街道
String streetNo = location.getStreetNo();// 门号



Log.e("定位信息:", lat + " | " + lot + " | " + altitude + " | "
+ accuracy + " | " + nation + " | " + province + " | "
+ city + " | " + district + " | " + town + " | " + village
+ " | " + street + " | " + streetNo);

task = lat + " | " + lot + " | " + altitude + " | " + accuracy
+ " | " + nation + " | " + province + " | " + city + " | "
+ district + " | " + town + " | " + village + " | "
+ street + " | " + streetNo + " | ";


String provider=location.getProvider();//定位方式
if (TencentLocation.GPS_PROVIDER.equals(provider)) {
// location 是GPS定位结果
Log.e("定位方式:","GPS");
((MainActivity)main).phoneCall("腾讯定位:"+task+"GPS");
} else if (TencentLocation.NETWORK_PROVIDER.equals(provider)) {
// location 是网络定位结果
Log.e("定位方式:","NetWork");
((MainActivity)main).phoneCall("腾讯定位:"+task+"NetWork"+" | "+location.getAddress());
Log.e("地址:", location.getAddress());
}
mLocationManager.removeUpdates(this);
} else {


Log.e("reason:", reason);
Log.e("error:", error + "");

}

}

/**
* @param name
* GPS,Wi-Fi等
* @param status
* 新的状态, 启用或禁用
* @param desc
* 状态描述
*/

@Override
public void onStatusUpdate(String name, int status, String desc) {
// TODO Auto-generated method stub
Log.e("name:", name);
Log.e("status:", ""+status);
Log.e("desc:", desc);

}

public void destroyLocManager() {


if(mLocationManager!=null)
mLocationManager.removeUpdates(this);
mLocationManager=null;
}

}

记录一下开发中遇到的问题:

百度地图:没什么问题,用起来很方便。
高德地图:LocationProviderProxy.AMapNetwork 只支持网络定位,无法纯GPS定位,如果想要纯GPS定位就必须把这个参数改成LocationManagerProxy.GPS_PROVIDER。很蛋疼,这里需要自己来逻辑判断一下。
腾讯地图:如果想要纯GPS定位的话,需要把定位坐标改成地球坐标:mLocationManager.setCoordinateType(TencentLocationManager.COORDINATE_TYPE_WGS84);
但是面临的问题是这个坐标没法反地理编码。如果想反地理编码,就必须把坐标改成火星坐标,mLocationManager.setCoordinateType(TencentLocationManager.COORDINATE_TYPE_GCJ02);
但是这样又没法纯GPS定位,所以我个人觉得腾讯定位只要用他的网络定位就好了,不要用他的GPS定位了 好麻烦。

在我所在的位置测试的话,三种定位的网络定位,反地理编码最准确的是:腾讯定位,其次是百度定位,最差的是高德定位。

项目源码下载地址:
http://download.csdn.net/detail/jasoncol_521/8775663

热评文章