如何在Google Map Android V2中通过点击地图来获取城市名称?

4
通过点击地图,如何获取该位置的地址。在特定位置上点击时,我们可以听取并传递那些坐标,以获取地址,但它应该在Android Google地图v2中工作。

你尝试过什么吗? - Niko
在点击时在标记上放置一个标记,并读取标记的LatLng。这样用户就可以看到他点击的确切位置。 - bofredo
3个回答

2
如果您有经纬度数据,那么使用Geocoder是非常容易的。您需要使用Geocoder
    protected String doInBackground(Location... params) {
        Geocoder geocoder =
                new Geocoder(mContext, Locale.getDefault());
        // Get the current location from the input parameter list
        Location loc = params[0];
        // Create a list to contain the result address
        List<Address> addresses = null;
        try {
            /*
             * Return 1 address.
             */
            addresses = geocoder.getFromLocation(loc.getLatitude(),
                    loc.getLongitude(), 1);
        } catch (IOException e1) {
        Log.e("LocationSampleActivity",
                "IO Exception in getFromLocation()");
        e1.printStackTrace();
        return ("IO Exception trying to get address");
        } catch (IllegalArgumentException e2) {
        // Error message to post in the log
        String errorString = "Illegal arguments " +
                Double.toString(loc.getLatitude()) +
                " , " +
                Double.toString(loc.getLongitude()) +
                " passed to address service";
        Log.e("LocationSampleActivity", errorString);
        e2.printStackTrace();
        return errorString;
        }
        // If the reverse geocode returned an address
        if (addresses != null && addresses.size() > 0) {
            // Get the first address
            Address address = addresses.get(0);
            /*
             * Format the first line of address (if available),
             * city, and country name.
             */
            String addressText = String.format(
                    "%s, %s, %s",
                    // If there's a street address, add it
                    address.getMaxAddressLineIndex() > 0 ?
                            address.getAddressLine(0) : "",
                    // Locality is usually a city
                    address.getLocality(),
                    // The country of the address
                    address.getCountryName());
            // Return the text
            return addressText;
        } else {
            return "No address found";
        }
    }
    ...
}
...

最好的方法是使用AsyncTask。您可以在Android开发者网站上找到完整的示例:http://developer.android.com/training/location/display-address.html


这个解决方案帮助我修复了数组越界异常,非常感谢@adek。 - pallavi

2
这可能会帮助您使用完整的地图操作。 这里 提供了有关在Google地图Android API V2中触摸位置进行反向地理编码的信息。

谢谢,它已经很好地工作了:http://wptrafficanalyzer.in/blog/android-reverse-geocoding-at-touched-location-in-google-map-android-api-v2/ - pallavi

1
你可以使用安卓GeoCoder实现此功能。在GoogleMaps v2中,您可以长按以获取LatLng点。通过将这些坐标传递给GeoCoder,您可以进行“反向地理编码”以查找地址。

长按示例的链接中包含代码。这篇帖子来自S/O,其中有一个地理编码器示例。如果你在谷歌上搜索反向地理编码器或获取Android坐标地址,你会找到很多示例。 - Rarw
我在我的应用程序中使用的相同代码出现了索引越界异常。我已经在上面的同一页中发布了代码。 - pallavi
现在通过添加这行代码已经修复了错误:if (addresses != null && !addresses.isEmpty()) - pallavi
我本来不会发现这个问题,因为你没有发布任何代码 ;) 不过很高兴你已经解决了。 - Rarw

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接