I have this type of data in an object in my project.
{
"name": "Travel",
"map": [
{
"name": "Montreal",
"lat": "45.498649",
"lng": "-73.492729"
},
{
"name": "Brossard",
"lat": "45.466667",
"lng": "-73.450000"
}
]
}
How can I create this type of structure code to show my Google Map ?
var locations = [
{
name: "Montreal",
latlng: new google.maps.LatLng(45.498649, -73.492729)
},
{
name: "Brossard",
latlng: new google.maps.LatLng(45.466667, -73.450000)
}
];
Thanks a lot.
2 Answers 2
It's more simple if you use 'forEach':
json.map.forEach(function(val){
locations.push({
name: val.name,
latlng: new google.maps.LatLng(val.lat, val.lng)
});
});
You were close:
for (i = 0; i < dmap.length; i++) {
locations.push({name: dmap[i].name, lat: dmap[i].lat,lng:dmap[i].lng});
}
Another way is doing it with Array.map:
dmap.map(function(item){
return {name: item.name, lat: item.lat, lng:item.lng};
});
Since You Editted this is the relevant code:
for (i = 0; i < dmap.length; i++) {
locations.push({name: val.name,
latlng: new google.maps.LatLng(val.lat, val.lng)
});
}
Or
dmap.map(function(item){
return {name: val.name,
latlng: new google.maps.LatLng(val.lat, val.lng)
};
});
answered Nov 22, 2014 at 13:52
Amir Popovich
30.1k9 gold badges58 silver badges102 bronze badges
3 Comments
Amir Popovich
thats an external class that probally comes with adding the google maps js file.
lang-js