2017-01-27 2 views
0

내가 가지고 MongoDB의 데이터를 가져옵니다 다음 코드를 사용하여 JSON 배열로 내 mapbox의 GL 응용 프로그램에 전달하는 RESTapi :GeoJSON FeatureCollection

$.ajax(
      { 
       type: "GET", 
       contentType: "application/json; charset=utf-8", 
       url: myUrl, 
       cache: false, 
       async: true, 
       timeout: 5000, 
       dataType: "json", 
       success: function (data) 
       { 
        console.log("Reading Data"); 
        console.log(data); 
       }, 
       error: function (xhr, ajaxOptions, thrownError) 
       { 
        console.log("http Status Response: " + xhr.status); 
        console.log(thrownError); 
       } 
      }); 

을 데이터는 mongo에 개별 문서로 저장되며 get은 다음 형식으로 가져옵니다.

[ 
     { 
      "_id": "588a3d5a524da321dd937891", 
      "__v": 0, 
      "geometry": { 
       "type": "Point", 
       "coordinates": [ -113.5299938027191, 53.42859997065679 ] 
      }, 
      "type": "Feature", 
      "properties": { 
       "icon": "horse-riding-15", 
       "title": "A Horse", 
       "description": "A Horse description", 
       "date": "2017-01-26T18:18:02.175Z" 
      } 
     }, 
     { 
      "_id": "588ac68aa99e6a38134997b5", 
      "__v": 0, 
      "geometry": { 
       "type": "Point", 
       "coordinates": [ -113.56076949999999, 53.4528447 ] 
      }, 
      "type": "Feature", 
      "properties": { 
       "icon": "dog-park-15", 
       "title": "A Dog", 
       "description": "A Dog description", 
       "date": "2017-01-27T04:03:22.381Z" 
      } 
     } 
] 

은 다음과 같이해야 GeoJSON 기능 컬렉션의 일부 (시작에 여분의 타입 정보를, 그리고 {} 래퍼)해야한다 mapbox이를 읽으려면 :

{ 
    "type": "FeatureCollection", 
    "features":  [ 
      { 
       "_id": "588a3d5a524da321dd937891", 
       "__v": 0, 
       "geometry": { 
        "type": "Point", 
        "coordinates": [ -113.5299938027191, 53.42859997065679 ] 
       }, 
       "type": "Feature", 
       "properties": { 
        "icon": "horse-riding-15", 
        "title": "A Horse", 
        "description": "A Horse description", 
        "date": "2017-01-26T18:18:02.175Z" 
       } 
      }, 
      { 
       "_id": "588ac68aa99e6a38134997b5", 
       "__v": 0, 
       "geometry": { 
        "type": "Point", 
        "coordinates": [ -113.56076949999999, 53.4528447 ] 
       }, 
       "type": "Feature", 
       "properties": { 
        "icon": "dog-park-15", 
        "title": "A Dog", 
        "description": "A Dog description", 
        "date": "2017-01-27T04:03:22.381Z" 
       } 
      } 
    ] 
} 

I을 선호하는 변환 방법, 실종 된 트릭 또는 다운로드 후 추가 데이터를 형식을 변경하고 추가하는 클라이언트 쪽 코드가 있는지는 확실치 않지만 추가 기능을 추가하는 가장 좋은 방법은 무엇인지 확실하지 않습니다. 랩퍼 데이터.

+1

'var에 foo는 = { "유형": "FeatureCollection", "기능"데이터}' – Andreas

답변

1

원하는 출력을 얻으려면 고유 한 개체를 만들 수 있습니다. 이 예제가 도움이 될 것입니다.

var dataReturned = [ 
 
     { 
 
      "_id": "588a3d5a524da321dd937891", 
 
      "__v": 0, 
 
      "geometry": { 
 
       "type": "Point", 
 
       "coordinates": [ -113.5299938027191, 53.42859997065679 ] 
 
      }, 
 
      "type": "Feature", 
 
      "properties": { 
 
       "icon": "horse-riding-15", 
 
       "title": "A Horse", 
 
       "description": "A Horse description", 
 
       "date": "2017-01-26T18:18:02.175Z" 
 
      } 
 
     }, 
 
     { 
 
      "_id": "588ac68aa99e6a38134997b5", 
 
      "__v": 0, 
 
      "geometry": { 
 
       "type": "Point", 
 
       "coordinates": [ -113.56076949999999, 53.4528447 ] 
 
      }, 
 
      "type": "Feature", 
 
      "properties": { 
 
       "icon": "dog-park-15", 
 
       "title": "A Dog", 
 
       "description": "A Dog description", 
 
       "date": "2017-01-27T04:03:22.381Z" 
 
      } 
 
     } 
 
]; 
 

 

 

 
var geoJSON = {}; 
 

 
geoJSON["type"] = "FeatureCollection"; 
 
geoJSON["features"] = dataReturned; 
 

 

 
console.log(JSON.stringify(geoJSON));

관련 문제