【发布时间】:2014-06-24 10:15:02
【问题描述】:
我在 'obj' 之类的 var 中得到低于来自 google map api 的 JSON 响应。当我提醒 obj.name 时,我得到了“Pancakes on the Rocks”。如果我需要图标,我会这样写 - obj.icon。但是我怎样才能获得纬度/经度的单独值。当我提醒 obj.geometry.location 时,我得到了 - (-33.87054,151.198815) 但我需要单独使用它们,而不是像这样。有什么建议么。 谢谢大家。
{
"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);
}
【问题讨论】:
-
只是
obj.geometry.location.lat和obj.geometry.location.lng??? -
不 :( 它给出 : function (){ "use strict"; return this[a]}
-
那么您的
obj不是您发布的普通(解析)JSON 响应。请向我们展示整个代码(edit您的问题) -
我不是说你应该发布整个 JSON 文件,而是发布它的 JavaScript 代码 以及你放置警报的位置!
-
要调试此类问题,
alert()不是最佳选择。最好使用console.log(obj.geometry)并在浏览器中打开 JavaScript 控制台。然后,您可以单击控制台输出并在树形视图中检查对象,这会提示您出了什么问题。
标签: javascript json google-maps