如何从谷歌地方详细信息结果中获取纬度和经度值

时间:2013-08-28 10:12:20

标签: android google-places-api

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

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值。

提前致谢。

2 个答案:

答案 0 :(得分:0)

在你的回复中,你得到拉特隆

"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/获取完整代码

答案 1 :(得分:0)

您将响应解析为以下情况

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