我有一个问题,我在点击地图时定义了一个事件,我在点击的位置移动我的标记。它可以工作,但当我想移动我的地图(通过点击/抓取),它也移动我的标记(声音合法,但我不希望这样)。
这里是移动标记的事件代码:
// On créé un nouvel event click afin de déplacer le marker déjà placé sur la map
map.events.register("click", map , function(e){
var opx = map.getLayerPxFromViewPortPx(e.xy);
marker.map = map;
marker.moveTo(opx);
}
如何在不移动我的标记的情况下抓住我的地图(保持在相同的纬度/经度位置)?
编辑: 这里有完整的功能代码,如果有帮助的话:
function initPageCarto() {
//niveau de zoom
zoom=12;
var scale= new OpenLayers.Control.ScaleLine({ topInUnits: 'km', bottomInUnits: 'm'});
// gestion de la carto
var options = {
projection: new OpenLayers.Projection("EPSG:900913"),
displayProjection: new OpenLayers.Projection("EPSG:4326"),
units: "m",
maxResolution: 156543.0339,
numZoomLevels: 18,
controls: [
new OpenLayers.Control.Navigation(),
scale]
};
map = new OpenLayers.Map("grandecarte",options);
var newL = new OpenLayers.Layer.OSM("Default", "serveurDeTuile", {numZoomLevels: 19});
map.addLayer(newL);
// récupération du polygone
var format = new OpenLayers.Format.WKT({
'internalProjection': map.baseLayer.projection,
'externalProjection': new OpenLayers.Projection("EPSG:4326")
});
var polygonFeature= format.read("<?php echo $polygone; ?>");
// Centre du polygone
var center=polygonFeature.geometry.getCentroid();
// Ajout de la couche vignette de localite
renderer = OpenLayers.Util.getParameters(window.location.href).renderer;
renderer = (renderer) ? [renderer] : OpenLayers.Layer.Vector.prototype.renderers;
// Creation du vectorlayer avec style
vectorLayer = new OpenLayers.Layer.Vector("Simple Geometry", {
styleMap: new OpenLayers.StyleMap({
fillColor: "#3C8DBC",
fillOpacity: 0.1,
strokeColor: "#3C8DBC",
strokeOpacity: 1
}),
renderers: renderer
});
// Ajout du polygone
vectorLayer.addFeatures([polygonFeature]);
map.addLayer(vectorLayer);
markers = new OpenLayers.Layer.Markers( "Markers" );
markers.id = "Markers";
map.addLayer(markers);
map.events.register("click", map, function(e) {
// On récupére la position afin de placer un marker
var position = map.getLonLatFromPixel(e.xy);
var posi = map.getLonLatFromPixel(e.xy).transform(new OpenLayers.Projection("EPSG:900913"), new OpenLayers.Projection("EPSG:4326"));
var newlon = posi.lon;
var newlat = posi.lat;
$.ajax({
url: "http://nominatim.openstreetmap.org/reverse?lon="+newlon+"&lat="+newlat,
dataType: "jsonp",
jsonp : "callback",
jsonpCallback: "jsonpCallback",
data: {
format:"json",
json_callback: 'jsonpCallback'
},
success: function ( data ) {
if(data.address) {
document.getElementById('searchbox_from_location').value = "";
document.getElementById('adresse').value = "";
if (data.address.house_number) {
document.getElementById('searchbox_from_location').value += data.address.house_number;
document.getElementById('adresse').value += data.address.house_number;
}
if (data.address.road) {
if(document.getElementById('searchbox_from_location').value != "") {
document.getElementById('searchbox_from_location').value += " ";
document.getElementById('adresse').value += " ";
}
document.getElementById('searchbox_from_location').value += data.address.road;
document.getElementById('adresse').value += data.address.road;
}
if (data.address.postcode) {
if(document.getElementById('searchbox_from_location').value != "") {
document.getElementById('searchbox_from_location').value += " ";
}
document.getElementById('searchbox_from_location').value += data.address.postcode;
document.getElementById('cp').value = data.address.postcode;
}
if (data.address.city) {
if (document.getElementById('searchbox_from_location').value != "") {
document.getElementById('searchbox_from_location').value += " ";
}
document.getElementById('searchbox_from_location').value += data.address.city;
document.getElementById('ville').value = data.address.city;
}
}
else {
document.getElementById('searchbox_from_location').value = data.display_name;
}
document.getElementById('lat').value = data.lat;
document.getElementById('lon').value = data.lon;
document.getElementById('bbox').value = data.boundingbox;
},
});
var size = new OpenLayers.Size(30,38);
var offset = new OpenLayers.Pixel(-(size.w/2), -size.h);
var icon = new OpenLayers.Icon("http://dokuwikispatial.sourceforge.net/dokuwiki/lib/plugins/openlayersmap/icons/marker-blue.png", size, offset);
marker = new OpenLayers.Marker(position,icon) ;
markers.addMarker(marker);
// On supprime l'event de click sur la map pour création d'un nouveau marker
map.events.remove('click');
// On créé un nouvel event click afin de déplacer le marker déjà placé sur la map
map.events.register("click", map , function(e){
var opx = map.getLayerPxFromViewPortPx(e.xy);
marker.map = map;
marker.moveTo(opx);
var posi = map.getLonLatFromPixel(e.xy).transform(new OpenLayers.Projection("EPSG:900913"), new OpenLayers.Projection("EPSG:4326"));
var newlon = posi.lon;
var newlat = posi.lat;
$.ajax({
url: "http://nominatim.openstreetmap.org/reverse?lon="+newlon+"&lat="+newlat,
dataType: "jsonp",
jsonp : "callback",
jsonpCallback: "jsonpCallback",
data: {
format:"json",
json_callback: 'jsonpCallback'
},
success: function ( data ) {
if(data.address) {
document.getElementById('searchbox_from_location').value = "";
document.getElementById('adresse').value = "";
if (data.address.house_number) {
document.getElementById('searchbox_from_location').value += data.address.house_number;
document.getElementById('adresse').value += data.address.house_number;
}
if (data.address.road) {
if (document.getElementById('searchbox_from_location').value != "") {
document.getElementById('searchbox_from_location').value += " ";
document.getElementById('adresse').value += " ";
}
document.getElementById('searchbox_from_location').value += data.address.road;
document.getElementById('adresse').value += data.address.road;
}
if (data.address.postcode) {
if (document.getElementById('searchbox_from_location').value != "") {
document.getElementById('searchbox_from_location').value += " ";
}
document.getElementById('searchbox_from_location').value += data.address.postcode;
document.getElementById('cp').value = data.address.postcode;
}
if (data.address.city) {
if (document.getElementById('searchbox_from_location').value != "") {
document.getElementById('searchbox_from_location').value += " ";
}
document.getElementById('searchbox_from_location').value += data.address.city;
document.getElementById('ville').value = data.address.city;
}
}
else {
document.getElementById('searchbox_from_location').value = data.display_name;
}
document.getElementById('lat').value = data.lat;
document.getElementById('lon').value = data.lon;
document.getElementById('bbox').value = data.boundingbox;
},
});
});
});
// On construit la bounding box en fonction des min / max du polygone
map.zoomToExtent(new OpenLayers.Bounds(<?php echo $min_x; ?>,<?php echo $min_y; ?>,<?php echo $max_x; ?>,<?php echo $max_y; ?>).transform("EPSG:4326", "EPSG:900913"));
}