1

我想要实际的地点或建筑物名称,而不是使用 GPS 的 andoird 应用程序中的地址。

例如:如果我在购物中心,我的应用程序应该显示我在 XXXX 购物中心。

目前我正在使用纬度和经度获取地址。

如果遵循此http://www.androidhive.info/2012/07/android-gps-location-manager-tutorial/

4

1 回答 1

2

通过使用谷歌地理编码器,您将能够实现您所寻求的。它返回一个对象,其中包含有关您传递给它的位置的Address所有信息。可以在下面找到如何使用它的示例:latitudelongitude

    if (Geocoder.isPresent()) {
        Geocoder gc = new Geocoder(context);
        List<Address> addresses = gc.getFromLocation(latitude, longitude, 1);
        // do stuff with addresses
    }

如果它无法返回一个好的Address对象,你仍然可以尝试使用 url 方法:

    URL url = new URL("http://maps.googleapis.com/maps/api/geocode/json?latlng=" + String.valueOf(latitude) + "," + String.valueOf(longitude) + "&sensor=false&language=" + Locale.getDefault().getLanguage());
    HttpURLConnection connection = (HttpURLConnection)url.openConnection();

    InputStream in = connection.getInputStream();
    JSONObject responseData;
    try {
        Scanner s = new Scanner(in).useDelimiter("\\A");
        try {
            String text = s.next();
            responseData = new JSONObject(text);
        } finally {
            s.close();
        }
    } finally {
        in.close();
    }

    // Do stuff with responseData, all the usefull informations are in this payload
于 2016-02-17T07:32:35.997 回答