【问题标题】:Can I implement the reverse geocoding function within my ActivityMaps.java file?我可以在我的 ActivityMaps.java 文件中实现反向地理编码功能吗?
【发布时间】:2015-05-29 14:12:20
【问题描述】:

我目前正在 Android Studio 1.1.0 中开发一个应用程序,并且我已经实现了 Google Maps API,我可以从中检索用户当前位置,但我想将其转换为地址。我可以在 MapsActivity.java 文件中使用以下代码吗?

function getReverseGeocodingData(lat, lng) {

   var latlng = new google.maps.LatLng(lat, lng);
   // This is making the Geocode request
   var geocoder = new google.maps.Geocoder();
   geocoder.geocode({ 'latLng': latlng }, function (results, status) {
      if (status !== google.maps.GeocoderStatus.OK) {
        alert(status);
        }

    // This is checking to see if the Geoeode Status is OK before proceeding
        if (status == google.maps.GeocoderStatus.OK) {
        console.log(results);

        var address = (results[0].formatted_address);
    }
});

}

任何帮助将不胜感激。对 Android Studio 来说还是比较新的

【问题讨论】:

    标签: java android google-maps geocoding reverse-geocoding


    【解决方案1】:

    您可以使用 android.location.Geocoder 轻松完成此操作。

     // get current locality based on lat lng
        Geocoder geocoder;
        List<Address> addresses;
        geocoder = new Geocoder(this, Locale.getDefault());
        addresses = geocoder.getFromLocation(latitude, longitude, 1); // Here 1 represent max location result to returned, by documents it recommended 1 to 5
    
        String address = addresses.get(0).getAddressLine(0); // If any additional address line present than only, check with max available address lines by getMaxAddressLineIndex()
        String city = addresses.get(0).getLocality();
        String state = addresses.get(0).getAdminArea();
        String country = addresses.get(0).getCountryName();
        String postalCode = addresses.get(0).getPostalCode();
        String knownName = addresses.get(0).getFeatureName();
    

    为了一个好的做法,请确保您在 AsyncTask 中执行此操作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-15
      • 2021-05-02
      • 2010-11-08
      • 1970-01-01
      相关资源
      最近更新 更多