簡體   English   中英

在Google地圖上獲取標記的坐標

[英]Getting the coordinates of a marker on Google Maps

我正在嘗試將Google地圖上兩個標記的緯度和經度傳遞到某些文本框。 我遵循了一些建議使用的指南:

google.maps.event.addListener(marker, 'dragend', function (event) {
document.getElementById("latbox").value = this.getPosition().lat();
document.getElementById("lngbox").value = this.getPosition().lng();
});

var lat = marker.getPosition().lat();
var lng = marker.getPosition().lng();

但是,我的文本框沒有填充任何東西。

谷歌地圖代碼(模糊了密鑰,但是因為我可以訪問地圖,所以可以正常工作):

<div class="content">
<div class="map" id="map"></div>
<script>
function initMap() {
var cpe = {lat: -20.269401 , lng: 57.497840};
var pl = {lat: -20.187042 , lng: 57.498377};
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 11,
center: cpe
});
var marker = new google.maps.Marker({
position: cpe,
map:map,
draggable:true,
label: "P"


});

ar infowindow = new google.maps.InfoWindow({
content: "Pickup Point"
});

marker.addListener('mouseover', function() {
infowindow.open(map, marker);
});

var marker2 = new google.maps.Marker({
position: pl,
map:map,
draggable:true,
icon: 'http://maps.google.com/mapfiles/ms/icons/green-dot.png'

});


var infowindow2 = new google.maps.InfoWindow({
content: "Drop-Off Point"
});



marker2.addListener('mouseover', function() {
infowindow2.open(map, marker2);
});
}

google.maps.event.addListener(marker, 'dragend', function (event) {
document.getElementById("fromlat").value = this.getPosition().lat();
document.getElementById("fromlng").value = this.getPosition().lng();
});

google.maps.event.addListener(marker2, 'dragend', function (event) {
document.getElementById("tolat").value = this.getPosition().lat();
document.getElementById("tolng").value = this.getPosition().lng();
});
</script>
<script async defer
src="https://maps.googleapis.com/maps/api/js?key=*****&callback=initMap">
</script>

</div>

文字框:

enter code here
<div class="row">
<div class="col-sm-3">
<fieldset>
<input type="text" id="fromlat" name="fromlat" placeholder="Pickup Latitutde(Auto)"/>
</fieldset>
<fieldset>
<input type="text" id="fromlng" name="fromlng" placeholder="Pickup Longitude(Auto)"/>
</fieldset>
</div>
<div class="col-sm-3">
<fieldset>
<input type="text" id="tolat" name="tolat" placeholder="Destination Latitude(Auto)"/>
</fieldset>
<fieldset>
<input type="text" id="tolng" name="tolng" placeholder="Destination Longitude(Auto)"/>
</fieldset>
</div>

您只需要在initMap()函數下定義事件函數,下面是腳本代碼。 請檢查

<script>
var map;
function initMap() {
var cpe = {lat: -20.269401 , lng: 57.497840};
var pl = {lat: -20.187042 , lng: 57.498377};
map = new google.maps.Map(document.getElementById('map'), {
zoom: 11,
center: cpe
});
var marker = new google.maps.Marker({
position: cpe,
map:map,
draggable:true,
label: "P"


});
var infowindow = new google.maps.InfoWindow({
content: "Pickup Point"
});

marker.addListener('mouseover', function() {
infowindow.open(map, marker);
});

var marker2 = new google.maps.Marker({
position: pl,
map:map,
draggable:true,
icon: 'http://maps.google.com/mapfiles/ms/icons/green-dot.png'

});


var infowindow2 = new google.maps.InfoWindow({
content: "Drop-Off Point"
});



marker2.addListener('mouseover', function() {
infowindow2.open(map, marker2);
});

google.maps.event.addDomListener(marker, 'dragend', function (event) {
document.getElementById("fromlat").value = this.getPosition().lat();
document.getElementById("fromlng").value = this.getPosition().lng();
});

google.maps.event.addDomListener(marker2, 'drag', function (event) {
document.getElementById("tolat").value = this.getPosition().lat();
document.getElementById("tolng").value = this.getPosition().lng();
});


}
</script>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM