I am developing a website with bing map. I've almost done it, but i want to have the pin clustering functionalitty. Then i got this code from the internet:
var data = [
{ "latitude": 59.441193, "longitude": 24.729494 },
{ "latitude": 59.432365, "longitude": 24.742992 },
{ "latitude": 59.431602, "longitude": 24.757563 },
{ "latitude": 59.437843, "longitude": 24.765759 },
{ "latitude": 59.439644, "longitude": 24.779041 },
{ "latitude": 59.434776, "longitude": 24.756681 }
];
I can get the latitude and longitude of every pushpin on my map, but i don't know how to add the latitude and longitude on this array. Can anyone help me?
asked Sep 5, 2013 at 3:36
Dustine Tolete
4711 gold badge8 silver badges20 bronze badges
2 Answers 2
data.push({"latitude": lat, "longitude": lng});
answered Sep 5, 2013 at 3:38
David Byttow
2712 silver badges5 bronze badges
Sign up to request clarification or add additional context in comments.
1 Comment
Dustine Tolete
am i right to declare the data array first as var data = [] ?
Another way is using the spread operator.
let data = [
{ "latitude": 59.441193, "longitude": 24.729494 },
{ "latitude": 59.432365, "longitude": 24.742992 },
{ "latitude": 59.431602, "longitude": 24.757563 },
{ "latitude": 59.437843, "longitude": 24.765759 },
{ "latitude": 59.439644, "longitude": 24.779041 },
{ "latitude": 59.434776, "longitude": 24.756681 }
];
data = [...data, {"latitude": lat, "longitude": lng}]
answered Sep 21, 2021 at 16:31
Siddique Thanikad
3432 silver badges9 bronze badges
1 Comment
Andrey
@SiddiqueThanikad Sorry, that was me. I downvoted because neither the question nor the answer said this was a context where mutating arrays is an issue (like React or VueJS). Outside of that context, adding elements to an array in this way is very unidiomatic. If someone used this without knowing what the difference is, it might lead to unintended consequences.
lang-js