[英]Get User Address By Drag & Drop marker on Google Maps
我的網站上有一張地圖,我必須通過將標記拖放到地圖上的任何地方來獲取用戶地址。 我已成功在我的網站上顯示地圖。 但是我不知道如何在地圖上顯示該標記,並可以拖動該標記並獲得用戶添加。 我下面的地圖代碼是通過單擊地圖來獲取用戶地址。 謝謝。
<div id='map-canvas'></div>
var myLatlng = new google.maps.LatLng(latitude,longitude);
var myOptions = {
zoom: 10,
center: myLatlng
}
var map = new google.maps.Map(document.getElementById("map-canvas"), myOptions);
var geocoder = new google.maps.Geocoder();
google.maps.event.addListener(map, 'click', function(event) {
geocoder.geocode({
'latLng': event.latLng
}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
if (results[0]) {
alert(results[0].formatted_address);
}
}
});
});
您應該在拖動端添加一個標記和一個偵聽器
在此示例中,列表器顯示了拖動結束緯度,經緯度和性能地理編碼
<div id='map-canvas'></div>
var myLatlng = new google.maps.LatLng(latitude,longitude);
var myOptions = {
zoom: 10,
center: myLatlng
}
var map = new google.maps.Map(document.getElementById("map-canvas"), myOptions);
var geocoder = new google.maps.Geocoder();
var marker = new google.maps.Marker({
position: myLatlng,
map: map,
draggable:true,
title:"Drag me!"
});
marker.addListener('dragend', function(event) {
alert(event.latLng.lat() + ' ' + event.latLng.lng());
geocoder.geocode({
'latLng': event.latLng
}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
if (results[0]) {
alert(results[0].formatted_address);
}
}
});
});
google.maps.event.addListener(map, 'click', function(event) {
geocoder.geocode({
'latLng': event.latLng
}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
if (results[0]) {
alert(results[0].formatted_address);
}
}
});
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.