gpt4 book ai didi

javascript - 如何在信息窗口中显示我的地理编码地址?谷歌地图 API

转载 作者:行者123 更新时间:2023-11-30 13:24:59 24 4
gpt4 key购买 nike

我正在使用地理定位通过查找经度和纬度并将结果返回到我的页面以在我的数据库中使用来获取我的 google map API 上的地点地址。

我无法在 map 的信息窗口中显示这些结果。

我如何使用下面的代码将地址包含在我的信息窗口中。它当前使用以下代码显示经度和纬度:

infowindow.setContent('<div><strong>' + marker.getPosition()+ '</strong><br>'); 
infowindow.open(map, marker);

geocodePosition 函数查找坐标的地址并将其附加到我的表单中的 html 输入。

我如何调整它以更新信息窗口。我现在尝试了很多方法都没有成功。有什么想法吗??

<script type="text/javascript">
var geocoder = new google.maps.Geocoder();

function geocodePosition(pos) {
geocoder.geocode({
latLng: pos
}, function(responses) {
if (responses && responses.length > 0) {
updateMarkerAddress(responses[0].formatted_address);

} else {
updateMarkerAddress('Cannot determine address at this location.');
}
});
}

function updateMarkerStatus(str) {
document.getElementById('markerStatus').value = str;
}

function updateMarkerPosition(latLng) {
document.getElementById('info').value = [
latLng.lat(),
latLng.lng()
].join(', ');
}

function updateMarkerAddress(str) {
document.getElementById('address').value = str;
}


function initialize() {

var latLng = new google.maps.LatLng(-34.397, 150.644);
var map = new google.maps.Map(document.getElementById('mapCanvas'), {
zoom: 8,
center: latLng,
mapTypeId: google.maps.MapTypeId.ROADMAP
});


var marker = new google.maps.Marker({
position: latLng,
title: 'Property location marker',
map: map,
draggable: true
});

var input = document.getElementById('searchTextField');
var autocomplete = new google.maps.places.Autocomplete(input);
var infowindow = new google.maps.InfoWindow();

autocomplete.bindTo('bounds', map);

google.maps.event.addListener(autocomplete, 'place_changed', function() {
var place = autocomplete.getPlace();
if (place.geometry.viewport) {
map.fitBounds(place.geometry.viewport);
} else {
map.setCenter(place.geometry.location);
map.setZoom(17);
}
var image = new google.maps.MarkerImage(
place.icon, new google.maps.Size(71, 71),
new google.maps.Point(0, 0), new google.maps.Point(17, 34),
new google.maps.Size(35, 35));
marker.setIcon(image);
marker.setPosition(place.geometry.location);

infowindow.setContent('<div><strong>' + marker.getPosition()+ '</strong><br>');
infowindow.open(map, marker);

});

// Update current position info.
updateMarkerPosition(latLng);
geocodePosition(latLng);

infowindow.setContent('<div><strong>' + marker.getPosition()+ '</strong><br>');
infowindow.open(map, marker);

// Add dragging event listeners.
google.maps.event.addListener(marker, 'dragstart', function() {
updateMarkerAddress('Dragging...');
});

google.maps.event.addListener(marker, 'drag', function() {
updateMarkerStatus('Dragging...');
updateMarkerPosition(marker.getPosition());
});

google.maps.event.addListener(marker, 'dragend', function() {
updateMarkerStatus('Drag ended');
geocodePosition(marker.getPosition());

//Show property address in window
infowindow.setContent('<div><strong>' + marker.getPosition()+ '</strong><br>');
infowindow.open(map, marker);
});
}

// Onload handler to fire off the app.
google.maps.event.addDomListener(window, 'load', initialize);
</script>

最佳答案

marker.getPosition() 不会提供街道地址。鉴于您当前的代码,您最好的选择是移动:

infowindow.setContent('<div><strong>' + marker.getPosition()+ '</strong><br>');

到您的 geocodePosition() 函数,然后将 infowindow 对象作为第二个参数传递给该函数。

因此,在您的 initialize() 函数中,您可以通过包含第二个参数来修改对 geocodePosition 的调用:

function initialize() {

...

geocodePosition(marker.getPosition(), infowindow);

}

然后在您的 geocodePosition() 函数中,您将接受第二个参数,并使用 responses[0].formatted_address 值 更新它。我添加了变量,以便更轻松地传递地址和消息的值。

function geocodePosition(pos, infowindow) {
geocoder.geocode({
latLng: pos
}, function(responses) {
if (responses && responses.length > 0) {
var address = responses[0].formatted_address;
updateMarkerAddress(address);
infowindow.setContent('<div><strong>' + address + '</strong><br>');

} else {
var msg = 'Cannot determine address at this location.';
updateMarkerAddress(msg);
infowindow.setContent('<div><strong>' + msg + '</strong><br>');
}
});
}

关于javascript - 如何在信息窗口中显示我的地理编码地址?谷歌地图 API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8813084/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com