您的位置:首页 > 移动开发 > Android开发

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

2015-12-26 20:17 381 查看
这几天的项目不是非常紧。于是想为未来可能要做的项目做一些技术储备。

下一个项目非常有可能是定位开发,须要用到手机定位功能,于是查了查如今比較流行的第三方定位,最火的基本上就是百度定位>高德定位>腾讯定位了。

想了想不如做一个DEMO把三种定位方式混合一下试试。

BaiduLocTool.java

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

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

<span style="font-size:10px;">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;
}

}</span><span style="font-size:18px;">
</span>


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

百度地图:没什么问题。用起来非常方便。

高德地图: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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: