gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:13:47 25 4
gpt4 key购买 nike

我正在读取一个 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();
}
);
}

输出: header 类型 featureCollection 重复。我只需要在添加功能之前和之后创建标题。有没有人提供我如何在 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
}
}
]
},
........

最佳答案

在迭代器之外创建对象并首先添加 header :

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

然后为了向对象添加新功能,使您的 getAddress 函数在成功时返回如下对象:

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

然后改为运行它,将它返回的内容推送到 objSuccess。

编辑:这已经解决,但只是向其他用户说明我的意思是将函数的返回值推送到 objSuccess["features"] 中。对于任何混淆,我深表歉意

希望这对您有所帮助。 :)

关于javascript - 在 Javascript 中读取 Json 并格式化为 GeoJSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37837729/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com