Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android定位開發之百度定位、高德定位、騰訊定位,三足鼎立一起為我所用!

Android定位開發之百度定位、高德定位、騰訊定位,三足鼎立一起為我所用!

編輯:關於Android編程

這幾天的項目不是很緊,於是想為未來可能要做的項目做一些技術儲備。

下一個項目很有可能是定位開發,需要用到手機定位功能,於是查了查現在比較流行的第三方定位,最火的基本上就是百度定位>高德定位>騰訊定位了。

想了想不如做一個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

 

 

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定位了 好麻煩。

 

 

在我所在的位置測試的話,三種定位的網絡定位,反地理編碼最准確的是:騰訊定位,其次是百度定位,最差的是高德定位。
 

 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved