如何与收到的JSON分开获取lat lng值?

时间:2014-06-24 10:15:03

标签: javascript json google-maps

我在google map api中得到的JSON响应低于'obj'这样的var。当我警告obj.name时,我得到了“岩石上的煎饼”。如果我需要图标,我会这样写 - obj.icon。但是我如何得到lat / lng的单独值。当我提醒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);

}

1 个答案:

答案 0 :(得分:0)

使用obj.geometry.location.lat()obj.geometry.lng(),它们是函数。

您实际上并未使用已发布的JSON,而是使用了google.maps.places服务,该服务返回google.maps.LatLng objects