Android.location.Address類方法獲取GPS定位信息

參考autojs的獲取GPS定位的代碼:

function getLocationLoop(){
    //判斷是否已經打開GPS模塊
    if(locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
          //GPS模塊打開,可以定位操作
            var criteria = new Criteria();
            criteria.setAccuracy(Criteria.ACCURACY_FINE);
            criteria.setAltitudeRequired(false);
            criteria.setBearingRequired(false);
            criteria.setCostAllowed(true);
            criteria.setPowerRequirement(Criteria.POWER_LOW);
            var provider = locationManager.getBestProvider(criteria, true);
            var location = locationManager.getLastKnownLocation(provider);
            log("經度:"+location.getLongitude()+"\n緯度:"+location.getLatitude())
        
            var gc = new android.location.Geocoder(context,java.util.Locale.getDefault());
            var result = gc.getFromLocation(location.getLatitude(),location.getLongitude(),1);
            log(result)
            log(result.get(0).getAddressLine(0))
        
            locationManager.requestLocationUpdates(provider, 1000, 10, new LocationListener({
            onLocationChanged:
                function(location){
                    log(location);
                }
            }));
        }
}

 

使用getFromLocation()函數獲得的Address類內容類似於這樣的:

Address[addressLines=[0:"浙江省xxx"],postalCode=null,countryCode=null,countryName=中國,hasLatitude=true,latitude=31.06799,hasLongitude=true,longitude=119.918762,phone=null,url=null,extras=null]

 

具體獲得某一項信息的函數:

  • getAdminArea() :返回狀態首字母縮略詞(“CA”,對於加利福尼亞州)
  • getCountryCode() :返回國家ISO代碼(“JP”,日本)
  • getCountryName() :返回國家名稱(“西class牙”,用於……西class牙)
  • getFeatureName() :返回該位置的名稱(如果有的話)(博物館的“盧浮宮”)
  • getLocality() :返回城市名稱(“倫敦”)
  • getPostalCode() :返回郵政編碼(“94110”,在美國)
  • getPremises() :???
  • getSubAdminArea() :???
  • getSubLocality() :???
  • getSubThoroughfare() :???
  • getThoroughfare() :返回街道和建築物編號(“1600 Amphitheatre Parkway”)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章