编程语言
首页 > 编程语言> > javascript – Google指示不起作用

javascript – Google指示不起作用

作者:互联网

我尝试为我当前的位置和标记坐标做出指示.所以我使用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);
    }
  });
}

但为什么我看不到从pos到节点的方向.有什么建议吗?

解决方法:

如果我调用calcRoute函数(并传入“pos”和“node”位置,它就找不到我在哪里(在美国)和该点所在的位置(Raya Its,Sukolilo,Surabaya City, East Java 60117,印度尼西亚共和国.)DirectionsService返回状态“ZERO_RESULTS”.

ZERO_RESULTS No route could be found between the origin and destination.

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); }
  });
}

标签:directions,javascript,google-maps,google-maps-api-3
来源: https://codeday.me/bug/20191002/1845556.html