谷歌地图api,方向从2点

时间:2013-07-22 06:15:08

标签: javascript google-maps-api-3

我的谷歌地图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;

感谢您的帮助

1 个答案:

答案 0 :(得分:0)

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

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");
  } 
}
当你修复1时,

2。)将起作用。)