dragging problem in markers in google maps
I am taking marker details from a database. The code is
app.entries[id].data = {'id': id, 'map': entry[1].innerHTML,'type_id':
entry[2].innerHTML,'name': entry[3].innerHTML,'descript': entry[4].innerHTML, 'parish':
entry[5].innerHTML,'zi开发者_如何转开发p': entry[6].innerHTML, 'url': entry[7].innerHTML, 'lng':
entry[8].innerHTML,'lat':entry[9].innerHTML,'tags':entry[10].innerHTML,'img':
entry[11].innerHTML,'imgs':entry[12].innerHTML};
app.entries[id].data.imgHTML = (app.entries[id].data.img == "") ? "" :
fill_template(app.entries[id].data,app.infoWindowImageHTML);
var str = fill_template(app.entries[id].data,app.searchRowHTML);
html.push(str);
if (app.entries[id].data.lat != 0 || app.entries[id].data.lng != 0) {
var point = new GLatLng(app.entries[id].data.lat,app.entries[id].data.lng);
var m = new GMarker(point,{draggable: true});
GEvent.addListener(m, "dragstart", function() {
// map.closeInfoWindow();
var latlngold = m.getLatLng();
});
GEvent.addListener(m, "dragend", function() {
var m = new GMarker(point,{draggable: true});
m.entry_id = id;
var p=m.entry_id;
var latlngnew = m.getLatLng();
alert(latlngnew);
//m.setLatLng(new GLatLng(latlng));
editentry1(latlngnew,p);
});
}
The dragend
and dragstart
function always takes the last marker's details not the details of dragging marker..
function editentry1(latlngnew,p){
var newlat=latlngnew.lat();
var newlng=latlngnew.lng();
var d = new Date();
var dv = d.valueOf();
var url = "<?php echo $g['appPath']; ?>admin/editPoint.php?d="+dv+"&
newlat="+newlat+"newlng="+newlng+"&p="+p;
var s = $('mapStatus');
s.innerHTML = "EDiting Point...";
s.show();
new Ajax.Request(url, {
method: 'get',
onSuccess: editEntryReturn,
onFailure: requestFailed
});
}
editPoint.php
require("createThumb.php");
$newlng = $_GET['newlng'];
$newlat = $_GET['newlat'];
$p=$_GET['p'];
$q = "UPDATE entry SET lat = '$newlat', lng = '$newlng' WHERE id='$p';";
$r = updateBatch($q);
} ?> but the position of the marker doesn't changes
Wrap your marker creation code in a function. This will create a closure in which the drag callbacks can find the appropriate marker object:
function createMarker (point)
{
var m = new GMarker(point,{draggable: true});
GEvent.addListener(m, "dragstart", function() {
var latlngold = m.getLatLng();
});
GEvent.addListener(m, "dragend", function() {
var latlngnew = m.getLatLng();
editentry1(latlngold,latlngnew);
});
return m;
}
Call this from inside your if statement after getting the point:
if (app.entries[id].data.lat != 0 || app.entries[id].data.lng != 0)
{
var point = new GLatLng(app.entries[id].data.lat,app.entries[id].data.lng);
map.addOverlay(createMarker (point));
}
精彩评论