2013-04-18 40 views
1

I JSON節點具有相同的標題,但每個節點中的緯度和經度值不同。我需要檢查相同的標題值,然後將緯度和經度值合併到地圖API的網址中。我需要它是按照這個順序的緯度,經度,緯度,經度等......我只是不知道該怎麼做。感謝您的任何幫助或建議。將JSON值合併到變量或字符串中

JS VAR

<img class="detail_map" src="http://open.mapquestapi.com/staticmap/v4/getplacemap?size=320,240&zoom=15&location=' + data.nodes.Latitude + ',' + data.nodes.Longitude + '&imagetype=jpeg&showicon=blue-1"> 

JSON對象

var data = fl({ 
"nodes":[ 
    {"node":{ 
     "title":"180","Address":"555 Market St. San Francisco, CA United States See map: Google Maps"," 
     Latitude":"37.789952"," 
     Longitude":"-122.400158"}}, 
    {"node":{ 
     "title":"180","Address":"Epic Roasthouse (399 Embarcadero) San Francisco, CA United States See map: Google Maps"," 
     Latitude":"37.797677"," 
     Longitude":"-122.394339"}}, 
    {"node":{ 
     "title":"180","Address":"Mason &amp; California Streets (Nob Hill) San Francisco, CA United States See map: Google Maps"," 
     Latitude":"37.791556"," 
     Longitude":"-122.410766"}}, 
    {"node":{ 
     "title":"180","Address":"Justin Herman Plaza San Francisco, CA United States See map: Google Maps"," 
     Latitude":"37.774930"," 
     Longitude":"-122.419416"}}, 
    {"node":{ 
     "title":"180","Address":"200 block Market Street San Francisco, CA United States See map: Google Maps"," 
     Latitude":"37.793133"," 
     Longitude":"-122.396560"}} 
]}); 

});

+0

什麼將緯度和經度值合併到url中意味着什麼? – lucuma

+0

當'node's合併時會發生什麼?除'title'外,每個'節點'包含不同的數據。 – Joseph

+0

當節點合併時,它只是將所有信息放在同一個值中。我無法在一個價值中擁有所有的緯度和經度。我需要相互關注的緯度和經度。緯度,經度,緯度,經度。我不能有拉特,拉特,拉特,拉特或長,長,長,長 – Justin

回答

0

您可以使用$.extend()函數合併兩個對象,不同的屬性將被替換。

例子:

var newData = $.extend(data1, data2); 
// Assuming data1, data2 are objects; 

如果需要轉換JSON字符串爲對象,使用$.parseJSON()

0

你可以嘗試做這樣的事情其中var url_part用於替換「data.nodes.Latitude + '' + data.nodes.Longitude」從IMG SRC:

var url_part = ''; 
$.each(data.nodes, function(key,val){ 
    url_part += val.node.Latitude+","+val.node.Longitude+","; 
}); 

但useding url_part之前,你需要刪除的最後昏迷...

0

使用jQuery的$.map()。比較對象的node.title值,然後從那裏返回字符串,就像您期望的那樣。完成後,請加入,

var locs = $.map(fl.nodes, function(obj,i){ 
    return obj.node.title == '180' ? 'latitude='+obj.node.Latitude+'&longitude='+obj.node.Longitude : ''; 
}).join(','); 

將返回lat1,long1,lat2,long2,lat3,long3,lat4,long4,lat5,long5

Working jsFiddle

編輯1

增加對動態標題支持。

function getNodesByTitle(title){ 
    return $.map(fl.nodes, function(obj,i){ 
     return obj.node.title == title ? 'latitude='+obj.node.Latitude+'&longitude='+obj.node.Longitude : ''; 
    }).join(','); 
} 

在理論實踐:

var locString = getNodesByTitle('your title here'); 
+0

太棒了。這會起作用,但如果有更多節點具有不同的標題呢? – Justin

+0

@Justin然後讓它成爲一個函數。傳入節點標題,並將「180」更改爲任何你想要的。 – Ohgodwhy

+0

嗯是的。我已經在函數中獲得了標題,但是它只返回給我的只是逗號(,,,,,,,)。也許我需要更好地解釋我在做什麼? – Justin

0

我不得不創建一個函數來合併地址和經緯度。看看jsFiddle看到它在行動。

function fl(data){ 
//data = JSON.parse(data); 
//Array to hold movie titles 
var movieTitles = []; 
//Assign Movies to to movies Array and ensure unique 
for (var i=0; i < data.nodes.length; i++) 
{ 
    //Look for the current title in the movieTitles array 
    var movieIndex = movieTitles.indexOf(data.nodes[i].node.title); 
    if (movieIndex>-1) //If the title already exists 
    { 
     //Merge all the properties you want here 
     movies[movieIndex].Address += ", " + data.nodes[i].node.Address; 
     if(!movies[movieIndex].Coords) movies[movieIndex].Coords = []; 
     movies[movieIndex].Coords.push(
      data.nodes[i].node.Latitude + "," + data.nodes[i].node.Longitude 
     ); 
    } 
    else 
    { 
     //var address = movies[movieIndex].Address; movies[movieIndex].Address = address.replace(/^\s*/,''); 
     //Add movie to movies array 
     movies.push(data.nodes[i].node); 
     //Add movie title to movieTitles array 
     movieTitles.push(data.nodes[i].node.title); 

    } 
} 

displayLinks(); //Load all the links 
//showCast(0); //Display details for first item 

}
//});

相關問題