我正在使用NPM Geocoder将位置转换为纬度和经度。但是我不知道如何从输出拉出lat和long。
JSON在下面,我想让第59行和第60行的内容成为一个新变量。我该怎么做呢?
这不起作用:
var location = data.results.geometry.location.lat;
_ JSON:
{
"results": [
{
"address_components": [
{
"long_name": "Kendall Park",
"short_name": "Kendall Park",
"types": [
"neighborhood",
"political"
]
},
{
"long_name": "South Brunswick Township",
"short_name": "South Brunswick Township",
"types": [
"locality",
"political"
]
},
{
"long_name": "Middlesex County",
"short_name": "Middlesex County",
"types": [
"administrative_area_level_2",
"political"
]
},
{
"long_name": "New Jersey",
"short_name": "NJ",
"types": [
"administrative_area_level_1",
"political"
]
},
{
"long_name": "United States",
"short_name": "US",
"types": [
"country",
"political"
]
}
],
"formatted_address": "Kendall Park, South Brunswick Township, NJ, USA",
"geometry": {
"bounds": {
"northeast": {
"lat": 40.4335151,
"lng": -74.54167489999999
},
"southwest": {
"lat": 40.3944258,
"lng": -74.586016
}
},
"location": {
"lat": 40.4209391, //MAKE THIS A NEW VARIABLE
"lng": -74.560711
},
"location_type": "APPROXIMATE",
"viewport": {
"northeast": {
"lat": 40.4335151,
"lng": -74.54167489999999
},
"southwest": {
"lat": 40.3944258,
"lng": -74.586016
}
}
},
"place_id": "ChIJqbniAsTCw4kRUI0mXQpaGAc",
"types": [
"neighborhood",
"political"
]
}
],
"status": "OK"
}
答案 0 :(得分:1)
data.results是一个数组,试试这个:
var location = BuildLatLng(data.results[0].geometry.location.lat,
data.results[0].geometry.location.lng);
其中BuildLatLng()
只是您需要在框架中使用的内容,相当于Google Maps JavaScript API中的google.maps.LatLng
。
当你在这里时,想想你是否真的想要总是拿出第一个结果。理想情况下,您的应用应查看每个results[i].types
以决定使用哪个。