0

我已经构建了一个地理编码器来放大到 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);
}
4

1 回答 1

0

我不得不将标记函数重写为一个单独的函数:故障出现在以下部分:markerLayer.markers = new Array();

这就是现在对我有用的方法:

var tbGeocoder = {
markers: null,
features: new Array(),

addMarker: function (lat, lng) {
    // Check of er al een marker is
    for (i = 0; i < tbMap.map.layers.length; i++) {
        if (tbMap.map.layers[i].CLASS_NAME == "OpenLayers.Layer.Markers") {
            tbMap.map.layers[i].clearMarkers();
        }
    }

    // Feature aanmaken
    var feature = new OpenLayers.Feature(this.markers, new OpenLayers.LonLat(lat, lng));
    var marker = feature.createMarker();

    // Markerlayer toevoegen en de marker toevoegen
    tbMap.map.addLayer(myLayers.markers);
    var layer = tbMap.map.layers.length - 1;
    tbMap.map.layers[layer].addMarker(marker);
    this.features.push(feature);
},
codeAddressLocation: function (address) {
    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 = '';

            // Haal de boundingbox op
            bbox = data[0].boundingbox;

            // Zet een marker in het midden van de map
            tbGeocoder.addMarker(tbMap.map.getCenter().lon, tbMap.map.getCenter().lat);


            // Maak een boundingbox voor de zoom
            var point = myLib.transform(bbox[0], bbox[2]);
            var point2 = myLib.transform(bbox[1], bbox[3]);

            // Zet de punten in een array
            site_points.push(point);
            site_points.push(point2);

            // Linear ring maken om een in een box te zoomen
            var linear_ring = new OpenLayers.Geometry.LinearRing(site_points);
            tbMap.map.zoomToExtent(linear_ring.getBounds());
        }
    });
}
};
于 2012-06-12T13:05:10.730 回答