我使用mapquest MQA.Nominatim模块对我的java脚本代码中的地址进行地理编码。
我想将其他信息传递给回调函数MQA.Nominatim.constructPOI
这些信息应根据每个地理位置而有所不同
我使用javascript闭包来封装每个回调函数中的数据(这在Google地理编码API中可以正常工作)。
MQA.Nominatim始终调用最后设置的回调函数。在猜测MQA.Nominatim.constructPOI是一个如何全局地址指针
所以我最终得到了地图上所有位置的所有标志和附加信息
任何想法如何解决这个问题?
我的代码如下:
<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];
/*Override the custom POI generation*/
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);
/*Override the custom text populating the InfoWindow content*/
p.setRolloverContent('<div style="font-size:14px;">' + title + '</div>');
p.setInfoContentHTML('<div style="font-size:14px; width:180px;">' + url + '</div>');
return p;
};
// define a function wich does the actual work of the callback and a func
function setMarker (location) {
MQA.Nominatim.constructPOI = function (osmLocation) {
var callbackLoc = location;
return locationsCallback(callbackLoc, osmLocation);
};
/*Executes a Nominatim search and adds the result to the map*/
map.nominatimSearchAndAddLocation(location[0], null);
}
function setMarkers () {
for (var i = 0; i < addressArr.length; i++) {
var thisLoc = addressArr[i];
// alert("Iteration: " + i + " Geocode: " + location);
/*This uses the MQA.withModule support to download and initialize the Nominatim support module. The constructPOI
method is called to override the default behavior of constructing a POI.*/
setMarker(thisLoc);
}
}
function initialize() {
/*Create an object for options*/
var options = {
elt : document.getElementById('map'), /*ID of element on the page where you want the map added*/
zoom : 11, /*initial zoom level of map*/
latLng: { lat:50.202250, lng:10.079730 }, /*center of the map is Bad Kissingen*/
mtype:'osm', /*map type (osm)*/
bestFitMargin: 0, /*margin offset from the map viewport when applying a bestfit on shapes*/
zoomOnDoubleClick: true /*zoom in when double-clicking on map*/
};
/*Construct an instance of MQA.TileMap with the options object*/
window.map = new MQA.TileMap(options);
/*This uses the MQA.withModule support to download and initialize the Nominatim support module. The constructPOI
method is called to override the default behavior of constructing a POI.*/
MQA.withModule('nominatim', setMarkers);
};
MQA.EventUtil.observe(window, 'load', initialize);
</script>
</head>
<body>
<div id='map' style='width:100%; height:750px;'></div>
</body>