我已经构建了一个地理编码器来放大到 nominatim 的边界框指定它的位置。它还在该点的中心放置了一个标记,以便人们可以看到街道的确切位置。
问题是当您在平移地图后放大或缩小另一个标记时,它会在中心再次创建另一个标记,该标记未在萤火虫中定义,它仍然说只有一个标记,这是正确的最后一次地理编码器搜索的位置。
我还记录了地理编码器功能,以查看在放大/缩小时是否调用了它,而事实并非如此。
这是地理编码器功能:
codeAddressLocation: function(address) {
// street_number huisnummer
// route straatnaam
// locality plaatsnaam
// administrative_area_level_2 gemeente
// administrative_area_level_1 provincie
// country land
// postal_code_prefix postcode (4 cijfers)
if (address.length < 10)
return;
var site_points = new Array();
var lon = '';
var lat = '';
input = address.replace(" ", "+");
$.getJSON('http://nominatim.openstreetmap.org/search?q=' + input + '&format=json&addressdetails=1', function(data) {
if (data != "" ) {
var bbox = '';
$.each(data, function(key,value) {
bbox = value.boundingbox;
});
var point = myLib.transform(bbox[0], bbox[2]);
var point2 = myLib.transform(bbox[1], bbox[3]);
if (tbMap.map.layers.length > 2 && tbMap.map.layers[2].CLASS_NAME == "OpenLayers.Layer.Markers") {
var markerLayer = tbMap.map.layers[2];
markerLayer.markers = new Array();
var size = new OpenLayers.Size(21,35);
var offset = new OpenLayers.Pixel(-(size.w/2), -size.h);
var icon = new OpenLayers.Icon('http://clipartist.info/RSS/openclipart.org/2011/May/28-Saturday/squat_marker_purple-555px.png', size, offset);
markerLayer.addMarker(new OpenLayers.Marker(tbMap.map.getCenter(),icon));
} else {
var markers = new OpenLayers.Layer.Markers("Markers");
tbMap.map.addLayer(markers);
var size = new OpenLayers.Size(21,35);
var offset = new OpenLayers.Pixel(-(size.w/2), -size.h);
var icon = new OpenLayers.Icon('http://clipartist.info/RSS/openclipart.org/2011/May/28-Saturday/squat_marker_purple-555px.png', size, offset);
markers.addMarker(new OpenLayers.Marker(tbMap.map.getCenter(),icon));
}
// Zet de punten in een array
site_points.push(point);
site_points.push(point2);
// return lonlat;
var linear_ring = new OpenLayers.Geometry.LinearRing(site_points);
tbMap.map.zoomToExtent(linear_ring.getBounds());
}
});
}
它在这里被调用:
$('#autosuggest').click(function() {
// two options 1) unchecking: remove all coordinateSensitive events
// 2) checking: add coordinateSensitive events
if(!$(this).prop('checked')) {
$(".coordinateSensitive").unbind('keyup');
$(".coordinateSensitive").unbind('blur');
} else {
tbGeocoder.codeAddressLocation($("#address").val() + '+' + $("#housenr").val() + '+' + $('#city').val());
$('.coordinateSensitive').keyup( function() {
getCoordinates($(this));
});
$('.coordinateSensitive').blur( function() {
getCoordinates($(this));
});
}
});
function getCoordinates(field) {
var input = $("#address").val() + '+' + $("#housenr").val() + '+' + $('#city').val();
tbGeocoder.codeAddressLocation(input);
}