2012-08-12 88 views
0

我想從數組中存儲緯度和經度,其長度有時是1,有時是2或3。無法從數組中獲取經度和緯度

下面是代碼:

var latitude,longitude; 
    $.each(data.results, function (i, item) { 
    var name = item.from_user; 
    var img = item.profile_image_url; 
    var text=item.text; 
    var profile_img=item.profile_image_url; 
    var url=(item.entities.urls.length > 0 ? item.entities.urls[0].url : ''); 
    Placemaker.getPlaces(text,function(o){ 
    console.log(o); 
    if (typeof(o.match!=='undefined') ||(o.length==1)){ 
    latitude=o.match.place.centroid.latitude, longitude=o.match.place.centroid.longitude; 
    console.log(latitude,longitude);} 
    else if (typeof(o.match[0]!=='undefined') ||(o.match.length==2)){ 
     latitude=o.match[0].place.centroid.latitude, longitude=o.match[0].place.centroid.longitude; 
    console.log(latitude,longitude); 
    } 

    }); 
    array.push({name:name,img:img,text:text,latitude:latitude,longitude:longitude,profile_img:profile_img,url:url}); 

從這個代碼我只得到緯度&經度值當陣列長度爲1。因此,只有第一,如果被執行。我正在修復其他數組,以便我可以從其他數組中獲取值,因爲當我使用console.log輸出數組時,緯度爲&的經度不存在。我猜測循環必須在將它們放入數組之前完成所有的值。

我試圖通過tweets位置輸出谷歌地圖上的推文。我沒有使用地理編碼,但雅虎placemaker從推文的文本地理位置。

這裏是placemaker陣列結構http://www.flickr.com/photos/codepo8/3553188917/

這是想獲得值對象{匹配= [2]}

回答

0

typeof(o.match!=='undefined')始終是「布爾」 我覺得其他陣列的快照你想 typeof(o.match)!=='undefined'或只是typeof o.match!=='undefined'

+0

那不是一個問題,想從誰擁有它內部的兩個對象一個這樣的數組獲取值---->對象{匹配= [2]} – 2012-08-12 19:10:26

+0

這是什麼原因第一個'如果'一直在它真的 – stark 2012-08-12 19:41:47

+0

它仍然不能正常工作 – 2012-08-12 19:59:59