Posts tagged Google Maps
Auto Center and Zoom with YM4R
Oct 11th
When I blogged last month about abstracting mapping with YM4R I commented:
What I’ve not yet discovered (and may not be implemented) is a way to automatically center and zoom a map. It would be very nice to be able to add a batch of points to a map and have the plugin automatically work out their mid-point. Maybe I need to work on that a little…
What I was missing was the center_zoom_on_bounds_init method. There are various ways to interact with it, but I’ve chosen:
sorted_latitudes = locations.collect(&:latitude).compact.sort sorted_longitudes = locations.collect(&:longitude).compact.sort @map.center_zoom_on_bounds_init([ [sorted_latitudes.first, sorted_longitudes.first], [sorted_latitudes.last, sorted_longitudes.last]])
That mostly replaces the call to
@map.center_zoom_init([latitude, longitude], 14)
that I was previously using, though I’ve actually kept that around if there’s only one point as I was finding Google Maps’ maximum zoom didn’t show quite enough context for my tastes.
While it would be nice if the plugin had a method that performed the calculations based on the points you’ve fed in, it looks like the current implementation stores the points as javascript strings, so that’s not really an option. But with only three lines of code involved, this is a nice simple way to get the desired effect.
Abstracting mapping with YM4R
Sep 1st
When Grand Rapids WiFi–then a PHP application–first implemented google map support all the logic for producing those maps came in the templates. The controller passed the view a list of locations for that page, and it wrote out a series of javascript calls that produced the map. It was a little unreliable and very clunky, but it worked. When I moved the app over to rails, that was one aspect that I didn’t change, and it continued to work.
With more and more talk lately of google and lock-in, and some desire to begin to do more with the maps (perhaps invoking the ajax buzzword) I realised that I needed better tools and cleaner abstraction to work with the map. And it just so happened that YM4R_Mapstraction appeared at just the right time.
YM4R_Mapstraction is a rails plugin that provides methods to build map objects in your controller and helpers that will turn that into a map in the view. Controller code looks like:
@map = Mapstraction.new('map', :google) @map.control_init(:small => true) @map.center_zoom_init([location.latitude, location.longitude], 11) @map.marker_init(Marker.new([location.latitude, location.longitude], :label => location.name, :info_bubble => location.description))
and view code would be:
< %= Mapstraction.header(:google) %> < %= @map.to_html %>
in the header, and
< %= @map.div(:width => 300, :height => 300) %>
where you want your map to appear in the page.
To switch from a google map to a yahoo map, you simply need to replace ‘:google’ with ‘:yahoo’ in the examples above, opening up the possibility of automatically checking for server availability, or of allowing users to choose their preferred map.
One thing I found frustrating was simply having text for the description in the info bubble. That was easily solved by creating a partial called _bubble.rhtml and changing the marker code to:
@map.marker_init(Marker.new([location.latitude, location.longitude], :label => location.name, :info_bubble => render_to_string(:partial => 'bubble')))
So far, using the partials doesn’t seem to slow things down too much, but eventually I hope to cache the partials to reduce any overhead that might introduce.
What I’ve not yet discovered (and may not be implemented) is a way to automatically center and zoom a map. It would be very nice to be able to add a batch of points to a map and have the plugin automatically work out their mid-point. Maybe I need to work on that a little…
More WiFi Google Mapping
Jul 1st
There has been much jubilation since Google announced their javascript-based maps API yesterday. I’ve been playing with it a little and have updated the Grand Rapids WiFi google map to use the new API. The web pages produced this way are much more responsive than those using the old hacks, and it’ll be great to see what people produce now we have official support.
I’m hoping to shortly release a new version of the WiFi site which allows search results to be plotted on a map (or viewed as a list as at present). Until then, this implementation remains ‘experimental’.
Google Maps and Grand Rapids WiFi
May 19th
For the past few weeks I’ve been meaning to play with myGmaps, and last night I finally got the opportunity. I’d introduced a map view to Grand Rapids WiFi a few months ago, but I’ve never been entirely satisfied with the map in use or the flexibility of the zoom, so I decided to explore what it would take to move that data onto a google map.
Generating the required XML was very straightforward. Between this piece at Engadget and the tools at myGmaps it was very simple to add a new Smarty template to the site and get everything up and running. It’s a shame google didn’t go with some more standardised vocabularies (it would be wonderful to be able to pipe the existing RDF version of the site straight in), but at least the format is simple.
What I did find, however, was that my browsers quickly became unstable. I’d never seen Firefox’s “A script is making Firefox become very slow, should I terminate it?” warning before last night, but plotting around 40 points on such a map triggered it. Reducing that to five points with an SQL LIMIT helped considerably, but at the cost of much of the map’s utility. The full maps is nice, but nowhere near stable enough to make a regular part of the site (I may put a link to it, but it will be with provisos).
I hope there’s a way to reduce the weight of this toolkit, as it’s definitely a far better map. If google were to launch a public API, perhaps we could start building more advanced applications which integrate directions and other tools. For now, it’s back to the drawing board for me, to try and work out a UI that lets me reduce the number of points plotted while still being useful for the user.
For those with plenty of RAM, you can find the full map at grwifi.net/gmaps/.