Click here to Skip to main content
15,885,767 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
Hi!

I have been given the task to upgrade googlemaps version 2 to version 3.

I have been reading up on googlemaps here: https://developers.google.com/maps/documentation/javascript/examples/[^]

And most of it is rather helpfull and contains most of the needed answers one needs to upgrade to version 3.

Sadly I have a problem which I have not been able to find a soloution to.

Issue:
I have a map that displays directions from a startpoint of the users chosing which goes through waypoints and finaly goes to the final destination, like this:

User types in city to travel from---------> goes through standard waypoint(mark B)---->goes to second waypont(mark c)----------> goes to final destination.

So whats the problem here?
well I want the user to recive directions that show him/her a route that uses a ferry between two countries BUT I get a route that does :

Startpoint---------->waypoint---->goes back to find a bridge and conntinnues to do so through the entire route(avoides water at all cost) and finaly it reaches the end destination.

Images:

Startpoint to first waypoint (Mark A----> Mark B)
http://imageshack.us/photo/my-images/24/number1b.png/[^]

As you can see it turns back from the waypoint and finds a bridge that goes between malmö and kopenhagen.

Mark B---->Mark C
http://imageshack.us/photo/my-images/21/number2l.png/[^]

As seen here it travels to the second waypoint and then turns to the final destnation.

The entire undesired route:
http://imageshack.us/photo/my-images/51/number3i.png/[^]

What I want:
http://img843.imageshack.us/img843/7837/wishy.png[^]

I want the route to give me directions from waypoint B to waypont C BUT I WANT IT TO TRAVEL VIA FERRY from trelleborg to rostock via ferry travel.

I tried setting the coordinates over the water and figured that googlemaps would automatically understand that it has to travel by ferry, this is how it was done in version 2(on the project im working on) but it did not. I have tried changing the travelmode but that did not work.

I have been reading up on this and looking for a answer but I have not found anything that can help me, either im missing something or i don't know what im doing wrong.

The code:

Document ready:

XML
function initialize(){

          //Map on page

                  map = new google.maps.Map(document.getElementById('map'), {
                  zoom: 10,
                  center: new google.maps.LatLng(<%=CenterCoords%>),
                  mapTypeId: google.maps.MapTypeId.TERRAIN
                });
                 map.setCenter(new google.maps.LatLng(<%=CenterCoords%>), <%=ZoomLevel %>); //

                 var marker;
                 marker = new google.maps.Marker({
                 position: new google.maps.LatLng(<%=CenterCoords%>),
                 map: map
                 });

                 mapdir = new google.maps.Map(document.getElementById('map_dir'), {
                  zoom: 10,
                  center: new google.maps.LatLng(<%=CenterCoords%>),
                  mapTypeId: google.maps.MapTypeId.TERRAIN
                });


                mapdir.setCenter(new google.maps.LatLng(<%=CenterCoords%>), <%=ZoomLevel %>); //

                var markerdir;
                markerdir = new google.maps.Marker({
                position: new google.maps.LatLng(<%=CenterCoords%>),
                map: mapdir
                   });

                gdir  = new google.maps.DirectionsService(mapdir, document.getElementById("directions"));

                google.maps.event.addDomListener(gdir, "error", handleErrors);


            $("#aspnetForm").submit(function() {
                overlayDirections();
                //new google.maps.DirectionsService();
                return false;
            });

            //Print a map using image from Google map
            $("#print-map").click( function() {
                $('.divMapToPrint').jqprint({ printContainer: false });
                return false;
            })

            //Print direction using Google maps
            $('#print-directions').click(function(){
                if (document.getElementById("fromAddress").value != '')
                    window.open("http://maps.google.com/maps?f=d&source=s_d&saddr=" + document.getElementById("fromAddress").value + "&daddr=<%=GetToAddress() %>&hl=sv&geocode=AIzaSyDFPnl71Kf3R8ZJW3EEorajmOnf9y9LExw&mra=ls&sll=37.0625,-95.677068&sspn=47.167389,75.410156&ie=UTF8&z=7&layer=c&pw=2","mywindow");
                else
                    alert('<%=GetKey("HotelPrintNoFromAddress") %>');
                return false;
            });

        }




user interaction code:

XML
function overlayDirections()
       {
           $("#directions").text('');
           fromAddress = document.getElementById("fromAddress").value;
           var language  = '<%=CurrentPage.LanguageBranch %>';

           <asp:PlaceHolder ID="Domestic" runat="server" visible="false">
               /*No ferry is needed*/
               var noOfPoints = 2;
               var waypoints = new Array(noOfPoints);
               waypoints[0] = fromAddress;
               waypoints[1] = '<%=GetToAddress() %>';
           </asp:PlaceHolder>

           <asp:PlaceHolder ID="International" runat="server" visible="false">
               /*Ferry is required*/
               viaAddress = document.getElementById('viaAddress').value;
               var noOfPoints = 4;
               var waypoints = new Array(noOfPoints);
               waypoints[0] = fromAddress;
               if (viaAddress == 'Rostock')
                   waypoints[<%=GetVia(1) %>] = '55.306898,13.131795'; //(Trelleborg - Rostock) 55.372387,13.149916
               else
                   waypoints[<%=GetVia(1) %>] = '55.309805,13.119736';
               waypoints[<%=GetVia(2) %>] = viaAddress;
               waypoints[3] = '<%=GetToAddress() %>';
           </asp:PlaceHolder>

           directionsDisplay = new google.maps.DirectionsRenderer();
           directionsDisplay.setMap(mapdir);

           gdir.route({
           origin: waypoints[0],
           destination: waypoints[3],
           waypoints: [{
           location: waypoints[1]

           },
           {
           location: waypoints[2]

           }],
           provideRouteAlternatives: true,
           travelMode: google.maps.DirectionsTravelMode.DRIVING
         }, function(result, status) {
           if (status == google.maps.DirectionsStatus.OK) {
             directionsDisplay.setDirections(result);
           }
         });

       }




If anything is unclear just ask.
thankfull for answers!
Posted
Updated 16-Apr-13 1:51am
v3

1 solution

After a long while i foudn the soloution!

"avoidTolls: true"
"If true, instructs the Directions service to avoid toll roads where possible. Optional."

So basicly the logic here was to set thios attribute to true so that googlemaps avoids roads where a fee is to be given to travel on it. And then it took the ferry.

The funny thing here is that the ferry travel is costly aswell, but hey thats the logic that googlmaps has implemented.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900