1

I JSON 节点具有相同的标题,但每个节点中的纬度和经度值不同。我需要检查相同的标题值,然后将纬度和经度值合并到地图 API 的 url 中。我需要它按纬度,经度,纬度,经度等顺序排列......我只是不知道此时该做什么。感谢您的任何帮助或建议。

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"}} 
]});

});

4

4 回答 4

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

}
//});

于 2013-04-19T23:55:01.913 回答
0

您可以使用$.extend()合并两个对象的功能,不同的属性将被替换。

例子:

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

如果您需要将字符串 JSON 转换为对象,请使用$.parseJSON()

于 2013-04-18T21:19:37.177 回答
0

您可以尝试做这样的事情,其中​​ var url_part 用于替换 img src 中的“data.nodes.Latitude + ',' + data.nodes.Longitude”:

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

但在使用 url_part 之前,您需要删除最后一个昏迷...

于 2013-04-18T21:30:00.963 回答
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

工作的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');
于 2013-04-18T21:31:04.563 回答