【发布时间】:2020-01-20 14:16:13
【问题描述】:
我遵循了 Google https://developers.google.com/maps/documentation/javascript/geolocation 的教程,因此我可以在地图上显示用户的位置。它目前看起来像这样,带有一个信息窗口:
但是我想显示一个地理位置标记而不是信息窗口:
这是我的代码:
function initialize() {
var mapOptions = {
center: new google.maps.LatLng(52.520008, 13.404954)
};
infoWindow = new google.maps.InfoWindow;
map = new google.maps.Map(document.getElementById("map"), mapOptions);
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
var pos = {
lat: position.coords.latitude,
lng: position.coords.longitude
};
infoWindow.setPosition(pos);
infoWindow.setContent('Location found.');
infoWindow.open(map);
map.setCenter(pos);
}, function() {
handleLocationError(true, infoWindow, map.getCenter());
});
} else {
// Browser doesn't support Geolocation
handleLocationError(false, infoWindow, map.getCenter());
}
}
function handleLocationError(browserHasGeolocation, infoWindow, pos) {
infoWindow.setPosition(pos);
infoWindow.setContent(browserHasGeolocation ?
'Error: The Geolocation service failed.' :
'Error: Your browser doesn\'t support geolocation.');
infoWindow.open(map);
}
任何有关如何将其从信息窗口更改为地理位置标记的帮助都会很棒。提前致谢。
【问题讨论】:
-
您需要在
getCurrentPosition回调中创建一个标记。 API 中没有现成的地理位置标记。您可以创建一个custom Marker。如果您还打算获取设备方向,则需要自己实现它。 This 可能会有所帮助。