Module: GoogleStaticMapsHelper

Defined in:
lib/google_static_maps_helper.rb,
lib/google_static_maps_helper/map.rb,
lib/google_static_maps_helper/path.rb,
lib/google_static_maps_helper/marker.rb,
lib/google_static_maps_helper/location.rb,
lib/google_static_maps_helper/gmap_polyline_encoder.rb

Overview

Utility for creating Google Maps Encoded GPolylines

License: You may distribute this code under the same terms as Ruby itself

Author: Joel Rosenberg

( Drawing from the official example pages as well as Mark McClure’s work )

Example

data = [
  [ 37.4419, -122.1419],
  [ 37.4519, -122.1519],
  [ 37.4619, -122.1819],
]

encoder = GMapPolylineEncoder.new()
result = encoder.encode( data )

javascript << "  var myLine = new GPolyline.fromEncoded({\n"
javascript << "     color: \"#FF0000\",\n"
javascript << "     weight: 10,\n"
javascript << "     opacity: 0.5,\n"
javascript << "     zoomFactor: #{result[:zoomFactor]},\n"
javascript << "     numLevels: #{result[:numLevels]},\n"
javascript << "     points: \"#{result[:points]}\",\n"
javascript << "     levels: \"#{result[:levels]}\"\n"
javascript << "  });"

Methods

Constructor args (all optional): 
  :numLevels (default 18)
  :zoomFactor (default 2)
  :reduce: Reduce points (default true)
  :escape: Escape backslashes (default true)

encode( points ) method
  points (required): array of longitude, latitude pairs

  returns hash with keys :points, :levels, :zoomFactor, :numLevels

Background

Description: www.google.com/apis/maps/documentation/#Encoded_Polylines API: www.google.com/apis/maps/documentation/reference.html#GPolyline Hints: www.google.com/apis/maps/documentation/polylinealgorithm.html

Example Javascript for instantiating an encoded polyline: var encodedPolyline = new GPolyline.fromEncoded(

color: "#FF0000",
weight: 10,
points: "yzocFzynhVq@n}@o}@nzD",
levels: "BBB",
zoomFactor: 32,
numLevels: 4

});

Changes

06.29.2007 - Release 0.1

Profiling showed that distance() accounted for 50% of the time when
processing McClure's British coast data. By moving the distance
calculation into encode(), we can cache a few of the calculations
(magnitude) and eliminate the overhead of the function call. This
reduced the time to encode by ~ 30%

06.21.2007 Implementing the Doublas-Peucker algorithm for removing superflous

points as per Mark McClure's design:
    http://facstaff.unca.edu/mcmcclur/GoogleMaps/EncodePolyline/

10.14.2006 Cleaned up (and finally grasped) zoom levels

09.2006 First port of the official example’s javascript. Ignoring zoom

levels for now, showing points at all zoom levels

++

Defined Under Namespace

Classes: BuildDataMissing, GMapPolylineEncoder, Location, Map, Marker, OptionMissing, OptionNotExist, Path, UnsupportedFormat, UnsupportedMaptype

Constant Summary collapse

API_URL =

The basic url to the API which we’ll build the URL from

'http://maps.google.com/maps/api/staticmap'

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.keyObject

Returns the value of attribute key.



33
34
35
# File 'lib/google_static_maps_helper.rb', line 33

def key
  @key
end

.sensorObject

Returns the value of attribute sensor.



33
34
35
# File 'lib/google_static_maps_helper.rb', line 33

def sensor
  @sensor
end

.sizeObject

Returns the value of attribute size.



33
34
35
# File 'lib/google_static_maps_helper.rb', line 33

def size
  @size
end

Class Method Details

.url_for(map_options = {}, &block) ⇒ Object

Provides a simple DSL stripping away the need of manually instantiating classes

Usage:

# First of all, you might want to set your key etc
GoogleStaticMapsHelper.key = 'your google key'
GoogleStaticMapsHelper.size = '300x600'
GoogleStaticMapsHelper.sensor = false

# Then, you'll be able to do:
url = GoogleStaticMapsHelper.url_for do
  marker :lng => 1, :lat => 2
  marker :lng => 3, :lat => 4
  path {:lng => 5, :lat => 6}, {:lng => 7, :lat => 7}
end

# You can send in key, size etc to url_for
url = GoogleStaticMapsHelper.url_for(:key => 'your_key', :size => [300, 600]) do
  # ...
end

# If you need to, the map object is yielded to the block, so you can do:
url = GoogleStaticMapsHelper.url_for do |map|
  map.marker object_which_responds_to_lng_lat
end


62
63
64
65
66
# File 'lib/google_static_maps_helper.rb', line 62

def url_for(map_options = {}, &block)
  map = Map.new(map_options)
  block.arity < 1 ? map.instance_eval(&block) : block.call(map)
  map.url
end