Android 根據座標擷取地址

來源:互聯網
上載者:User

Android中根據座標擷取地址,一般用Geocoder,大概像下面這樣:

try{    Geocoder geo = new Geocoder(NewCommentActivity.this, Locale.getDefault());      List<Address> addresses = geo.getFromLocation(23.0897174,113.3116872, 1);      if (addresses.isEmpty()) {          Log.i("location", "addressed is Empty");      }      else {          if (addresses.size() > 0) {              Log.i("location", addresses.get(0).getFeatureName() + ", " + addresses.get(0).getLocality() +", " + addresses.get(0).getAdminArea() + ", " + addresses.get(0).getCountryName());          }      }  } 

當我使用這段代碼在真機上運行時,報錯:
java.io.IOException:Sevice not Available
at android.location.Geocoder.getFromLocation

Google了一下,都說是在模擬器上才有這個問題,真機不會有此問題。但我確確實實在真機上遇到了這個問題。
查看Android官方文檔,發現人家已經說的很明白了:
The Geocoder API is available for this purpose. Note that behind the scene, the API is dependent on a web service. If such service is unavailable on the device, the API will throw a "Service not Available exception" or return an empty list of addresses. A helper
method calledisPresent() was added in Android 2.3 (API level 9) to check for the existence of the service.

看來對於沒有這個服務的機型,確實是用不了這個方法的。那隻能另闢蹊徑了。在網上找到另外的方法:
訪問Google的這個URL可以獲得地址資訊的JSON字串。
String url = String.format("http://ditu.google.cn/maps/geo?output=json&key=abc&q=%s,%s",   latitude, longitude); 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.