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]); });
}
});
}
Google Maps doesn't reroute often but does present alerts as traffic conditions change during transit. For example, when I was driving on a major highway the other day, I received a pop-up notification for an upcoming traffic jam. These notifications can be dismissed with a simple tap.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With