2016-06-15 4 views
4

에서 GeoJSON와 같은 형식으로 내가 JSON 파일을 읽고 GeoJSON읽기 JSON과 자바 스크립트

function getAddress(id,search,vagas, next) { 
    geo.geocode({address:search}, function (results,status) 
     { 
     if (status == google.maps.GeocoderStatus.OK) { 
      $scope.go = false;    
      var latlng = results[0].geometry.location; 
      var objSuccess = 
      { 
      "type": "FeatureCollection", 
      "features": [ 
       { 
        "type": "Feature", 
        "geometry": { 
         "type": "Point", 
         "coordinates": [ 
          latlng.lng(), latlng.lat() 
         ] 
        },       
        "properties": { 
         "id": id, 
         "endereco" : search, 
         "vagas" : vagas 
        } 
       } 
       ]  
      };  

      $scope.success.push(objSuccess); 
      console.log(objSuccess); 
      $scope.geocodes=true;    
      $scope.$apply();    
     } 
     else { 
      // === se houver over_query_limit error dá o delay e segue do próximo address 
      if (status == google.maps.GeocoderStatus.OVER_QUERY_LIMIT) { 
      nextAddress--; 
      console.log("over_query_limit:"+nextAddress); 
      delay++; 
      } else { 
      //alert("Error:"+status); 
      var objError= { 
       Id:id, 
       Endereco: search, 
       Erro: status 
      } 
      $scope.error.push(objError);    
      if ($scope.error.length >1){ 
       $scope.errors=true;    
      }  
      $scope.$apply();    
      } 
     } 
     next(); 
     } 
    ); 
}  

출력 서식하고 있습니다 : 헤더 유형 featureCollection 반복. 전후에 머리말을 만들 필요가 있습니다. 누구든지 자바 스크립트에서이 작업을 수행 할 수있는 방법을 보여주는 예제가 있습니까? 그렇게 당신의 getAddress에 기능이 성공이 같은 개체를 반환하기 위해선, 객체에 새로운 기능을 추가하기 위해 다음

var objSuccess = {} 
var objSuccess["type"] = "FeatureCollection" 
var objSuccess["features"] = [] 

:

{ 
     "type":"FeatureCollection", 
     "features":[ 
      { 
      "type":"Feature", 
      "geometry":{ 
       "type":"Point", 
       "coordinates":[ 
        -51.21582910000001, 
        -30.0331466 
       ] 
      }, 
      "properties":{ 
       "id":3, 
       "endereco":"Av. Osvaldo Aranha, n° 632", 
       "vagas":18 
      } 
      } 
     ] 
    }, 
    { 
     "type":"FeatureCollection", 
     "features":[ 
      { 
      "type":"Feature", 
      "geometry":{ 
       "type":"Point", 
       "coordinates":[ 
        -51.2141699, 
        -30.0338833 
       ] 
      }, 
      "properties":{ 
       "id":4, 
       "endereco":"Av. Osvaldo Aranha, n° 806", 
       "vagas":17 
      } 
      } 
     ] 
    }, 
    { 
     "type":"FeatureCollection", 
     "features":[ 
      { 
      "type":"Feature", 
      "geometry":{ 
       "type":"Point", 
       "coordinates":[ 
        -51.21328779999999, 
        -30.0343426 
       ] 
      }, 
      "properties":{ 
       "id":5, 
       "endereco":"Av. Osvaldo Aranha, n° 908", 
       "vagas":14 
      } 
      } 
     ] 
    }, 
    { 
     "type":"FeatureCollection", 
     "features":[ 
      { 
      "type":"Feature", 
      "geometry":{ 
       "type":"Point", 
       "coordinates":[ 
        -51.212449600000014, 
        -30.0348684 
       ] 
      }, 
      "properties":{ 
       "id":6, 
       "endereco":"Av. Osvaldo Aranha, n° 1004", 
       "vagas":12 
      } 
      } 
     ] 
    }, 
    { 
     "type":"FeatureCollection", 
     "features":[ 
      { 
      "type":"Feature", 
      "geometry":{ 
       "type":"Point", 
       "coordinates":[ 
        -51.21169850000001, 
        -30.0352397 
       ] 
      }, 
      "properties":{ 
       "id":7, 
       "endereco":"Av. Osvaldo Aranha, n° 1092", 
       "vagas":17 
      } 
      } 
     ] 
    }, 
    ........ 

답변

2

먼저 헤더를 반복자의 외부에서 객체를 생성 및 추가 :

  { 
       "type": "Feature", 
       "geometry": { 
        "type": "Point", 
        "coordinates": [ 
         latlng.lng(), latlng.lat() 
        ] 
       },       
       "properties": { 
        "id": id, 
        "endereco" : search, 
        "vagas" : vagas 
       } 
      } 

그런 다음 대신 objSuccess로 반환하는 항목을 실행하십시오.

편집 :이 문제는 해결되었지만 다른 사용자에게이 함수의 반환 값을 objSuccess [ "features"]로 푸시하는 것이 었음을 분명히합니다. 어떤 혼란에 대해서도 사과드립니다.

희망이 있습니다. :)

관련 문제