2017-04-06 5 views
0
는만큼 쉽게

:벡터 레이어 openlayers의 JSON에 대한 API에서

var restaurantsold = new ol.layer.Vector({ 
    title: 'b_layer', 
    source: new ol.source.Vector({ 
     url: 'restaurantjson.geojson', 

     format: new ol.format.GeoJSON() 
    }), 
}); 

그리고지도에 직접 해당 레이어를 추가 할 수 있습니다. 내가 API를 호출을 만들려고하지만, 내가지도에 표시 할 수없는, 나의 가장 친한 시도이있다 :

var restaurants; 
    $.ajax({ 
     type: "GET", 
     url: "http://tour-pedia.org/api/getPlaces?category=restaurant&location=Berlin&name=La+Dolce+Vita", 
     dataType:"json", 
     success:function(data){ 
      console.log(data) 
      restaurants = data; 
      $(restaurants).each(function(index, value) { 

       console.log(value.address); 
      }); 
      } 
    }); 
    var resta2 = new ol.layer.Vector({ 
     title : "Resta2", 
     source: new ol.source.Vector(restaurants) 
    }); 

을 그리고 난 아무데도 이에 대한 적절한 해결책을 찾을 수없는, 감사 너의 도움으로!

편집 : 문제는이 JSON 파일을 가져오고 openlayers는 GeoJSON 파일 ..이 다음은 GeoJSON로 변환하는 것이었다 해결하는 길 싶어했다 끝에 : https://gis.stackexchange.com/questions/73756/is-it-possible-to-convert-regular-json-to-geojson

답변

1

레스토랑을 Ajax 호출을하고 있기 때문에 벡터 레이어를 생성 할 때 데이터를 전혀 사용할 수 없을 수도 있습니다.

따라서 GeoJSON을 컬렉션으로 변환하려면 ol.format.GeoJSONreadFeatures() 메서드를 사용하십시오. 그런 다음 addFeatures() 방법을 사용하여 벡터 소스를 추가합니다.

수정 :

var vectorSource = new ol.source.Vector({ 
     format: new ol.format.GeoJSON() 
    }) 

var restaurantsold = new ol.layer.Vector({ 
    title: 'b_layer', 
    source : vectorSource 
}); 

$.ajax({ 
     type: "GET", 
     url: "http://tour-pedia.org/api/getPlaces?category=restaurant&location=Berlin&name=La+Dolce+Vita", 
     dataType:"json", 
     success:function(data){ 
      // If response is valid 
      var geojsonFormat = new ol.format.GeoJSON(); 

      // reads and converts GeoJSon to Feature Object 
      var features = geojsonFormat.readFeatures(data); 
      vectorSource.addFeatures(features); 
     } 
    }); 
+0

자네 말이 맞아! 콜백, buuut 내 시도를 완전히 잊어 버렸습니다. 당신의 솔루션이 작동하지 않습니다. (콘솔에 오류가 표시되지 않습니다. 오류를 추적하려고 할 수있는 것이 있습니까? – ImanolUr

+0

AJAX 호출에서 반환 된 데이터를 확인하십시오. 데이터는 GeoJSON 형식이어야합니다. 또는 데이터에 GeoJSON 응답이 포함 된 일부 객체가 포함되어있을 수 있습니다. –

+0

첫 번째 코드에서 결과를 표시하는 콘솔 로그가 있었으며 구조는 Array [Object, Object, Object]이고 각 객체는 이름, 위도, LNG, 이드 ... 꽤 닮았다. 제 아들. – ImanolUr