我通过以下方式使用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。

如何访问这两个值?

最佳答案

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

关于javascript - 您如何解析此Google Maps JSON,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37910326/

10-14 19:47
查看更多