0

我正在使用 yahoo 的 placemaker 从文本中提取位置名称。从这里我得到一个回调函数,它给了我 2 种不同类型的数组。

这是正在使用的代码,但我无法使用任何我想要的值。

  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
        }
    }]
})
4

1 回答 1

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
}

或者如果你只想要第一个纬度:

console.log(o.match[0].place.centroid.latitude); //will show only first lat
于 2012-08-13T12:02:44.423 回答