I am using the mapquest MQA.Nominatim module to geocode addresses inside my java script code. I would like to pass additional information to the MQA.Nominatim.constructPOI callback function.
This information should be different for each geolocation.
I use javascript closure to encapsulate data in each callback function (this works as desired in the Google api geocoding).
MQA.Nominatim always names the last installed callback function. By assumption, MQA.Nominatim.constructPOI is some kind of pointer to a global address.
Thus, I get all the flags and additional information that looks the same for all locations on the map.
Any ideas how to solve this?
My code looks like this:
<html>
<head>
<script src="http://open.mapquestapi.com/sdk/js/v7.0.s/mqa.toolkit.js?key=xxxxxxx"></script>
<script type="text/javascript">
var flags = ['icons/flag_green.png', 'icons/flag_red.png'];
var addressArr = [
[', 49084 Osnabrueck, Germany','R','/trac/start/ticket/6677','Repair #6677'],
[', 39599 Uchtspringe, Germany','R','/trac/start/ticket/6670','Repair #6670'],
[', 17491 Greifswald, Germany','I','/trac/start/ticket/6625','Install #6625'],
[', 37269 Eschwege, Germany','R','/trac/start/ticket/6620','Repair #6620'],
[', 57076 Siegen, Germany','R','/trac/start/ticket/6602','Repair #6602'],
[', 3109 St Poelten, Germany','I','/trac/start/ticket/6598','Install #6598'],
[', 45884 Gelsenkirchen, Austria','R','/trac/start/ticket/6594','Repair #6594'],
[', 48653 Coesfeld, Germany','R','/trac/start/ticket/6588','Repair #6588']
];
function locationsCallback (location, osmLocation) {
var type = location[1];
var url = location[2];
var title = location[3];
var image = flags[0];
if ('R' == type)
image = flags[1];
var lat = osmLocation.lat;
var lng = osmLocation.lon;
var p = new MQA.Poi({
lat : lat,
lng : lng
});
var customIcon = new MQA.Icon(image, 20, 29);
p.setIcon(customIcon);
p.setRolloverContent('<div style="font-size:14px;">' + title + '</div>');
p.setInfoContentHTML('<div style="font-size:14px; width:180px;">' + url + '</div>');
return p;
};
function setMarker (location) {
MQA.Nominatim.constructPOI = function (osmLocation) {
var callbackLoc = location;
return locationsCallback(callbackLoc, osmLocation);
};
map.nominatimSearchAndAddLocation(location[0], null);
}
function setMarkers () {
for (var i = 0; i < addressArr.length; i++) {
var thisLoc = addressArr[i];
setMarker(thisLoc);
}
}
function initialize() {
var options = {
elt : document.getElementById('map'),
zoom : 11,
latLng: { lat:50.202250, lng:10.079730 },
mtype:'osm',
bestFitMargin: 0,
zoomOnDoubleClick: true
};
window.map = new MQA.TileMap(options);
MQA.withModule('nominatim', setMarkers);
};
MQA.EventUtil.observe(window, 'load', initialize);
</script>
</head>
<body>
<div id='map' style='width:100%; height:750px;'></div>
</body>