I have an array of javascript elements. Each element contains an address
and a unique id
.
var elements =
[
{
id: 1,
address: "address1"
},
{
id: 2,
address: "address2"
}
];
Element addresses are geocoded and resulting markers are displayed in a Google Map, as indicated in this post.
for (var i = 0; i < elements.length; i++) {
$.getJSON('http://maps.googleapis.com/maps/api/geocode/json?address='+elements[i].address+'&sensor=false', null, function (data) {
// add marker to map
console.log(data.results[0]);
});
}
Now my goal is to associate each marker with the proper request. For example, suppose that the first geocoding (id=1
) results in a marker positioned at (lat: 15.0526206, lng: 19.6929845)
. I would like to find a way to associate this marker with id=1
element.
I've tried to log data result (see snippet above), but unfortunately it doesn't help (formatted_address
is similar to original element address
but formatted differently, so string comparion is not feasible). How can I do?