Last update: 2008-03-17 (see comments)
The problem
The week's coming to an end, you desperately want to meet your friends for a beer (or two). But with over 250 bars in Amsterdam to choose from, where should you meet? Let's say you want to minimize the distance that everyone has to travel to the bar. So you'd want to find the bar nearest to the Center of Gravity or Center of Mass (wikipedia) of everybodies location...
How about a website that lets you pinpoint the location of all your friends, shows you the geographical center and lets you search for nearby bars?
Solutions already out there
Brammeleman already solved the hardest part of this problem, whilst trying to find the best location for his family reunion. Brammeleman's php script, as discussed in his post on nerdnotes.org (in Dutch), takes in a Google Earth KLM file and calculates the geographical center of the placemarks in the KML. This point is served back as a KML file, which can than be opened in Google Earth.
Stefan Geens, implemented Brammeleman's algorithm (with slight adjustments which Brammeleman later adopted in his script), in a similar application. His post at www.oglearth.com gives all the background info on Center Of Gravity theory, so check that out if you're interested.
The juggling with KML files and the need to have Google Earth installed doesn't make this approach a viable solution to my friday night drinking problems. Every week there is a different bunch of us, and we don't always start out from the same place. I need to be able to add and adjust locations on the fly.
At www.geomidpoint.com they come close to my needs. This application lets you put in a number of cities or coordinates by hand and then calculates either the Center of Gravity or the Center of Distance. The former is the same geographical center of the given coordinates as Brammelemans algorithm calculates. The latter is the point where the total sum of distances is minimized. Check out the methods page on their site for better explanation about the difference between the two. The Center of Distance is found by a brute force algorithm and not by mathematical calculation. The result is displayed on a Google Maps map, along with the given points.
Now this is close. But still not close enough. I want to be able to search within one city instead of in between cities. Although this is technically possible by entering latitudes and longitudes for each point... it's not really user friendly.
My solutions mash-up
Since the ultimate solution wasn't yet out there, I sacrificed a weekend on building it myself.
So, I translated Brammeleman's Center Of Gravity algorithm into Javascript to whip up a client side Google Maps application. This Google Maps mashup lets you add placemarks by clicking on a map and calculates the Center Of Gravity on the fly. The search nearby form field on the left pane queries Google Maps for the bars or pizza places closest to the geographical center of all marked locations and displays the results on the map.
Additionally, you can upload a KML file or provide the url to an online file or a custom created Google Maps map ("My Maps"). The application plots the placemarks from the KML file or the "My Maps" source on the map and calculates the Center Of Gravity. You can deselect the placemarks you don't want to use for the calculation and add additional placemarks if needed.
To use a custom made Google Maps map as the source for the calculation, simply go to the My Maps area on Google Maps, select/create the map you want to use and click the "link to this page" link. The than presented link can be copied into the form field in the Center of Gravity application to load the placemarks on the map.
The source files for this application are available here.
Below is an embedded version of the Center Of Gravity application.
The fullscreen version can be found here.
All credits for the Center of Gravity algorithm go to Brammaleman, with noted additions by Stefan Geens.
Please let me know what you think by leaving a comment. You're free to use this script for your own application, but I'd appreciate it if you tell me about it. Plain curiosity :-)
For my other Google Maps trials, see this overview.
Future improvements
* Better KML support (now only few formats actually work)
* Loading of KML files through AJAX
* Placemark management (edit name/delete)
* Doubleclick zoom-in without adding a marker (annoying!)
* Optional Center of Distance calculator
* Permalink option, to be able to send the resulting map to friends
Update 2008-03-04:
ReplyDelete- Search Nearby form field moved from marker infowindow to left pane.
- Search Nearby results now plotted on the map and summarized in the left pane.
- On drag/drop of markers, Search Nearby results are updated instantly.
This is exactly what I had in mind back then. At that time, I didn't yet knew about google maps. Having an interactive JS implementation in maps makes more sense. Especially dragging the markers and see what it does to the cog, is nice! Great work! I wonder how many octave functions are translated to javascript through php ;-) Cheers, Bram
ReplyDeleteUpdate 2008-03-17:
ReplyDelete- restructured PHP, HTML and CSS
- KML handling now through a class
- Upload KML from harddrive now possible
Hi there
ReplyDeleteBrammaleman's site is no longer available. Do you know where I can find the original php script?
Thanks
Hi there!
ReplyDeleteThis is just what I was looking for. Great function!
However, I am trying to use it and by both uploading or putting the URL it is not working. I cannot input my places.
The URL is from google maps and the file is a KML from Google earth pro.
Can you give me a help?
Thank you!
Frederico
@frederico:
ReplyDeleteI don't know if google earth pro KML files work. It would surprise me if the don't,
I just tested a google maps url to see if something is broken. My urls still work fine.
Do you have a custom map under the "my maps" section, where you have your placemarks? If you do, open it and click on the "link" icon. It presents the permalink to this map. It should look something like this:
http://maps.google.com/maps/ms?ie=UTF8&hl=nl&msa=0&msid=206311162471104306271.000443eb3dd26e2d3fdbe&t=h&z=13
You can take the link above to test the function, it should give you the COG for a couple of bars in Amsterdam.
To test if your googlemaps url should work, try appending the url with "&output=kml" and viewing that in your browser. It should serve you the KML of your map. If it doesn't, the url will not work with my mashup either, since thats what the function does (appending the output parameter to fetch the kml).
Is this what you tried?