javascript - 谷歌地图 API : how to show driving direction?

标签 javascript jquery google-maps google-maps-api-2 map-directions

有没有简单的解决方案来修复路由功能

var directionDisplay;
var directionsService = new google.maps.DirectionsService();

var map;
var digiLocation = { "lat" : "51.597336" , "long" : "-0.109035" };
$(document).ready(function() {
  $("#direction-route").click(function(event){
    event.preventDefault();
    var locationStart = "turnpike lane"
    calcRoute(locationStart)
  });
  var laLatLng;
  initialize();
});
// Set pointer at the middle while resize
google.maps.event.addDomListener(window, "resize", function() {
    var center = map.getCenter();
    google.maps.event.trigger(map, "resize");
    map.setCenter(center); 
});
function calcRoute(locationStart){
  var request = {
      origin: locationStart, 
      destination:(digiLocation.lat, digiLocation.long),
      travelMode: google.maps.DirectionsTravelMode.DRIVING
  };
  directionsService.route(request, function(response, status) {
    if (status == google.maps.DirectionsStatus.OK) {
      myLatlng.setDirections(response);
    }
  });
}

function initialize() {
    var myLatlng = new google.maps.LatLng(digiLocation.lat, digiLocation.long);
    var myOptions = {
        scrollwheel: false,
        zoom: 16,
        center: myLatlng,
        mapTypeId: google.maps.MapTypeId.ROADMAP,
        styles: [ { "stylers": [{ "saturation": -100 } ]}]
    };
    map = new google.maps.Map(document.getElementById('digi-map'), myOptions);
    marker = new google.maps.Marker({
        position: myLatlng,
        icon: 'img/digi-marker.png',
        map: map
    });
}
google.maps.event.addDomListener(window, 'load', initialize);

http://jsfiddle.net/sweetmaanu/qQjeB/

最佳答案

天哪,这充满了错误! :-)

  1. 按照您设置代码的方式,myLatlng 需要全局作用域,否则在函数 calcRoute 中它不可用。
  2. request.destination:new google.maps.LatLng(digiLocation.lat, digiLocation.long)
  3. 您从未设置过 DirectionDisplay 对象:
    • directionsDisplay = new google.maps.DirectionsRenderer();
    • directionsDisplay.setMap( map );
  4. calcRoute 应包含此行:directionsDisplay.setDirections(response);

查看工作 fiddle :http://jsfiddle.net/manishie/5fGEZ/

JavaScript:

var directionDisplay;
var directionsService = new google.maps.DirectionsService();

var map;
var digiLocation = {
    "lat": "51.597336",
        "long": "-0.109035"
};
var myLatlng = new google.maps.LatLng(digiLocation.lat, digiLocation.long);

$(document).ready(function () {
    $("#direction-route").click(function (event) {
        event.preventDefault();
        var locationStart = "turnpike lane"
        calcRoute(locationStart)
    });
    var laLatLng;
    initialize();
});
// Set pointer at the middle while resize
google.maps.event.addDomListener(window, "resize", function () {
    var center = map.getCenter();
    google.maps.event.trigger(map, "resize");
    map.setCenter(center);
});

function calcRoute(locationStart) {
    var request = {
        origin: locationStart,
        destination: new google.maps.LatLng(digiLocation.lat, digiLocation.long),
        travelMode: google.maps.DirectionsTravelMode.DRIVING
    };
    directionsService.route(request, function (response, status) {
        if (status == google.maps.DirectionsStatus.OK) {
            directionsDisplay.setDirections(response);
        }
    });
}

function initialize() {
    var myOptions = {
        scrollwheel: false,
        zoom: 16,
        center: myLatlng,
        mapTypeId: google.maps.MapTypeId.ROADMAP,
        styles: [{
            "stylers": [{
                "saturation": -100
            }]
        }]
    };
    map = new google.maps.Map(document.getElementById('digi-map'), myOptions);
    marker = new google.maps.Marker({
        position: myLatlng,
        icon: 'img/digi-marker.png',
        map: map
    });
    directionsDisplay = new google.maps.DirectionsRenderer();
    directionsDisplay.setMap(map);

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

关于javascript - 谷歌地图 API : how to show driving direction?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19193471/

相关文章:

javascript - 页脚位于底部

javascript - 将字符添加到值中

javascript - 不改变 "left"属性的可调整大小的 div

php - 如何从 .JS 文件中重置一次性 PHP 表单 token 以防止表单泛滥?

google-maps - 谷歌地图滑雪道名称

javascript - 如何从 math.floor 中获取重复的数字?

javascript - 在 vue 1.x 中选择 `optgroup`

javascript - 如何使用ckeditor中的onKeyUp函数

javascript - 堆叠列每列两个值

mysql - 在 Google map 上追踪位置路径