你如何解析这个谷歌地图JSON

时间:2016-06-19 18:13:57

标签: javascript json

我通过以下方式使用Google商家信息:

https://ubilabs.github.io/geocomplete/

var geocomp =  $("#geocomplete").geocomplete({types:['establishment', 'geocode']}); 

我找回了以下JSON:

{
    "address_components": [
        {
            "long_name": "Empire State Building",
            "short_name": "Empire State Building",
            "types": [
                "point_of_interest",
                "establishment"
            ]
        },
        {

        },
        …{

        }
    ],
    "formatted_address": "Empire State Building, 350 5th Ave, New York, NY 10118, USA",
    "geometry": {
        "location": {
            "lat": 40.7484405,
            "lng": -73.98566440000002
        },
        "location_type": "APPROXIMATE",
        "viewport": {
            "south": 40.7470915197085,
            "west": -73.9870133802915,
            "north": 40.7497894802915,
            "east": -73.98431541970848
        }
    },
    "place_id": "ChIJaXQRs6lZwokRY6EFpJnhNNE",
    "types": [
        "point_of_interest",
        "establishment"
    ]
}

它返回以下回调中的JSON数据:

$("#geocomplete")
  .bind("geocode:result", function(event, result){
}

我可以按如下方式访问address_components数组的元素:

result.address_components[0].long_name
result.address_components[1].long_name
result.address_components[2].long_name

如何在location.geometry中访问“lat”和“lng”?

我试过

result.geometry.location.lat        
result.geometry.location.lng

并说lat包含:

function (){return a}

和lng包含:

function (){return b}

这对我来说毫无意义,在开头就包含了JSON。

我如何访问这两个值?

2 个答案:

答案 0 :(得分:2)

我认为location是一个google.maps.LatLng对象。无论如何,您可以使用result.geometry.location.lat()result.geometry.location.lng()访问坐标。

答案 1 :(得分:0)

以下是您的需求: 我的例子展示了如何获取特定地址的数据,但我认为它会起作用。

<强>类:

 public class GoogleGeoCodeResponse
    {

        public string status { get; set; }
        public results[] results { get; set; }
        public string error_message { get; set; }
        public string next_page_token { get; set; }

    }

    public class results
    {
        public string name { get; set; }
        public string formatted_address { get; set; }
        public geometry geometry { get; set; }
        public string[] types { get; set; }
        public address_component[] address_components { get; set; }
        public string vicinity { get; set; }
        public string place_id { get; set; }
        public string international_phone_number { get; set; }
    }

    public class geometry
    {
        public string location_type { get; set; }
        public location location { get; set; }
    }

    public class location
    {
        public string lat { get; set; }
        public string lng { get; set; }
    }

    public class GooglePalceDetail
    {
        public results result { get; set; }
        public string status { get; set; }
        public string error_message { get; set; }

    }

    public class address_component
    {
        public string long_name { get; set; }
        public string short_name { get; set; }
        public string[] types { get; set; }
    }

<强>控制器:

var address = string.Format("https://maps.googleapis.com/maps/api/geocode/json?address={0}&key={1}", currentAddress, googleKey);

 string json = client.DownloadString(address);
 var gglGeo = JsonConvert.DeserializeObject<GoogleGeoCodeResponse>(json);

if ((gglGeo.status == "ZERO_RESULTS")){
 ViewBag.msg="no results";
 return View()
}
 var lat = gglGeo.results[0].geometry.location.lat;
 var lng = gglGeo.results[0].geometry.location.lng;