Question

I am using ym4r_gm to create a map of markers on my website. I have created 1 marker which is fine with the following code.

@map = GMap.new("locations_map")
@map.control_init(:large_map => true,:map_type => true)
geocode = GMap::Geocoding.get("G12 8BZ")
@map.record_init @map.add_overlay(GMarker.new([geocode.first.latitude,geocode.first.longitude], :title => "Hillhead, Glasgow", :info_window =>"Hillhead, Glasgow"))

How would I go about getting a group of markers to display on the map? I have an array of postcodes (zipcodes) like so:

postcodes = ["G11 6PR", "G1 T3R", "G12 8BZ"]

I have noticed the MarkerGroup class in ym4r_gm but I can not figure it out :-S

If you someone could give me a hand that would be amazing, here is a link to the docs also.

http://ym4r.rubyforge.org/ym4r_gm-doc/

Any help would be appreciated.

Cheers

Eef

Was it helpful?

Solution

Not sure about ym4r_gm, but with ym4r_mapstraction it would be something like this (although this would be with both ym4r_gm and ym4r_mapstraction available since I dont think ym4r_mapstraction has the handy geocoding helper)

 @map = Mapstraction.new("map_div", :google)         
 @map.control_init(:small_map => true, :map_type => true)

   postcodes.each do |this_postcode|
     begin
        # might want to put the Geocoding part into a begin-rescue clause
        # in case postcode isn't valid

        returned_geocode = GMap::Geocoding.get(this_postcode)
        this_title = this_postcode
        new_marker = Marker.new([returned_geocode.first.lat.to_f, returned_geocode.first.lon.to_f], :info_bubble => this_title, :icon => "images/gmaps/blue_image.png")
        blue_markers.push(new_marker)
      rescue Exception => e
        logger.error e.inspect
      end
   end
 @map.marker_group_global_init(MarkerGroup.new(blue_markers, true),"BLUE")
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top