【问题标题】:How to get lat and longtitude values out from google places details result如何从谷歌地方详细信息结果中获取纬度和经度值
【发布时间】:2013-08-28 10:12:20
【问题描述】:

我已使用以下代码成功返回地点详细信息的结果:

private PlaceDetails getPlaceDetails(String url) throws Exception{


        String jsonResults =null;
        HttpHelper helper = new HttpHelper();
        try {
            jsonResults = helper.GetJSonString(url);
            // Create a JSON object hierarchy from the results

             JSONObject jsonObj = new JSONObject(jsonResults.toString()).getJSONObject("result");

             placeDetails = new PlaceDetails();
             placeDetails.setName(jsonObj.getString("name"));
             placeDetails.setFormatted_address(jsonObj.getString("formatted_address"));
             placeDetails.setInternational_phone_number(jsonObj.getString("international_phone_number"));



        } catch (JSONException e) {
            Log.e("LOG_TAG", "Error processing JSON results " + e.getMessage().toString(), e);
        }

        return placeDetails;
    }

现在,我的问题是我不知道如何从结果中获取 lat 和 lng 值。

提前致谢。

【问题讨论】:

标签: android google-places-api


【解决方案1】:

在您的回复中,您会得到 lat lon

"geometry" : {
     "location" : {
       "lat" : -33.8669710,
       "lng" : 151.1958750
     }

你只需要像这样使用 JsonArray 解析数据

jsonObj.getJsonArray("几何");

完整代码可以参考这个链接http://yuvislm.wordpress.com/2012/09/10/google-places-api-and-json-parsing-in-android/

【讨论】:

  • 我用过这段代码 JSONObject jsonObj1 = new JSONObject(jsonResults.toString()); String lat = ((JSONArray)jsonObj1.get("result")).getJSONObject(0).getJSONObject("geometry") .getJSONObject("location").getString("lat");但它仍然无法正常工作。
【解决方案2】:

你有如下解析响应

JSONObject jsonObj = new JSONObject(jsonResults.toString());
JSONObject result =jsonObj.getJSONObject("result").getJSONObject("geometry").getJSONObject("location");
Double longitude  = result.getDouble("lng");
Double latitude =  result.getDouble("lat");

你会得到类似的回应

"geometry" : {
     "location" : {
       "lat" : -33.8669710,
       "lng" : 151.1958750
     }

查看回复详情

https://developers.google.com/places/web-service/details#PlaceDetailsResults

【讨论】:

    猜你喜欢
    • 2016-08-29
    • 2018-06-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多