Make custom routes

2,540 views
Skip to first unread message

Kasper

unread,
Sep 28, 2011, 5:37:54 PM9/28/11
to google-map...@googlegroups.com
I have read that it is possible to create custom polylines on your own maps, but is it also possible to plan routes using only those points?
Please let me know

Also, is there an easy way to make lots of those polylines, with a tool or something?
Message has been deleted

davie strachan

unread,
Sep 29, 2011, 5:06:03 AM9/29/11
to Google Maps JavaScript API v3

Kasper

unread,
Sep 30, 2011, 10:43:18 AM9/30/11
to Google Maps JavaScript API v3
That looks like what I need!
And when I want google to use those lines to plan routes across them,
do they all need to be 1 shape, or how do I have to build the railroad
network?

Kasper

On Sep 29, 11:06 am, davie strachan <daviestrac...@gmail.com> wrote:
> Tryhttp://www.doogal.co.uk/polylines.php
> Regards Davie
Message has been deleted

Rossko

unread,
Sep 30, 2011, 2:45:56 PM9/30/11
to Google Maps JavaScript API v3
> And when I want google to use those lines to plan routes across them,
> do they all need to be 1 shape, or how do I have to build the railroad
> network?

You can't persuade the Google routing engine to use your data.
As already discussed in
http://groups.google.com/group/google-maps-js-api-v3/browse_thread/thread/5fb2a3655a4722f2/3465f27a584d81df
as you want to do custom routing, you will need to build your own
route database (this is about the logical connections of the route
segments, not just the data to draw the lines) and implement your own
routing algorithms.
It is not a trvial task.
Message has been deleted

Kasper

unread,
Sep 30, 2011, 3:55:07 PM9/30/11
to google-map...@googlegroups.com
That is for when you'd like the user to plan it's own routes, right?

Let's say if I wanted to animate trains along custom drawn polylines, how hard would that be?
And the data for the departure and arrival times would be gathered from a database.

geoco...@gmail.com

unread,
Sep 30, 2011, 4:14:25 PM9/30/11
to Google Maps JavaScript API v3
On Sep 30, 12:55 pm, Kasper <kaspervandel...@gmail.com> wrote:
> That is for when you'd like the user to plan it's own routes, right?
>
> Let's say if I wanted to animate trains along custom drawn polylines, how
> hard would that be?

If you have a polyline, it isn't hard (or at least there are examples
of how to animate a marker along the polyline). You just have to
adapt them to your purpose.

-- Larry

Rossko

unread,
Oct 1, 2011, 6:15:59 AM10/1/11
to Google Maps JavaScript API v3
I'm probably misunderstanding what you mean by 'plan routes'.

Can you describe what you expect a user to see and do, and what you
expect to happen when they do it?

As Larrys says, it's easy to move a marker along a known polyline and
you've already found an example of that in your other threads.

> And the data for the departure and arrival times would be gathered from a
> database.

Okay, are you asking for help with that? This probably isn't the
right place as it won't be a Google maps database.

Kasper

unread,
Oct 1, 2011, 7:34:48 AM10/1/11
to google-map...@googlegroups.com
@Larry:
Yeah, that would be good aswell, as long as it is possible to move them based on time, and really animate them, so you can see them move.
Ok, the dynamic animation isnt really neccessary, but would make the whole look a lot better.

Could you perhaps show me an example of the examples? Because I've searched a bit, but couldn't find a proper example...

