其他分享
首页 > 其他分享> > 如何在谷歌地图android v2的地图上获取城市名称ontap?

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

作者:互联网

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

解决方法:

如果您有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

标签:android-maps-v2,android,google-maps,map,coordinates
来源: https://codeday.me/bug/20190831/1777100.html