2012-06-11 51 views
0

我已經構建了一個地理編碼器來放大nominatim的boundingbox指定的地方。此外,它在該點的中心放置一個標記,以便人們可以看到街道的確切位置。OpenLayers將另一個標記放置在中間,當不要求這個時

問題是當您在平移地圖後放大或縮小另一個標記時,它會在中心再次創建另一個標記,標記未在螢火蟲中定義,但它仍然表示只有一個標記,這是在最後的地理編碼器搜索的地方是正確的。

我還記錄了地理編碼器功能,以查看放大/縮小時是否調用它,而不是。

這裏的地理編碼器功能:

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

回答

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()); 
     } 
    }); 
} 
}; 
相關問題