Class: MapWKT::Map
- Inherits:
-
Object
- Object
- MapWKT::Map
- Defined in:
- lib/mapwkt/gmap/map.rb
Instance Method Summary collapse
- #<<(obj) ⇒ Object
- #center ⇒ Object
- #center=(center) ⇒ Object
- #id ⇒ Object
- #id=(id) ⇒ Object
-
#initialize(arg = {}) ⇒ Map
constructor
A new instance of Map.
- #js_initialize ⇒ Object
- #js_output(arg = {}) ⇒ Object
- #map_type_id ⇒ Object
- #map_type_id=(map_type_id) ⇒ Object
- #options ⇒ Object
- #options=(options) ⇒ Object
- #overlays ⇒ Object
- #refresh! ⇒ Object
- #zoom ⇒ Object
- #zoom=(zoom) ⇒ Object
Constructor Details
#initialize(arg = {}) ⇒ Map
Returns a new instance of Map.
38 39 40 41 42 43 44 45 46 |
# File 'lib/mapwkt/gmap/map.rb', line 38 def initialize (arg = {}) @overlays = [] self. = case arg when String then { id: arg } when Hash then arg else {} end end |
Instance Method Details
#<<(obj) ⇒ Object
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/mapwkt/gmap/map.rb', line 121 def << (obj) = case obj when MapWKT::Geometry then [MapWKT::Overlay.new(obj)] when MapWKT::Overlay then [obj] else raise(TypeError, "method `wkt' is not defined for #{obj.inspect}") unless obj.respond_to?(:wkt) MapWKT::Geometry.parse_wkt(obj.wkt).map do |g| { MapWKT::Geometry::Point => MapWKT::Overlay::Marker, MapWKT::Geometry::LineString => MapWKT::Overlay::Polyline, MapWKT::Geometry::Polygon => MapWKT::Overlay::Polygon }[g.class].new(g, obj) end end self..push(*) self end |
#center ⇒ Object
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/mapwkt/gmap/map.rb', line 4 def center return @center if @center return if self..empty? n, s, e, w = nil self..each do |o| n = o.center.y if !n || o.center.y > n s = o.center.y if !s || o.center.y < s e = o.center.x if !e || o.center.x > e w = o.center.x if !w || o.center.x < w end x = (e + w) / 2 y = (n + s) / 2 @center = MapWKT::Geometry::Point.new(y, x) end |
#center=(center) ⇒ Object
23 24 25 26 |
# File 'lib/mapwkt/gmap/map.rb', line 23 def center= (center) return unless MapWKT::Geometry::Point === center @center = center end |
#id ⇒ Object
28 29 30 |
# File 'lib/mapwkt/gmap/map.rb', line 28 def id @id end |
#id=(id) ⇒ Object
32 33 34 35 36 |
# File 'lib/mapwkt/gmap/map.rb', line 32 def id= (id) return unless id raise(ArgumentError, "invalid HTML id: #{id.to_json}") unless id =~ /^[a-zA-Z][\w\d.:-]*$/ @id = id end |
#js_initialize ⇒ Object
48 49 50 |
# File 'lib/mapwkt/gmap/map.rb', line 48 def js_initialize "mapwkt_#{self.object_id}_initialize()" end |
#js_output(arg = {}) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/mapwkt/gmap/map.rb', line 52 def js_output (arg = {}) self. = case arg when String then { id: arg } when Hash then arg else {} end raise(ArgumentError, "HMTL id is required") unless self.id <<-JAVASCRIPT.gsub('new google.maps.LatLng','new p') function mapwkt_#{self.object_id}_initialize () { var p = google.maps.LatLng var element = document.getElementById('#{self.id}') var options = { center: new google.maps.LatLng(#{self.center.latitude_f},#{self.center.longitude_f}), mapTypeId: google.maps.MapTypeId.#{self.map_type_id}, zoom: #{self.zoom} } var map = new google.maps.Map(element, options) #{self..map {|obj| obj.js_output('map') }.join("\n ")} } JAVASCRIPT end |
#map_type_id ⇒ Object
73 74 75 |
# File 'lib/mapwkt/gmap/map.rb', line 73 def map_type_id @map_type_id ||= 'ROADMAP' end |
#map_type_id=(map_type_id) ⇒ Object
77 78 79 80 |
# File 'lib/mapwkt/gmap/map.rb', line 77 def map_type_id= (map_type_id) return unless map_type_id @map_type_id = ([map_type_id.to_s.upcase] & %w:HYBRID ROADMAP SATELLITE TERRAIN:).first || 'ROADMAP' end |
#options ⇒ Object
82 83 84 85 86 87 88 89 |
# File 'lib/mapwkt/gmap/map.rb', line 82 def { center: self.center, id: self.id, map_type_id: self.map_type_id, zoom: self.zoom } end |
#options=(options) ⇒ Object
91 92 93 94 95 96 97 |
# File 'lib/mapwkt/gmap/map.rb', line 91 def () .symbolize_keys! self.center = [:center] || self.center self.id = [:id] || self.id self.map_type_id = [:map_type_id] || [:mapTypeId] || self.map_type_id self.zoom = [:zoom] || self.zoom || 10 end |
#overlays ⇒ Object
99 100 101 102 |
# File 'lib/mapwkt/gmap/map.rb', line 99 def self.refresh! @overlays end |
#refresh! ⇒ Object
104 105 106 107 108 109 110 |
# File 'lib/mapwkt/gmap/map.rb', line 104 def refresh! return self if @refreshed @refreshed = true self..select! {|p| MapWKT::Overlay === p } self end |
#zoom ⇒ Object
112 113 114 |
# File 'lib/mapwkt/gmap/map.rb', line 112 def zoom @zoom ||= 10 end |
#zoom=(zoom) ⇒ Object
116 117 118 119 |
# File 'lib/mapwkt/gmap/map.rb', line 116 def zoom= (zoom) return unless zoom @zoom = [0,[zoom.to_i, 25].min].max end |