2012-08-13 138 views
0

我正在使用yahoo的placemaker從文本中提取位置名稱。從這我得到一個回調函數,它給了我2種不同類型的數組。從不同數組中獲取值javascript

這是我正在使用的代碼,但我能夠滿足任何我想要的值。

Placemaker.getPlaces(text,function(o){ 
    if (typeof o.match!=='undefined' && o.match.length==1){ 
    latitude=o.match.place.centroid.latitude, longitude=o.match.place.centroid.longitude; 
    console.log(latitude,longitude);} 

    if(typeof o.match !=='undefined'&& o.match.length==2){ 
    latitude=o[match].place.centroid.latitude, 
    longitude=o[match].place.centroid.longitude; 
    console.log(latitude,longitude); 
     } 

第一陣列看起來像這樣

({ 
    match: { 
     place: { 
      woeId: "29007292", 
      type: "Town", 
      name: "Jubila, West Bengal, IN", 
      centroid: { 
       latitude: "23.1626", 
       longitude: "87.7889" 
      } 
     }, 
     reference: [{ 
      woeIds: "29007292", 
      placeReferenceId: "2", 
      placeIds: "1", 
      start: "14", 
      end: "20", 
      isPlaintextMarker: "1", 
      text: "jubila", 
      type: "plaintext", 
      xpath: null 
     }, { 
      woeIds: "29007292", 
      placeReferenceId: "3", 
      placeIds: "1", 
      start: "82", 
      end: "88", 
      isPlaintextMarker: "1", 
      text: "jubila", 
      type: "plaintext", 
      xpath: null 
     }] 
    } 
}) 

,另一個這樣

({ 
    match: [{ 
     place: { 
      woeId: "23424950", 
      type: "Country", 
      name: "Spain", 
      centroid: { 
       latitude: "39.8949", 
       longitude: "-2.98831" 
      } 
     }, 
     reference: { 
      woeIds: "23424950", 
      placeReferenceId: "1", 
      placeIds: "1", 
      start: "64", 
      end: "70", 
      isPlaintextMarker: "1", 
      text: "Espa\xF1a", 
      type: "plaintext", 
      xpath: null 
     } 
    }, { 
     place: { 
      woeId: "24865675", 
      type: "Continent", 
      name: "Europe", 
      centroid: { 
       latitude: "52.9762", 
       longitude: "7.85784" 
      } 
     }, 
     reference: { 
      woeIds: "24865675", 
      placeReferenceId: "2", 
      placeIds: "2", 
      start: "93", 
      end: "99", 
      isPlaintextMarker: "1", 
      text: "Europa", 
      type: "plaintext", 
      xpath: null 
     } 
    }] 
}) 

回答

0

變化

if (typeof o.match!=='undefined' && o.match.length==1){ 

if (typeof o.match!=='undefined'){ 

對於第二:

for(var i in o.match) { 
console.log(o.match[i].place.centroid.latitude); //wil show all lat's in loop 
} 

,或者如果你只想先lattitude:

console.log(o.match[0].place.centroid.latitude); //will show only first lat 
+0

是目前第一個是工作..你能告訴我第二陣列,以及因爲這是最棘手的? – 2012-08-13 12:07:36

+0

它給了我這個錯誤:TypeError:o.match.place未定義 – 2012-08-13 12:17:23

+0

看到添加的代碼,它應該是o.match [0] .place如果你想得到單一的格位,我測試過,它對我來說工作得很好 – 2012-08-13 12:18:47

相關問題