Skip to content

Instantly share code, notes, and snippets.

@tordans
Created October 17, 2011 15:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tordans/1292895 to your computer and use it in GitHub Desktop.
Save tordans/1292895 to your computer and use it in GitHub Desktop.
Rails Helper to create Google Static Map-Images. Used in StayScout.de
module GoogleStaticMapHelper
# Creates an image tag for a static google map
# Learn more: http://code.google.com/intl/de-DE/apis/maps/documentation/staticmaps/
# Code inspired by BillyIII's gmap2ozi http://github.com/BillyIII/gmap2ozi/tree/master
# TODO: Helper so ändern, dass er ein array von Adressen aufnimmt, die dann als Marker angezeigt werden. Alternativ müssen es aber Koordinaten sein...
def static_google_map_image_tag(markers, options = {})
options.symbolize_keys!
url = "http://maps.google.com/maps/api/staticmap"
# sensor:true = GPS-Geräte die Konstant die Location updaten. sensor:false = normale maps
# Hinweis: Sensor is required und muss false sein damit es funktioniert. Daher lieber defautl setzen als Gefahr laufen, dass die Abfrage nicht klappt wg. ihrem Wert true/false...
url << "?sensor=false" # + (options[:sensor] ? 'true' : 'false')
# The google maps key must be placed in the application.yml "google_maps_api_key"
url << "&key=#{h(Settings.google_maps_api_key)}"
# The size of the map.
url << "&size=#{h(options[:size])}"
# No delete since this is also used for image-widht and -height below.
# You can change the color of all markers if you want.
options[:color] = "color:#{h(options[:color])}|" if options[:color]
# All the markers that are given and the color changed.
# Markers can be a string or lat/long. See docs at google.
url << "&markers=#{options[:color]}#{h(markers)}"
options.delete(:color)
options.delete(:markers)
# The map format. Default is roadmap.
# * roadmap (Standard) zeigt eine Standardstraßenkarte an, wie sie normalerweise auf der Google Maps-Website zu sehen ist. Wenn kein maptype-Wert angegeben wurde, zeigt das Static Maps-API standardmäßig roadmap-Kacheln an.
# * mobile definiert ein Straßenkarten-Bild für mobile Geräte. Symbole und Text sind größer dargestellt, entsprechend der höheren Auflösung und dem kleinen Bildschirm mobiler Geräte.
# * satellite definiert ein Satellitenbild.
# * terrain definiert eine physische Reliefkarte mit Gelände und Vegetation.
# * hybrid definiert eine Hybridkarte aus Satellitenbild und Straßenkarte, mit einer transparenten Ebene auf dem Satellitenbild zur Einblendung großer Straßen und Plätze.
url << "&maptype=#{h(options[:maptype])}" if options[:maptype]
options.delete(:maptype)
# Image format can be jpeg, jpeg-baseline, png8, png32, gif
url << "&format=#{options[:format] ? h(options[:format]) : 'png32'}"
# Options we dont need and use and that are not checked against the latest documentation. They come from BillyIII's Code.
#url << '&center=' + options[:center]
#url << '&span=' + options[:span]
# Default is no zoom-level which means autozoom. Zoomlevel are from 1 to 19
#url << '&zoom=' + options[:zoom]
#url << '&hl=' +options[:language]
options[:src] = url
if size = options.delete(:size)
options[:width], options[:height] = size.split("x") if size =~ %r{^\d+x\d+$}
end
tag("img", options)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment