简体   繁体   English

Google Maps无法在Chrome上运行

[英]Google Maps not working on Chrome

I have a function that gives the user directions from their location to a fixed location on a map. 我有一个功能,可为用户提供从他们的位置到地图上固定位置的方向。 It is being loaded in as external content to populate a main div, the map shows fine on both IE and Firefox but when I view it in Chrome it does not show. 正在将其作为外部内容加载以填充主div,该地图在IE和Firefox上均显示正常,但是当我在Chrome中查看时,该地图未显示。 The Chrome dev console shows that the content is being loaded into the div and it is not putting out any errors. Chrome开发者控制台显示内容正在加载到div中,并且没有发出任何错误。

External map.html 外部map.html

<script type="text/javascript" src="JS/location.js"></script>
<div id="mapContainer"></div>
<div id="writtenDir"></div>

JS to load content: JS加载内容:

  $('#Contact').click(function () {
    $('#centerPane').load('External/map.html');
    $.getScript("JS/location.js");
  });

Location.js Location.js

if (navigator.geolocation) { 
  navigator.geolocation.getCurrentPosition(geoInfo, noGeoInfo);
} else {
  noGeoInfo();
}

function geoInfo(position) {
  navigator.geolocation.getCurrentPosition(function (position) { 
    var latitude = position.coords.latitude;                    
    var longitude = position.coords.longitude;                 
    var coords = new google.maps.LatLng(latitude, longitude); 
    var directionsService = new google.maps.DirectionsService();
    var directionsDisplay = new google.maps.DirectionsRenderer();
    var mapOptions = {  
      zoom: 15,        
      center: coords, 
      mapTypeControl: true, 
      navigationControlOptions:
      {
        style: google.maps.NavigationControlStyle.SMALL 
      },
      mapTypeId: google.maps.MapTypeId.ROADMAP 
    };
    map = new google.maps.Map(document.getElementById("mapContainer"), mapOptions); 
    directionsDisplay.setMap(map);
    directionsDisplay.setPanel(document.getElementById(''));
    var request = {
      origin: coords, 
      destination: '54.861283, -6.326805', 
      travelMode: google.maps.DirectionsTravelMode.DRIVING 
    };
    directionsService.route(request, function (response, status) {
      if (status == google.maps.DirectionsStatus.OK) {
        directionsDisplay.setDirections(response);
      }
    });
  });
}

function noGeoInfo() { 
  var location = new google.maps.LatLng(54.861283, -6.326805); 
  var mapOptions = { 
    center: location, 
    zoom: 15, //Sets zoom level (0-21)
    mapTypeId: google.maps.MapTypeId.ROADMAP 
  };
  var map = new google.maps.Map(document.getElementById("mapContainer"), mapOptions);
  marker = new google.maps.Marker({  
    position: location, 
    map: map 
  });
}

Div to be loaded into: 将Div加载到:

<div id="centerPane" data-dojo-type="dijit/layout/ContentPane" data-dojo-props="region:'center'"></div>

Why is the map not showing in Google Chrome? 为什么地图没有显示在Google Chrome中? (live link, press "Contact Us" - http://scmweb.infj.ulst.ac.uk/~B00518833/DNA/View/index.php ) (实时链接,请按“联系我们” -http://scmweb.infj.ulst.ac.uk/~B00518833/DNA/View/index.php

The problem resides inside the Nav.js file at line 59: 问题位于第59行的Nav.js文件中:

google.maps.event.trigger(map, 'resize');

The map is undefined when this line is executed. 执行此行时, map未定义。

Ensure that your js files are loaded in the right order and that the map is always defined before triggering the resize event. 确保在触发resize事件之前按正确的顺序加载js文件,并且始终定义映射。

在此处输入图片说明

I hope this helps. 我希望这有帮助。

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

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