Location

在android設備上,應用程式只要經過使用者的同意,就能透過手機中的GPS模組取得目前的位置
在應用程式中通常會使用或記錄這些定位資訊,進而實現某些功能

為了取得當前位置,Google官方有提供2種在android設備中取得定位的APIs

那麼應該要用哪一種API呢?
其實答案是都可以,2種寫法都能夠取得目前的定位

不過Android官方教學文件強力推薦使用Google Play service location APIs
文件中也有提到,如果你用的是原本的android.location這個package下的API
那麼Google建議你應該盡快換成使用Google Play service location APIs

TheGoogle Play services location APIsare preferred over the Android framework location APIs (android.location) as a way of adding location awareness to your app. If you are currently using the Android framework location APIs, you are strongly encouraged to switch to the Google Play services location APIs as soon as possible.

使用Google Play service location APIs,作者爬了一些文章後,歸納出以下幾個特點

  • API如果更新,支援度與普及率會相對高
    android.location是原生android系統提供的API,也就代表了API如果要更新
    只有新版的android系統保證能夠支援,不一定會下向相容到其他舊版的android系統
    不過Google Play Service的服務是以app的形式預設安裝於系統中的
    所以有任何重大更新,只需要在Google Play上發布更新讓使用者安裝即可
    這樣的做法將大幅提高新API的支援度以及普及率

  • 能夠判斷裝置移動時的運輸模式

  • 建立和監視預先定義的地理區域(稱為地理柵欄)

接下來就是使用Google Play service location APIs的相關教學啦

首先要在

compile 'com.google.android.gms:play-services-location:11.6.0'
allprojects {
    repositories {
        jcenter()
        maven {
            url "https://maven.google.com"
        }
    }
}

取得最後一筆已知的定位

Reference

Making Your App Location-Aware | Android Developers
Google Map位置資料#Google Play 服務 Location API


Android : 如何取得現在現在位置的經緯度(一)
[Android] 取得目前位置顯示在Google Map上

Android - 取得 GPS 資訊 - LocationListener

results matching ""

    No results matching ""