简体   繁体   English

谷歌地图 API 在我将 Modal eventListener 添加到标记后停止显示地理位置

[英]Google maps API stoped showing geolocation after I added Modal eventListener to marker

I added only Google Maps Script because it is the only part that does not work.我只添加了谷歌地图脚本,因为它是唯一不起作用的部分。

My issue is that I don't understand why the map does not see geolocation (my location).我的问题是我不明白为什么 map 看不到地理位置(我的位置)。 Visual Studio Code does not show any error and map loads but no geolocation. Visual Studio Code 没有显示任何错误,map 加载但没有地理位置。 Unfortunately, I can't find the issue either.不幸的是,我也找不到问题。

<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');
});
}

I think that something goes wrong from here.我认为这里出了点问题。

// 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);
}

It was because I was running page in localhost, thanks to Google Chrome inspect element console I saw the text that now they are not showing your location even if you are in the localhost.这是因为我在 localhost 中运行页面,多亏了 Google Chrome 检查元素控制台,我看到了现在即使您在 localhost 中它们也没有显示您的位置的文本。 So there you go.所以你有 go。 When the page is live everything works.当页面上线时,一切正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM