如何在Google地图填充方向时添加加载消息

时间:2014-01-12 17:44:42

标签: javascript jquery google-maps javascript-events listener

我有以下脚本,它将从浏览器位置加载到拉斯维加斯的路线:

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
<title>Google Maps API v3 Directions Example</title>
<script type="text/javascript"
   src="http://maps.google.com/maps/api/js?sensor=false"></script>
</head>
<body style="font-family: Arial; font-size: 12px;">
<div style="width: 600px;">
 <div id="map" style="width: 280px; height: 400px; float: left;"></div>
 <div id="panel" style="width: 300px; float: right;"></div>
</div>

<script type="text/javascript">

var mylat,mylong,request;
var directionsService = new google.maps.DirectionsService();
var directionsDisplay = new google.maps.DirectionsRenderer();

var map = new google.maps.Map(document.getElementById('map'), {
   zoom:7,
   mapTypeId: google.maps.MapTypeId.ROADMAP
 });

navigator.geolocation.getCurrentPosition(GetLocation);
function GetLocation(location) {

mylat= location.coords.latitude;
mylong = location.coords.longitude;
request = {
origin: mylat+','+mylong,
destination: '35.580789,-105.210571',
   travelMode: google.maps.DirectionsTravelMode.DRIVING
 };

 directionsDisplay.setMap(map);
 directionsDisplay.setPanel(document.getElementById('panel'));
 directionsService.route(request, function(response, status) {
   if (status == google.maps.DirectionsStatus.OK) {
     directionsDisplay.setDirections(response);
   }
 });
}

</script>
</body>
</html>

这非常适合我需要的东西,但正如您所看到的,这会在页面加载时留下一些停机时间。我想添加一条消息,告诉用户在加载路线时按住。据我所知,我只需要在加载时添加一个事件监听器。以下是我尝试使用的示例:

google.maps.event.addListenerOnce(map, 'idle', function(){
document.write("<p>please hold while loading</p>");
});

我曾尝试将其添加到脚本中的几个位置,但是在停机期间没有加载,而是在页面加载后加载。有关如何在代码中调整此侦听器以便仅在停机期间显示的任何建议?

1 个答案:

答案 0 :(得分:0)

不确定但仍尝试

<script type="text/javascript">
 document.write("<p id='loader'>please hold while loading</p>");
  .....
  ......
  function GetLocation(location) {
    ............................
    if (status == google.maps.DirectionsStatus.OK) {
      directionsDisplay.setDirections(response);
      document.getElementById('loader').innerHTML = "Map loaded";
    }

  }

</script>