谷歌的指示不起作用

时间:2013-07-19 18:02:55

标签: javascript google-maps google-maps-api-3 directions

我尝试为当前位置和标记坐标做出指示。所以我使用getcurrentposition得到纬度和经度。然后,我标记它。

function initialize() {
  directionsDisplay = new google.maps.DirectionsRenderer();
  var node = new google.maps.LatLng(-7.276442,112.791174);
  var mapOptions = {
    zoom: 15,
    mapTypeId: google.maps.MapTypeId.ROADMAP
  };
  map = new google.maps.Map(document.getElementById('map'),
      mapOptions);
  directionsDisplay.setMap(map);

    navigator.geolocation.getCurrentPosition(function(position) {
      pos = new google.maps.LatLng(position.coords.latitude,
                                       position.coords.longitude);

      var marker = new google.maps.Marker({
            position : pos,
            map: map,
            title:'Lokasi Anda'
      });
      var marker1 = new google.maps.Marker({
            position : node,
            map: map,
            title:'Lokasi Anda'
      });

      map.setCenter(pos);
    }, function() {
      handleNoGeolocation(true);
    });

}

然后,我用这个函数计算它。

function calcRoute() {

  var request = {
      origin:pos,
      destination:node,
      travelMode: google.maps.DirectionsTravelMode.DRIVING
  };

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

但为什么我看不到posnode的方向。有什么建议吗?

1 个答案:

答案 0 :(得分:1)

如果我调用calcRoute函数(并传入“pos”和“node”位置,则无法找到我(在美国)和该点所在位置之间的路线(Raya Its,Sukolilo, Surabaya City,East Java 60117,Indonesia of Indonesia。DirectionsService返回状态"ZERO_RESULTS"

  

ZERO_RESULTS 在原点和目的地之间找不到路线。

working example

function calcRoute(pos,node) {
var directionsService = new google.maps.DirectionsService();
  var request = {
      origin:pos,
      destination:node,
      travelMode: google.maps.DirectionsTravelMode.DRIVING
  };

  directionsService.route(request, function(response, status) {
    if (status == google.maps.DirectionsStatus.OK) {
      directionsDisplay.setDirections(response);
    } else { alert("Directions failed: "+status); }
  });
}