谷歌地图API:如何显示行车方向?

时间:2013-10-05 02:53:56

标签: 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 个答案:

答案 0 :(得分:1)

哦,小伙子,这充满了错误! : - )

  1. 您设置代码的方式,myLatlng需要全局范围,否则在函数calcRoute中它不可用。
  2. request.destination:new google.maps.LatLng(digiLocation.lat,digiLocation.long)
  3. 您永远不会设置directionsDisplay对象:
    • directionsDisplay = new google.maps.DirectionsRenderer();
    • directionsDisplay.setMap(地图);
  4. calcRoute应该包含以下行:directionsDisplay.setDirections(response);
  5. 见工作小提琴: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);