Google maps directions API redirecting to map.google.com

I have used a bit of JavaScript to get directions on my Google Map, but for some reason I keep getting redirected away to the Google maps page.

The problem is when I try to get walking directions mainly, although it also happens with public transport option.

This is an app working within PhoneGap, although I'm not sure this is an issue with PhoneGap.

Has anybody seen this issue before, or can anyone see an issue with my implementation?

    // get drections
directions: function (method) {

    // set directions method
    method = google.maps.DirectionsTravelMode.WALKING;
    if (method === 'public')
        method = google.maps.DirectionsTravelMode.TRANSIT;

    // current position
    var currentPos = new google.maps.LatLng(app.positionCords.latitude, app.positionCords.longitude);

    // set the directions options
    var request = {
        origin: currentPos,
        destination: app.venueCords,
        travelMode: method
    };

    // get the directions
    app.directionsService.route(request, function (response, status) {

        if (status == google.maps.DirectionsStatus.OK) {

            // set up directions on the map
            app.directionsDisplay.setMap(document.getElementById('map'));
            app.directionsDisplay.setPanel(document.getElementById('directionsPanel'));
            app.directionsDisplay.setDirections(response);

        } else {
            app.messageBox.alert(app.alertTitle, app.noDirectionsMsg, function(button) { console.warn('alert', [this, arguments]); });
        }

    });

}

I am not able to understand totally, but you may be using wrong method 'app.directionDisplay.setMap'.

As I have gone through the code.

I was not able to find where you have initialize the MAP and then you may assign directions to the MAP

      function initialize() {
        var mapOptions = {
          center: new google.maps.LatLng(-34.397, 150.644),//put your initial position values
          zoom: 8,
          mapTypeId: google.maps.MapTypeId.ROADMAP
        };
        var map = new google.maps.Map(document.getElementById("map"),
            mapOptions);
      }

then using different function you can initialize route/directions.

Please refer for more details and example GOOGLE Documentation

I have doubts with your code starting with

    // set up directions on the map
    app.directionsDisplay.setMap(document.getElementById('map'));

Please correct that..

I hope this will do

链接地址: http://www.djcxy.com/p/70864.html

上一篇: B中是否有任何批量加载算法?

下一篇: Google将方向API重定向到map.google.com