@Rossko:
What I mean by planning routes, is planning routes like you do in Google Maps.
The reason I wanted to do that, is because I had this example [ http://www.kmcgraphics.com/google/ ] where a planned route was animated.
But if that can be done with just poly lines, that would be great.


Can you describe what you expect a user to see and do, and what you
expect to happen when they do it?


Ok, what I want the users to see:
-A map of The Netherlands, where they can select a province.
-After clicking that province, a zoomed Google Maps map [of that province] is shown where markers are moving along the railroad tracks on the maps. (and those railroadtracks would have to be make by me, in poly lines, as it seems).
-Then they could click the markers for additional information, but that is not very relevant at the moment.
What I want to do is use my custom made railroad routes (made from poly lines, following the railroad tracks in google maps)


As Larrys says, it's easy to move a marker along a known polyline and
you've already found an example of that in your other threads.

Unfortunatly that example I found, uses google's routes, not poly lines, as far as I know.

Rossko

unread,
Oct 1, 2011, 8:35:25 AM10/1/11
to Google Maps JavaScript API v3
> -A map of The Netherlands, where they can select a province.

OK

> -After clicking that province, a zoomed Google Maps map [of that province]

OK

> is shown where markers are moving along the railroad tracks on the maps.
> (and those railroadtracks would have to be make by me, in poly lines, as it
> seems).

Are you expecting to track trains in real time here? You will need a
source of information about train location. Google cannot provide
that.
Or do you just want randomly moving markers?

> *As Larrys says, it's easy to move a marker along a known polyline and
> you've already found an example of that in your other threads. *
> Unfortunatly that example I found, uses google's routes, not poly lines, as
> far as I know.

Yes. It gets a route from the Directions service. gets the polyline
for that route, then animates it. So what you want is easier, just
the last part.

Kasper

unread,
Oct 1, 2011, 9:00:20 AM10/1/11
to google-map...@googlegroups.com
@Rossko:
Well, the national train company from The Netherlands has got a database, which is accessible to all.
In that database you can request all the available trainstations, and request all departing trains for every station. A small example of that can be seen here: http://ephemurl.com/3d/pjo [made by me]
Using cronjobs I could then save departure and arrival dates for each station, and then calculate the train locations on the routes.

As for my example, that really is awesome!
I didn't get a chance to look at that yet, so in that case it would be awesome, atleast, if I could change the speed and location on the route.

I ran into this example by accident yesterday, which is exactly what I want to make: http://kubus.mailspool.nl/spoorkaart/
If this is too complex to make, I can maybe make it without the animations, and add a forced refresh or something.

Thank you very much for your help so far btw, I really appreciate it!

Rossko

unread,
Oct 1, 2011, 12:47:26 PM10/1/11
to Google Maps JavaScript API v3
If you are calculating and plotting real-time train locations, you
don't need any kind of polyline animation as such. You just plot the
trains, and recalculate and re-plot at intervals.

You will need the polyline between stations so that you know where to
plot the trains, though. From the timetable you could derive the
duration of the leg to the next station. From your polyline you can
get the distance between stations. Then you know the speed of the
train, and can calculate how far it has got along that leg at some
given time. Then you can plot the train position at that distance
along the line.

You'll need some kind of logic to select which of your polylines is
the right one leading away from any given station, that should just
involve checking destination.

I'm not sure how useful is for a traveller to know which trains they
have missed though. It might be better to look ahead and see which
trains will depart soon, but that depends on what your purpose is.

Kasper

unread,
Oct 1, 2011, 2:28:36 PM10/1/11
to google-map...@googlegroups.com
What exactly do you think is the difference between an animation and calculating the positions in intervals?
Ultimately I'd like to have the marker moving smoothly, (which is why I reffered to that example earlier) and I don't know if that's the case with what you are describing (the intervals).

As for the selection of polyline, that would indeed be hard.
I'd either have to make seperate polylines for each trainroute, or make each segment unique, so I can specify which routes need which segments.
Does that sound like a good idea?
Or even possible? [to animate through different polylines]

Could you perhaps give an example or a link to a page with help about putting a marker at a certain distance on a polyline?

Rossko

unread,
Oct 1, 2011, 3:21:39 PM10/1/11
to Google Maps JavaScript API v3
> What exactly do you think is the difference between an animation and
> calculating the positions in intervals?

Same thing ; my point was that you don't need to animate or display
any kind of _polyline_ at all, just plot train markers on the map.
Polyline-animating examples may be irrelevant for you.

> Ultimately I'd like to have the marker moving smoothly, (which is why I
> reffered to that example earlier) and I don't know if that's the case with
> what you are describing (the intervals).

What exactly is the difference between smooth animation and doing it
in steps at short intervals? How smooth you can get it depends on the
resources you can devote to it, I'd worry about that after you have a
functional demo.

> As for the selection of polyline, that would indeed be hard.
> I'd either have to make seperate polylines for each trainroute, or make each
> segment unique, so I can specify which routes need which segments.

Yes, something like that. Exactly how you do it is up to you. I
would think a dataset of segments of railroad between each station
form a flexible basis? For each segment, you'd need the polyline
describing the route for your plotting purposes, and something to
indicate the 'node' at each end - the stations - so that when you get
your timetable info you can search for and select the segment.

You are going to have to deal with cases of trains going A to C
without stopping at B inbetween (so B won't be in the timetable info
you get), making it more difficult to select the correct segments. No-
one said this was easy, but there's more than one way to deal with
that.

> Could you perhaps give an example or a link to a page with help about
> putting a marker at a certain distance on a polyline?

Searching this group leads to a few
http://groups.google.com/group/google-maps-js-api-v3/search?group=google-maps-js-api-v3&q=distance+along+polyline
Larry's example
http://www.geocodezip.com/v3_polyline_example_kmmarkers_0.html

Kasper

unread,
Oct 4, 2011, 3:36:22 AM10/4/11
to google-map...@googlegroups.com
Cool, thanks for those examples, I'll check them out soon.

As for the database, considering the fact trains always follow fixed routes, I guess I could make those myself from the polyline segments.
However, this will not be very nice with temporary changes, I'll have to look into that.

Kasper

unread,
Oct 12, 2011, 4:35:55 PM10/12/11
to google-map...@googlegroups.com
I didn't get to work on my web application a lot this week, but I found a way to get the times of arrival a lot faster and easier. (by planning routes and saving all the stations)
Now, the problem is that all the routes are planned in the future, you can't request data from the past. So somehow I have to buffer the train data on my website.

What do you think is the best way to do that?
It is hard to do in databases (atleast, I think so) because the amount of stations on a route is never the same...

Also, I need to find a way to get the length of a polyline, is that just the function: polyline.Distance(); ?
That seems a bit too simple imo, though I would really like it if so :D

vasile

unread,
Oct 12, 2011, 4:48:32 PM10/12/11
to google-map...@googlegroups.com
Hello Kasper,


On Wednesday, October 12, 2011 10:35:55 PM UTC+2, Kasper wrote:
What do you think is the best way to do that?
It is hard to do in databases (atleast, I think so) because the amount of stations on a route is never the same...

It's animating markers(vehicles) along polylines(train tracks) by know-ing the departures and arrival in the stations and the tracks between them. You can see it in action here http://www.vasile.ch/hacks/swisstrains-v2/
 
Also, I need to find a way to get the length of a polyline, is that just the function: polyline.Distance(); ?

You need to include the geometry lib in your API call

Then use google.maps.geometry.computeLength()

Kasper

unread,
Oct 15, 2011, 7:20:57 AM10/15/11
to google-map...@googlegroups.com
Wow that project looks amazing!
All your data is coming from the map.js file?
Could you perhaps explain a bit how you did that?

Sorry for the late reply...

Kasper

unread,
Oct 15, 2011, 7:32:42 AM10/15/11
to google-map...@googlegroups.com
I've been looking at your code now, but you've made everything in JS, as it seems.
I, unfortunatly, am not familiar with javascript...
Some things I'm really curious about:

-How did you build your routes and stations?

-(How) did you cache the planned routes? (assuming that is the way you get the arrival and departure dates)

-How did you animate the markers across the polylines?

Vasile Cotovanu

unread,
Oct 25, 2011, 1:39:35 PM10/25/11
to google-map...@googlegroups.com
On Saturday, October 15, 2011 1:32:42 PM UTC+2, Kasper wrote:
I, unfortunatly, am not familiar with javascript...

:( then I suggest you to invest some time and read more about it, is an important prerequisite if you want to go in advanced topics as animating marker along a polyline. 
 
-How did you build your routes and stations?

I digitized (manually) the whole swiss railway network then exported the vectors in two places
- Fusion Tables - which I am using just to display the network (the red dot and lines)
- Encoded polylines - https://github.com/vasile/vehicle-simulator/blob/master/static/js/edges_encoded.js - content of this file is used for building the vehicle routes
 
-(How) did you cache the planned routes? (assuming that is the way you get the arrival and departure dates)

Based on the timetables I know in which stations a vehicle stops, so having the network I can pre-compute what elements of the network are needed to follow that route. This I did using simple graph algorithms, i.e. Dijkstra. The output is then a simple list of polyline ids that are needed to build a route. Check the "edges" section of the README here https://github.com/vasile/vehicle-simulator/
 
-How did you animate the markers across the polylines?

Well, being just a simulation and knowing the routes and the timetables all you have to do is to see how much time passed since last station and how much time left. Knowing these and the route geometry between the two stations, you can project a point along a polyline at given percent. The "magic" is in linesPool.positionGet() from https://github.com/vasile/vehicle-simulator/blob/master/static/js/map.js

Regarding this feature, I asked for a feature request, feel free to star it to get prioritized :) http://goo.gl/P0exk

Kasper

unread,
Oct 25, 2011, 2:27:02 PM10/25/11
to google-map...@googlegroups.com
Yeah, I guess I have to figure out a lot more about Javascript before I can make something close to this...

I actually already figured out you used the FusionTables as overlay, but didnt really understand why you also used the encoded polylines. (I do now :P )
But how did you place them on the polylines? I assume your routes consist from different small polylines, which are then linked together (in an array?)
I've tried doing this myself here [ http://bit.ly/vY4YFj ] but i havent figured out how to really... I got the code from this page [ http://www.geocodezip.com/v3_polyline_example_kmmarkers_0.html ] but I believe that uses only 1 polyline, and I use multiple [like you?]

As for the linesPool.positionGet function, that looks very nice!
But I'm not sure how this would be useable in my case, as I dont have the complex algorithmes you have [and don't have the time to make/use any].
It seems nice, and I already set up a way to calculate the percentage of the trains on the full route [first to last station, instead of previous to next station].
Calculating the percentage otherwise would require me to get the lengths of the polylines, which I figured out today :P [ also this page: http://bit.ly/vY4YFj ]

So basically all I want to know for now is how to place a marker along a polyline. Which isn't really working for me using the script I got from that other page. [ http://www.geocodezip.com/v3_polyline_example_kmmarkers_0.html ]
Perhaps you could give me some tips/directions/info on that?

Vasile Cotovanu

unread,
Oct 25, 2011, 4:14:59 PM10/25/11
to google-map...@googlegroups.com
On Tuesday, October 25, 2011 8:27:02 PM UTC+2, Kasper wrote:
But how did you place them on the polylines? I assume your routes consist from different small polylines, which are then linked together (in an array?)

That's correct.
 
I've tried doing this myself here [ http://bit.ly/vY4YFj ] but i havent figured out how to really... I got the code from this page [ http://www.geocodezip.com/v3_polyline_example_kmmarkers_0.html ] but I believe that uses only 1 polyline, and I use multiple [like you?]

Let's say that you have a vehicle that starts in DenBosch and ends in Tilburg Uni, so it has two paths (polylines)
- DenBosch_TilburgCentraal_path
- TilburgCentraal_TilburgUni_path

If you combine them (I would let you do the search for how to sum up two arrays in JS) you get a bigger polyline. 

Assuming that a train leaves DenBosch at 10:10 and arrives in Uni at 10:20. 
If the clock is 10:14 then the train should be projected at 40% along the longer path.

Here is a bit of code of how I did it in my project, you need to change / adapt it to your needs. Plus, to perform the animation, I would let you google / use this forum / check my code to see what is the core JS function that is used to repeat a task (i.e. to animate a marker). 
 
As for the linesPool.positionGet function, that looks very nice!
But I'm not sure how this would be useable in my case, as I dont have the complex algorithmes you have [and don't have the time to make/use any].

