当没有问到这个问题时,OpenLayers会在中心放置另一个标记

时间:2012-06-11 10:19:34

标签: firebug openlayers center markers

我已经构建了一个地理编码器,可以放大到nominatim的边界框指定的位置。此外,它在该点的中心放置一个标记,以便人们可以看到街道的确切位置。

问题是当您在平移地图后放大或缩小另一个标记时,它会在中心再次创建另一个标记,标记未在firebug中定义,它仍然表示只有一个标记,这就是在最后一个地理编码器搜索位置更正。

我还记录了地理编码器功能,看是否在放大/缩小时调用它,而不是。

这是地理编码器功能:

 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);
}

1 个答案:

答案 0 :(得分:0)

我不得不将标记函数重写为一个单独的函数: 这个故障是过去的:标记Layer.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());
        }
    });
}
};