0

我对谷歌地图的 API 有疑问。我必须向我的站点添加一张地图,该地图显示用户的位置以及到另一个前缀点的路线。

1)我不能给出请求的“来源”,即地理定​​位中的变量 pos。

2)我不能用 coorect 距离自动缩放来查看两个标记。

这是我的代码:

var directionsService = new google.maps.DirectionsService();
var directionsDisplay = new google.maps.DirectionsRenderer();

var request = {
    travelMode: google.maps.DirectionsTravelMode.DRIVING
  };

//Initializing map
var initialize = function() {

  // Taking the address of school
  var address = document.getElementById('address').firstChild.nodeValue;
  var city = document.getElementById("city").firstChild.nodeValue;
  var comun = document.getElementById("comun").firstChild.nodeValue;
  var search = address + " " + city + " " + comun; 

  // Initializing the geocoder and searcing the address in search
  var geocoder = new google.maps.Geocoder();
  geocoder.geocode( {'address': search}, function(results,status) {
    if (status == google.maps.GeocoderStatus.OK) {
      var marker2 = new google.maps.Marker({ 
        position: results[0].geometry.location,
        map: map,
        title: 'Posizione scuola'
      });
    } else {
      alert("Problema nella ricerca dell'indirizzo: " + status);
    }
  });

  // Input the visualization options
  var options = { 
    zoom: 12,
    mapTypeId: google.maps.MapTypeId.ROADMAP,
    disableDefaultUI: true
  };

  // Create the map
  var map = new google.maps.Map(document.getElementById('maps'), options);

  // Try HTML5 geolocation
  if(navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(function(position) {
      var pos = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
      map.setCenter(pos);
      request.origin = (map.center);
      // Put the marker
      var marker = new google.maps.Marker({ 
        position: pos,
        map: map,
        title: 'Tua posizione'
      });
    });
  } else {
    alert("Il tuo dispositivo non permette di visualizzare la tua posizione");
  } 

  directionsDisplay.setMap(map);
  calcRoute();
} // End initialize

var calcRoute =function() {

  // Taking the address of school
  var address = document.getElementById('address').firstChild.nodeValue;
  var city = document.getElementById("city").firstChild.nodeValue;
  var comun = document.getElementById("comun").firstChild.nodeValue;
  var search = address + " " + city + " " + comun; 

  request.destination = search;

  directionsService.route(request, function(response, status) {
    if (status == google.maps.DirectionsStatus.OK) {
      directionsDisplay.setDirections(response);
    }
  });
}

window.onload = initialize;

谢谢您的帮助

4

1 回答 1

0

1.)您必须将调用移动calcRoute到成功回调中getCurrentPosition(地理定位是一个异步过程,当前request.origin调用时尚未设置calcRoute

if(navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(function(position) {
      var pos = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
      map.setCenter(pos);
      request.origin = (pos);
      // Put the marker
      var marker = new google.maps.Marker({ 
        position: pos,
        map: map,
        title: 'Tua posizione'
      });
      directionsDisplay.setMap(map);
      calcRoute();
    });
  } else {
    alert("Il tuo dispositivo non permette di visualizzare la tua posizione");
  } 
}

2.) 将在您修复 1.) 后工作

于 2013-07-22T11:17:01.753 回答