【问题标题】:Google Maps API: Combining Directions Service with watchPosition GeolocationGoogle Maps API:将路线服务与 watchPosition 地理定位相结合
【发布时间】:2014-10-10 19:26:00
【问题描述】:

我正在尝试使用 Google 的路线服务显示地图,其中包含起点、目的地和连接两者的蓝色路线。但是,我正在尝试将 watchPosition 用于原点 lat/lng。

到目前为止,一切都正确显示,除了绿色原点标记没有移动,因为 watchPosition 使用新的 lat/lng 坐标更新。我正在尝试做的事情可能吗?还是我只需要创建第三个跟随用户坐标的标记?

这是我的脚本中的内容:

    var directionsDisplay;
    var directionsService = new google.maps.DirectionsService();
    var map;

    function initialize() {
      directionsDisplay = new google.maps.DirectionsRenderer();
      var mapOptions = {
        zoom:7,
        center: new google.maps.LatLng(38.5815719, -121.4943996)
      }
      map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
      directionsDisplay.setMap(map);
      calcRoute();
}

function handleNoGeolocation(errorFlag) {
  if (errorFlag) {
    var content = 'Error: The Geolocation service failed.';
} else {
    var content = 'Error: Your browser doesn\'t support geolocation.';
}

  var options = {
    map: map,
    position: new google.maps.LatLng(<%= @order.origin_lat %>, <%= @order.origin_lng %>),
    content: content
  };

  var infowindow = new google.maps.InfoWindow(options);
  map.setCenter(options.position);
}



function calcRoute() {
    if(navigator.geolocation) {
        navigator.geolocation.watchPosition(function(position) {
            var pos = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
            var request = {
                  origin: pos,
                  destination: new google.maps.LatLng(<%= @order.user.latitude %>, <%= @order.user.longitude %>),
                  travelMode: google.maps.TravelMode.DRIVING
              };
            directionsService.route(request, function(response, status) {
            if (status == google.maps.DirectionsStatus.OK) {
                  directionsDisplay.setDirections(response);
                }
            });
        },

        function() {
        handleNoGeolocation(true);
        });
    } else {
    // Browser doesn't support Geolocation
        handleNoGeolocation(false);
    }

}

google.maps.event.addDomListener(window, 'load', initialize);

【问题讨论】:

    标签: javascript jquery google-maps google-maps-api-3 geolocation


    【解决方案1】:

    这在移动网络中看起来并不容易实现,所以我只创建了第三个根据用户位置移动的标记。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-07-22
      • 1970-01-01
      • 2017-01-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-13
      相关资源
      最近更新 更多