简体   繁体   English

在Javascript中读取Json并格式化为GeoJSON

[英]Read Json and format as GeoJSON in Javascript

I'm reading a JSON file and formatting for GeoJSON 我正在阅读JSON文件并格式化GeoJSON

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();
      }
    );
}    

Output:The header type featureCollection repeats. 输出:标题类型featureCollection重复。 I need to create the header before and after only go adding features. 我需要在添加功能之前和之后创建标题。 Does anyone have an example of how I can do this in JavaScript? 有没有人有一个如何在JavaScript中执行此操作的示例?

    {  
       "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
             }
          }
       ]
    },
  ........

Create your object outside of the iterator and add the header first: 在迭代器之外创建对象并首先添加标题:

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

Then for adding new features to the object, make it so your getAddress function returns an object like this on success: 然后,为了向对象添加新功能,请使getAddress函数在成功时返回如下对象:

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

Then run that instead, pushing what it returns into objSuccess. 然后运行它,将其返回的内容推送到objSuccess。

Edit: This has been solved, but just clarifying to other users that I meant push the return value of the function into objSuccess["features"]. 编辑:这已经解决,但只是向其他用户说明我的意思是将函数的返回值推入objSuccess [“features”]。 My apologies for any confusion 我为任何困惑道歉

Hope this helps. 希望这可以帮助。 :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM