2

我尝试为我当前的位置和标记坐标制定方向。所以我使用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);
    }
  });
}

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

4

1 回答 1

1

如果我调用 calcRoute 函数(并传入“pos”和“node”位置,它无法找到我所在位置(在美国)和该点所在位置(Raya Its、Sukolilo、泗水市)之间的方向, East Java 60117, Republic of Indonesia)。DirectionsService 返回“ZERO_RESULTS”状态。

ZERO_RESULTS 在起点和终点之间找不到路线。

工作示例

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); }
  });
}
于 2013-07-19T20:27:37.347 回答