Well, the heavy lifting is done in advance(serverside), i.e. computing which polyline ids are forming a route from A to B, then the "problem" reduces to the one that you have; the pastie that I gave you is based exclusively on my linesPool.positionGet() method.

Vasile Cotovanu

unread,
Oct 25, 2011, 4:16:32 PM10/25/11
to google-map...@googlegroups.com
Forgot to link the pastie. Here it is: http://pastie.org/2758220

Kasper

unread,
Oct 25, 2011, 4:57:28 PM10/25/11
to google-map...@googlegroups.com
Thanks for that explanation, it is indeed very simple to calculate the percentage between two stations [I've actually made something similar, but then giving the time in hours :P ]

You are talking about combining the paths, now, Oh wait...
I just realised that the data which is loaded into a path is just an array of gps locations!
I never looked at the data like that before! This opens up completely new possibilities!
And ofcourse that makes it very easy to find the first and last point on the route!

At the moment I load my paths from a database [which basically is a table with the names of the pieces between stations, fully in javascript so I echo them and they work instantly as a polyline.
I never realised I was using an array of coordinates to make those polylines.


Back to your function:
Let's see if I get this correctly:
You have an array with all the required points for the route from one station to another. [path_points]
Then you calculate the traveled distance [distanceAC]
and seek what point on the polyline [from path_points] should have been passed to be where you are now.

But there I lose it:
How do you determine where the vehicle is?
As far as I understand, you have a polyline which is VERY detailed and has got hundreds of points with equal spaces between them, where you put the vehicle, to indicate its position?
Or does this line:
position = google.maps.geometry.spherical.interpolate(p1, p2, (distanceAC - cumulatedDistanceToC)/d12);
allow the vehicle to be placed between two points? [as they are all just straight lines between the coordinates]

also: what is path_length? is that just the length of the route from one station to another? [i don't fully understand that, because you say: compute the length]

Kasper

unread,
Oct 28, 2011, 3:13:58 PM10/28/11
to google-map...@googlegroups.com
Ok, so I just put the script in my code...

And it works!! This is amazing!!

I first had to completely change the way I interacted with my server to get the polyline data, but now it's much easier.
I will now also have to start writing a php function [as I get all my data from php] which picks the right polylines etc. to display the route, but this is really amazing!
Thank you so much for this function!!!

You can see it here btw: http://bit.ly/vY4YFj

Kasper

unread,
Oct 30, 2011, 12:01:59 PM10/30/11
to google-map...@googlegroups.com
There are just a few things bothering me though:
-How did you select the polylines you needed for determining the positions?

and:
-How do you take returning rides into account? For example, when a train is going away from a station, it will also come back some time, but the coordinates of the polyline will have to be inverted, how did you work with that?

Vasile Cotovanu

unread,
Nov 7, 2011, 4:48:19 AM11/7/11
to google-map...@googlegroups.com
On Sunday, October 30, 2011 5:01:59 PM UTC+1, Kasper wrote:
There are just a few things bothering me though:
-How did you select the polylines you needed for determining the positions?

That's done in advance, server-side, check the AJAX feed with vehicle data that is explained here https://github.com/vasile/vehicle-simulator
 
-How do you take returning rides into account? For example, when a train is going away from a station, it will also come back some time, but the coordinates of the polyline will have to be inverted, how did you work with that?

Just reverse the array containing the coordinates, i.e.

Kasper

unread,
Nov 7, 2011, 11:05:32 AM11/7/11
to google-map...@googlegroups.com
Ah ok, thanks.
I think its working just perfect now: http://bit.ly/vZdevz [the dark blue marker is de train] [press Volg Deze Trein to keep updating the location: it refreshes every minute]

At first I thought the position calculation was inaccurate, but then I thought of the fact that the polylines need to be properly sorted in the array, or else the marker would jump all over the place.
This makes me realise even more how complicated your script probably is.
But I will definitly have a look at it!
Reply all
Reply to author
Forward
0 new messages