Javascript 如何从收到的JSON中分别获取lat lng值?
我在一个类似“obj”的变量中得到了来自GoogleMapAPI的JSON响应。当我提醒obj.name时,我得到了“岩石上的煎饼”。如果我需要图标,我会这样写-obj.icon。但是我怎样才能得到lat/lng的单独值呢。当我向obj.geometry.location发出警报时,我得到了-(-33.87054151.198815),但我需要它们分开,而不是像这样。任何建议。 谢谢大家Javascript 如何从收到的JSON中分别获取lat lng值?,javascript,json,google-maps,Javascript,Json,Google Maps,我在一个类似“obj”的变量中得到了来自GoogleMapAPI的JSON响应。当我提醒obj.name时,我得到了“岩石上的煎饼”。如果我需要图标,我会这样写-obj.icon。但是我怎样才能得到lat/lng的单独值呢。当我向obj.geometry.location发出警报时,我得到了-(-33.87054151.198815),但我需要它们分开,而不是像这样。任何建议。 谢谢大家 { "html_attributions" : [], "results" : [ { "ge
{
"html_attributions" : [],
"results" : [
{
"geometry" : {
"location" : {
"lat" : -33.87054,
"lng" : 151.198815
}
},
"icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/restaurant-71.png",
"id" : "c71365287e7606bd21a3311d21fda087830b7813",
"name" : "Pancakes on the Rocks",
"opening_hours" : {
"open_now" : true
},
"photos" : [
...
function createMarkers(results, status) {
if (status == google.maps.places.PlacesServiceStatus.OK) {
for (var i = 0; i < results.length; i++) {
createResultMarker(results[i]);
}
}
}
function createResultMarker(obj) {
alert('title: ' + obj.name); // Alert correct name.
alert(obj.geometry.location);
alert(obj.geometry.location.lat);
distance = getDistanceFromLatLonInKm(init_lat, init_lng, obj.geometry.location.lat, obj.geometry.location.lng);
}
{
“html_属性”:[],
“结果”:[
{
“几何学”:{
“地点”:{
“lat”:-33.87054,
“液化天然气”:151.198815
}
},
“图标”:”http://maps.gstatic.com/mapfiles/place_api/icons/restaurant-71.png",
“id”:“c71365287e7606bd21a3311d21fda087830b7813”,
“名字”:“岩石上的煎饼”,
“开放时间”:{
“立即打开”:正确
},
“照片”:[
...
函数createMarkers(结果、状态){
if(status==google.maps.places.PlacesServiceStatus.OK){
对于(var i=0;i
使用obj.geometry.location.lat()
和obj.geometry.lng()
,它们是函数
你实际上并没有使用你发布的JSON,你使用的是google.maps.places服务,它只返回obj.geometry.location.lat和obj.geometry.location.lng??不:(它给出:函数(){“使用严格”;返回这个[a]}那么你的
obj
就不是普通的(解析的)您发布的JSON响应。请向我们展示整个代码(您的问题)。我没有说您应该发布整个JSON文件,但是要发布加载它的JavaScript代码以及您将警报放在哪里!要调试此类问题,alert()
不是最佳选项。最好使用console.log(obj.geometry)
并在浏览器中打开javascript控制台。然后,您可以单击控制台输出并在树状视图中检查对象,这将为您提供错误提示。