我仅添加了Google Maps Script,因为它是唯一不起作用的部分。
我的问题是我不明白为什么地图看不到地理位置(我的位置)。 Visual Studio Code不显示任何错误和地图加载,但没有地理位置。不幸的是,我也找不到问题。
<script>
var myStyles =[
{
featureType: "poi",
elementType: "labels",
stylers: [
{ visibility: "off" }
]
}
];
function initMap() {
var artavenue = new google.maps.LatLng(59.441970, 24.784039),
myOptions = {
clickableIcons: false,
center: {lat:59.4370, lng:24.7536},
zoom: 12,
styles: myStyles,
disableDefaultUI: true,
},
map = new google.maps.Map(document.getElementById('map-canvas'), myOptions),
infoWindow = new google.maps.InfoWindow;
markerA = new google.maps.Marker({
position: artavenue,
title: "Art Avenue 🍽️ 🍣 🍔 🥗 🍝 🍲",
icon:'art-avenue.png',
map: map
});
markerA.addListener('click', function(e) {
map.setCenter(this.position);
$(".modal-header .modal-title").text(this.title);
$(".modal-body #modalLatLng").text(this.position);
$('#myModal').modal('show');
});
markerA = new google.maps.Marker({
position: pointB,
title: "Marker A",
label: "A",
map: map
});
markerA.addListener('click', function(e) {
map.setCenter(this.position);
$(".modal-header .modal-title").text(this.title);
$(".modal-body #modalLatLng").text(this.position);
$('#myModal').modal('show');
});
}
我认为这里出了点问题。
// Try HTML5 geolocation.
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
var pos = {
lat: position.coords.latitude,
lng: position.coords.longitude,
};
var marker = new google.maps.Marker({
position: pos,
map: map,
title: 'Your position',
});
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);
}
这是因为我在本地主机上运行页面,这要归功于Google Chrome检查元素控制台,我看到的文本是,即使您在本地主机中,它们也不会显示您的位置。所以你去了。页面上线后,一切正常。