答案 0 :(得分:3)
我写了一个这样做的项目,它使用了Google Maps API的第2版。以下是制作可拖动标记的代码片段,在将其拖动到地图上的新位置后将显示lat / long:
function createMarker(latlng, number, html) {
var marker = new GMarker(latlng, {draggable: true});
marker.value = number;
var myHtml = html;
var center = marker.getLatLng();
map.openInfoWindowHtml(latlng, "<font color=black>" + myHtml + "<br>" + center.toString() + "</font>");
GEvent.addListener(marker, "dragstart", function() {
map.closeInfoWindow();
});
GEvent.addListener(marker, "dragend", function() {
var center = marker.getLatLng();
marker.openInfoWindowHtml("<font color=black>" + myHtml + "<br>" + center.toString() + "</font>");
});
return marker;
}
答案 1 :(得分:2)
使用javascript和google maps API尝试这样的事情:
// update element with latest lat and lon
function updateMarkerPosition(latLng) {
document.getElementById('ll').value = [
latLng.lat(),
latLng.lng()
].join(', ');
}
function initialize() {
// look for any lat lon in url, to set map center
if ($.urlParam('ll')) {
var llparam = $.urlParam('ll').split(', ');
var lat = parseFloat(llparam[0]);
var lon = parseFloat(llparam[1]);
var latLng = new google.maps.LatLng(lat, lon);
} else {
var latLng = new google.maps.LatLng(47.65130629733119, -122.34994607543945);
}
var map = new google.maps.Map(document.getElementById('mapCanvas'), {
zoom: 12,
center: latLng,
mapTypeId: google.maps.MapTypeId.ROADMAP
});
var marker = new google.maps.Marker({
position: latLng,
map: map,
draggable: true
});
// Update current position info.
updateMarkerPosition(latLng);
// Add dragging event listeners.
google.maps.event.addListener(marker, 'drag', function() {
updateMarkerPosition(marker.getPosition());
});
}
答案 2 :(得分:0)
您可以访问标记.latLng()以获取任何标记的坐标。