2013-08-18 51 views
0

我將通過AJAX解析值/座標並在Google地圖上顯示爲標記。從JSON獲取值以在Google地圖上創建標記

AJAX代碼

$.each(data.Level, function(i, Level) { 
    $.each(Level.route, function(index, route) { 
     // alert(route.lat)  
     myLatlngcc = new google.maps.LatLng(route.lat, route.lgn); 
     var markercc = new google.maps.Marker({ 
      position: myLatlngcc, 
      map: map 
     }); 
     markers.push(markercc); 
    }); 

JSON

{ 
    "Level": [ 
     { 
      "route": [ 
       { 
        "lat": 39.105251, 
        "lgn": 26.551727 
       }, 
       { 
        "lat": 39.105247125, 
        "lgn": 26.551774625 
       }... 
      ], 
      "balls": [ 
       { 
        "lat": 39.105239375, 
        "lgn": 26.551869875 
       }, 
       { 
        "lat": 39.10524325, 
        "lgn": 26.55182225 
       } 
      ] 
     }, 
     { 
      "route": [ 
       { 
        ........ 
       }, 
       { 
        ..... 
       }... 
      ], 
      "balls": [ 
       { 
        ........ 
       }, 
       { 
        ..... 
       }... 
      ] 
     } 
    ] 
} 

我想只包括陣列route[1]latlgn對象。任何想法我可以做到這一點?

回答

0

試試這個:

var json = { /* Your object */ } 
var lat = json.Level[0].route[1].lat; 
var lgn = json.Level[0].route[1].lgn; 

假設你從Level陣列中的每個元素需要latlgn,你只需要將上述的一個循環。

+0

你能解釋一下嗎?......謝謝! – user2352548

+0

@ user2352548它只是直接訪問數組。有關詳細信息,請參閱此鏈接:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array –

相關問題