2014-06-24 41 views
-1

我得到的谷歌地圖api在像'obj'的var下面的JSON響應。當我提醒obj.name時,我得到了「岩石上的煎餅」。如果我需要圖標,我會這樣寫 - obj.icon。但我怎樣才能得到經緯度的單獨價值。當我提醒obj.geometry.location我得到 - (-33.87054,151.198815)但我需要他們分開不是這樣的。有什麼建議麼。 謝謝大家。如何從接收的JSON中分別獲取經緯度值?

{ 
"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); 

} 
+0

只是'obj.geometry.location.lat'和'obj.geometry.location。 lng' ??? – Bergi

+0

nope :(它給出: 函數(){ 「use strict」; return this [a]} – user3500427

+5

然後你的'obj'不是你發佈的plain(parsed)JSON響應,請告訴我們整個代碼 – Bergi

回答

0

使用obj.geometry.location.lat()obj.geometry.lng(),它們是功能。

您沒有實際使用您已發佈的JSON,您使用的是google.maps.places服務,它返回google.maps.LatLng objects