1. 程式人生 > >Android之高德地圖定位

Android之高德地圖定位

最近在寫天氣預報的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類中相關方法:
這裡寫圖片描述