在for循环中创建GeoJSON

时间:2013-02-05 11:00:29

标签: javascript flickr mapbox

我正在尝试创建geoJson对象,用于在mapbox.js和flickr-api的地图上显示flickr图像。

我无法以某种方式获得正确的格式,各种引号都缺失,任何人都可以帮忙吗?我已经发布了我需要在下面创建的循环和JSON类型。

var data = [];

window.jsonFlickrApi = function(rsp) {

  var photos = rsp.photos.photo;
  for (var i = 0; i < photos.length; i++) {
    var p = photos[i];
    var url = [ 'http://farm', p.farm, '.static.flickr.com/', p.server, '/', p.id, '_', p.secret, '_s.jpg' ].join('');    

    data.push({"geometry":{
                    "type": "Point", 
                    "coordinates": [p.latitude, p.longitude]},
                "properties":{
                    "url": url, 
                    "image": url, 
                    "name":p.title, 
                    "description": "something"}
                });
  }
}

console.debug(data);

以下是我想要创建的JSON类型:

{ "type": "feature collection",

"features":[
{
    "geometry": { "type": "Point", "coordinates": [5.123699,52.071039]},
    "properties": {
        "image": "https://lh4.googleusercontent.com/-YDA7Borc-K8/S2vU_zk9GKI/AAAAAAAAAQ0/4rn5myADmdE/s912/pavello.jpg",
        "url": "http://timcastelijn.nl",
        "name": "pavello",
        "description": "paviljoentje voor hergebruik",
     }

}, {

    "geometry": { "type": "Point", "coordinates": [5.117997,52.085776]},
    "properties": {
      "image": "http://farm9.staticflickr.com/8363/8376204495_61d75a7f10.jpg",
      "url": "http://timcastelijn.nl",
      "name": "hout en bank",
      "description": "houten bedframe en puntgave tweezitter",
     }

}, {
"geometry": { "type": "Point", "coordinates": [5.08, 52.08]},
    "properties": {    
      "image": "http://1.bp.blogspot.com/_aq9-5vIIz5s/TNbBtAJLaJI/AAAAAAAAANs/92_6emkfm8s/s1600/hout.jpg",
      "url": "http://timcastelijn.nl",
      "name": "boomstammen",
      "description": "30 stammetjes van ca 1m lang",
     }
}, {
    "geometry": { "type": "Point", "coordinates": [5.12, 52.11]},    
    "properties": {
      "image": "http://www.deouderust.nl/wp-content/uploads/2011/09/kast-%E2%82%AC2200-.jpg",
      "url": "http://timcastelijn.nl",
      "name": "oude kast",
      "description": "kast, uit hardhout onderdelen",
    }
  }]
}

1 个答案:

答案 0 :(得分:0)

至于这一位:"coordinates": [p.latitude, p.longitude] - GeoJSON坐标是经度,纬度顺序 - 反之亦然。