【问题标题】:Google directions don't work谷歌路线不起作用
【发布时间】:2013-07-19 03:02:54
【问题描述】:

我尝试为我当前的位置和标记坐标制定方向。所以我使用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 的方向。有什么建议吗?

【问题讨论】:

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


    【解决方案1】:

    如果我调用 calcRoute 函数(并传入“pos”和“node”位置,它无法找到我所在的位置(在美国)和该点所在的位置(Raya Its、Sukolilo、 Surabaya City, East Java 60117, Republic 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); }
      });
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-30
      • 1970-01-01
      • 2020-05-02
      • 1970-01-01
      • 2015-09-28
      相关资源
      最近更新 更多