如何在谷歌地图android v2的地图上获取城市名称ontap?

时间:2013-08-13 11:48:10

标签: android google-maps map coordinates android-maps-v2

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

3 个答案:

答案 0 :(得分:2)

This可以帮助您使用完整的地图操作

答案 1 :(得分:2)

如果你有LatLng数据 - 这很容易。您需要使用Goecoder

    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 Devlopers的网站上找到完整的示例:http://developer.android.com/training/location/display-address.html

答案 2 :(得分:1)

您可以使用android GeoCoder执行此操作。使用GoogleMaps v2,您可以use longpress获得LatLng点。通过获取这些坐标并将它们传递给GeoCoder,您可以“反向地理编码”以查找地址。

相关问题