|
1 | 1 | # -*- coding: utf-8 -*- |
2 | 2 | # |
3 | 3 | # google_map.rb - embeded Google Map for tDiary, use Google Maps JavaScript API V3. |
4 | | -# http://code.google.com/intl/ja/apis/maps/documentation/v3/services.html |
| 4 | +# https://developers.google.com/maps/documentation/javascript/tutorial |
5 | 5 | # |
6 | 6 | # Copyright (C) 2010, tamoot <tamoot+tdiary@gmail.com> |
7 | 7 | # You can redistribute it and/or modify it under GPL2. |
8 | 8 | # |
9 | 9 |
|
10 | 10 | def google_map(lat, lon, params = {}) |
11 | | - params.merge!(:lat => lat, :lon => lon) |
12 | | - google_map_common(params) |
| 11 | + params.merge!(:lat => lat, :lon => lon) |
| 12 | + google_map_common(params) |
13 | 13 | end |
14 | 14 |
|
15 | 15 | def google_geomap(address, params = {}) |
16 | | - params.merge!(:address => address) |
17 | | - google_map_common(params) |
| 16 | + params.merge!(:address => address) |
| 17 | + google_map_common(params) |
18 | 18 | end |
19 | 19 |
|
20 | 20 | def google_map_common(params) |
21 | | - params[:id] ||= '' |
22 | | - params[:lat] ||= 0.0 |
23 | | - params[:lon] ||= 0.0 |
24 | | - params[:address] ||= nil |
25 | | - params[:zoom] ||= 10 |
26 | | - params[:html] ||= nil |
27 | | - params[:title] ||= nil |
28 | | - params[:width] ||= 320 |
29 | | - params[:height] ||= 240 |
30 | | - params[:type] ||= :ROADMAP |
31 | | - params[:overview]||= false |
32 | | - |
33 | | - if feed? |
34 | | - require 'cgi' |
35 | | - |
36 | | - url = nil |
37 | | - if params[:lat].nonzero? && params[:lon].nonzero? |
38 | | - query = "#{params[:lat]},#{params[:lon]}" |
39 | | - url = %Q|http://maps.google.com/maps?q=#{CGI::escape(query)}| |
40 | | - |
41 | | - elsif params[:address] != nil |
42 | | - query = params[:address] |
43 | | - url = %Q|http://maps.google.com/maps?q=#{CGI::escape(query)}| |
44 | | - |
45 | | - end |
46 | | - |
47 | | - return %Q|<a href="#{url}">#{url}</a>| if url |
48 | | - |
49 | | - end |
50 | | - |
51 | | - dom_id = "#{@gmap_date.strftime("%Y%m%d")}_#{@gmap_count}" |
52 | | - params.merge!(:id => dom_id) |
53 | | - @gmap_data << params |
54 | | - @gmap_count += 1 |
55 | | - |
56 | | - %Q|<div class="gmap" id="#{dom_id}" style="width : #{params[:width]}px; height : #{params[:height]}px;"></div>| |
| 21 | + params[:id] ||= '' |
| 22 | + params[:lat] ||= 0.0 |
| 23 | + params[:lon] ||= 0.0 |
| 24 | + params[:address] ||= nil |
| 25 | + params[:zoom] ||= 10 |
| 26 | + params[:html] ||= nil |
| 27 | + params[:title] ||= nil |
| 28 | + params[:width] ||= 320 |
| 29 | + params[:height] ||= 240 |
| 30 | + params[:type] ||= :ROADMAP |
| 31 | + params[:overview]||= false |
| 32 | + |
| 33 | + if feed? |
| 34 | + require 'cgi' |
| 35 | + |
| 36 | + url = nil |
| 37 | + if params[:lat].nonzero? && params[:lon].nonzero? |
| 38 | + query = "#{params[:lat]},#{params[:lon]}" |
| 39 | + url = %Q|https://maps.google.com/maps?q=#{CGI::escape(query)}| |
| 40 | + |
| 41 | + elsif params[:address] != nil |
| 42 | + query = params[:address] |
| 43 | + url = %Q|https://maps.google.com/maps?q=#{CGI::escape(query)}| |
| 44 | + |
| 45 | + end |
| 46 | + |
| 47 | + return %Q|<a href="#{url}">#{url}</a>| if url |
| 48 | + |
| 49 | + end |
| 50 | + |
| 51 | + dom_id = "#{@gmap_date.strftime("%Y%m%d")}_#{@gmap_count}" |
| 52 | + params.merge!(:id => dom_id) |
| 53 | + @gmap_data << params |
| 54 | + @gmap_count += 1 |
| 55 | + |
| 56 | + %Q|<div class="gmap" id="#{dom_id}" style="width : #{params[:width]}px; height : #{params[:height]}px;"></div>| |
57 | 57 | end |
58 | 58 |
|
59 | 59 | add_header_proc do |
60 | | - if /\A(?:latest|day|month|nyear|preview)\z/ =~ @mode |
61 | | - %Q|<script type="text/javascript" src="//maps.google.com/maps/api/js?sensor=true"></script>\n| |
62 | | - end |
| 60 | + if /\A(?:latest|day|month|nyear|preview)\z/ =~ @mode |
| 61 | + %Q|<script type="text/javascript" src="//maps.google.com/maps/api/js?key=#{@conf['google_map.key']}"></script>\n| |
| 62 | + end |
63 | 63 | end |
64 | 64 |
|
65 | 65 | add_body_enter_proc do |date| |
66 | | - @gmap_data = [] |
67 | | - @gmap_date = date |
68 | | - @gmap_count = 0 |
69 | | - '' |
| 66 | + @gmap_data = [] |
| 67 | + @gmap_date = date |
| 68 | + @gmap_count = 0 |
| 69 | + '' |
70 | 70 | end |
71 | 71 |
|
72 | 72 | add_body_leave_proc do |date| |
73 | | - gmap_scripts = '' |
74 | | - if !feed? && @gmap_data.any? |
75 | | - gmap_scripts = %Q|<script type="text/javascript">\n<!--\n| |
76 | | - @gmap_data.each do |data| |
77 | | - if data[:address] |
78 | | - gmap_scripts << google_geomap_script(data) |
79 | | - else |
80 | | - gmap_scripts << google_map_script(data) |
81 | | - end |
82 | | - end |
83 | | - gmap_scripts << %Q|//-->\n</script>\n| |
84 | | - end |
85 | | - gmap_scripts |
| 73 | + gmap_scripts = '' |
| 74 | + if !feed? && @gmap_data.any? |
| 75 | + gmap_scripts = %Q|<script type="text/javascript">\n<!--\n| |
| 76 | + @gmap_data.each do |data| |
| 77 | + if data[:address] |
| 78 | + gmap_scripts << google_geomap_script(data) |
| 79 | + else |
| 80 | + gmap_scripts << google_map_script(data) |
| 81 | + end |
| 82 | + end |
| 83 | + gmap_scripts << %Q|//-->\n</script>\n| |
| 84 | + end |
| 85 | + gmap_scripts |
| 86 | +end |
| 87 | + |
| 88 | +add_conf_proc('google_map', 'Google Maps', 'etc') do |
| 89 | + if @mode == 'saveconf' then |
| 90 | + @conf['google_map.key'], = @cgi.params['google_map.key'] |
| 91 | + end |
| 92 | + |
| 93 | + <<-HTML |
| 94 | + <h3 class="subtitle">Google Maps API Key</h3> |
| 95 | + <p>API Key (see <a href="https://developers.google.com/maps/documentation/javascript/get-api-key">developers.google.com/maps</a>)</p> |
| 96 | + <p><input name="google_map.key" value="#{h @conf['google_map.key']}" size="50"></p> |
| 97 | + HTML |
86 | 98 | end |
87 | 99 |
|
88 | 100 | def google_map_script(hash) |
89 | | - str = '' |
90 | | - str << %Q|google.maps.event.addDomListener(window, 'load', function() {\n| |
91 | | - str << %Q| var mapdiv = document.getElementById("#{hash[:id]}");\n| |
92 | | - str << %Q| if(mapdiv){\n| |
93 | | - str << %Q| var myOptions = {\n| |
94 | | - str << %Q| zoom: #{hash[:zoom]},\n| |
95 | | - str << %Q| overviewMapControl: #{hash[:overview]},\n| |
96 | | - str << %Q| overviewMapControlOptions: {\n| |
97 | | - str << %Q| opened: #{hash[:overview]}\n| |
98 | | - str << %Q| },\n| |
99 | | - str << %Q| center: new google.maps.LatLng(#{hash[:lat]}, #{hash[:lon]}),\n| |
100 | | - str << %Q| mapTypeId: google.maps.MapTypeId.#{hash[:type]},\n| |
101 | | - str << %Q| scaleControl: true\n| |
102 | | - str << %Q| };\n| |
103 | | - str << %Q| var gMap = new google.maps.Map(mapdiv, myOptions);\n| |
104 | | - # set Marker |
105 | | - if hash[:title] |
106 | | - str << %Q| var marker = new google.maps.Marker({\n| |
107 | | - str << %Q| position: new google.maps.LatLng(#{hash[:lat]}, #{hash[:lon]}),\n| |
108 | | - str << %Q| map: gMap,\n| |
109 | | - str << %Q| title: '#{hash[:title]}'\n| |
110 | | - str << %Q| });\n| |
111 | | - # set InfoWindow |
112 | | - if hash[:html] |
113 | | - str << %Q| var infowindow = new google.maps.InfoWindow({\n| |
114 | | - str << %Q| content: '<span style="color: #000000;">#{hash[:html]}</span>',\n| |
115 | | - str << %Q| size: new google.maps.Size(350, 200)\n| |
116 | | - str << %Q| });\n| |
117 | | - str << %Q| infowindow.open(gMap, marker);\n| |
118 | | - end # :html |
119 | | - end # :title |
120 | | - str << %Q| };\n| |
121 | | - str << %Q|});\n| |
122 | | - |
123 | | - str |
| 101 | + str = '' |
| 102 | + str << %Q|google.maps.event.addDomListener(window, 'load', function() {\n| |
| 103 | + str << %Q| var mapdiv = document.getElementById("#{hash[:id]}");\n| |
| 104 | + str << %Q| if(mapdiv){\n| |
| 105 | + str << %Q| var myOptions = {\n| |
| 106 | + str << %Q| zoom: #{hash[:zoom]},\n| |
| 107 | + str << %Q| overviewMapControl: #{hash[:overview]},\n| |
| 108 | + str << %Q| overviewMapControlOptions: {\n| |
| 109 | + str << %Q| opened: #{hash[:overview]}\n| |
| 110 | + str << %Q| },\n| |
| 111 | + str << %Q| center: new google.maps.LatLng(#{hash[:lat]}, #{hash[:lon]}),\n| |
| 112 | + str << %Q| mapTypeId: google.maps.MapTypeId.#{hash[:type]},\n| |
| 113 | + str << %Q| scaleControl: true\n| |
| 114 | + str << %Q| };\n| |
| 115 | + str << %Q| var gMap = new google.maps.Map(mapdiv, myOptions);\n| |
| 116 | + # set Marker |
| 117 | + if hash[:title] |
| 118 | + str << %Q| var marker = new google.maps.Marker({\n| |
| 119 | + str << %Q| position: new google.maps.LatLng(#{hash[:lat]}, #{hash[:lon]}),\n| |
| 120 | + str << %Q| map: gMap,\n| |
| 121 | + str << %Q| title: '#{hash[:title]}'\n| |
| 122 | + str << %Q| });\n| |
| 123 | + # set InfoWindow |
| 124 | + if hash[:html] |
| 125 | + str << %Q| var infowindow = new google.maps.InfoWindow({\n| |
| 126 | + str << %Q| content: '<span style="color: #000000;">#{hash[:html]}</span>',\n| |
| 127 | + str << %Q| size: new google.maps.Size(350, 200)\n| |
| 128 | + str << %Q| });\n| |
| 129 | + str << %Q| infowindow.open(gMap, marker);\n| |
| 130 | + end # :html |
| 131 | + end # :title |
| 132 | + str << %Q| };\n| |
| 133 | + str << %Q|});\n| |
| 134 | + |
| 135 | + str |
124 | 136 | end |
125 | 137 |
|
126 | 138 | def google_geomap_script(hash) |
127 | | - str = '' |
128 | | - str << %Q|google.maps.event.addDomListener(window, 'load', function() {\n| |
129 | | - str << %Q| var mapdiv = document.getElementById("#{hash[:id]}");\n| |
130 | | - str << %Q| if(mapdiv){\n| |
131 | | - str << %Q| var geocoder = new google.maps.Geocoder();\n| |
132 | | - str << %Q| if(geocoder) {\n| |
133 | | - str << %Q| geocoder.geocode( { 'address': '#{hash[:address]}'}, function(results, status) {\n| |
134 | | - str << %Q| if (status == google.maps.GeocoderStatus.OK) {\n| |
135 | | - str << %Q| var geoLat = results[0].geometry.location;\n| |
136 | | - str << %Q| var myOptions = {\n| |
137 | | - str << %Q| zoom: #{hash[:zoom]},\n| |
138 | | - str << %Q| overviewMapControl: #{hash[:overview]},\n| |
139 | | - str << %Q| overviewMapControlOptions: {\n| |
140 | | - str << %Q| opened: #{hash[:overview]}\n| |
141 | | - str << %Q| },\n| |
142 | | - str << %Q| center: geoLat,\n| |
143 | | - str << %Q| mapTypeId: google.maps.MapTypeId.#{hash[:type]},\n| |
144 | | - str << %Q| scaleControl: true\n| |
145 | | - str << %Q| };\n| |
146 | | - str << %Q| var gMap = new google.maps.Map(mapdiv, myOptions);\n| |
147 | | - # set Marker |
148 | | - if hash[:title] |
149 | | - str << %Q| var marker = new google.maps.Marker({\n| |
150 | | - str << %Q| position: geoLat,\n| |
151 | | - str << %Q| map: gMap,\n| |
152 | | - str << %Q| title: '#{hash[:title]}'\n| |
153 | | - str << %Q| });\n| |
154 | | - # set InfoWindow |
155 | | - if hash[:html] |
156 | | - str << %Q| var infowindow = new google.maps.InfoWindow({\n| |
157 | | - str << %Q| content: '<span style="color: #000000;">#{hash[:html]}</span>',\n| |
158 | | - str << %Q| size: new google.maps.Size(350, 200)\n| |
159 | | - str << %Q| });\n| |
160 | | - str << %Q| infowindow.open(gMap, marker);\n| |
161 | | - end # :html |
162 | | - end # :title |
163 | | - str << %Q| }else{\n| |
164 | | - str << %Q| alert("Geocode was not successful for the following reason: " + status)\n| |
165 | | - str << %Q| }\n| |
166 | | - str << %Q| });\n| |
167 | | - str << %Q| }\n| |
168 | | - str << %Q| }\n| |
169 | | - str << %Q|});\n| |
170 | | - |
171 | | - str |
| 139 | + str = '' |
| 140 | + str << %Q|google.maps.event.addDomListener(window, 'load', function() {\n| |
| 141 | + str << %Q| var mapdiv = document.getElementById("#{hash[:id]}");\n| |
| 142 | + str << %Q| if(mapdiv){\n| |
| 143 | + str << %Q| var geocoder = new google.maps.Geocoder();\n| |
| 144 | + str << %Q| if(geocoder) {\n| |
| 145 | + str << %Q| geocoder.geocode( { 'address': '#{hash[:address]}'}, function(results, status) {\n| |
| 146 | + str << %Q| if (status == google.maps.GeocoderStatus.OK) {\n| |
| 147 | + str << %Q| var geoLat = results[0].geometry.location;\n| |
| 148 | + str << %Q| var myOptions = {\n| |
| 149 | + str << %Q| zoom: #{hash[:zoom]},\n| |
| 150 | + str << %Q| overviewMapControl: #{hash[:overview]},\n| |
| 151 | + str << %Q| overviewMapControlOptions: {\n| |
| 152 | + str << %Q| opened: #{hash[:overview]}\n| |
| 153 | + str << %Q| },\n| |
| 154 | + str << %Q| center: geoLat,\n| |
| 155 | + str << %Q| mapTypeId: google.maps.MapTypeId.#{hash[:type]},\n| |
| 156 | + str << %Q| scaleControl: true\n| |
| 157 | + str << %Q| };\n| |
| 158 | + str << %Q| var gMap = new google.maps.Map(mapdiv, myOptions);\n| |
| 159 | + # set Marker |
| 160 | + if hash[:title] |
| 161 | + str << %Q| var marker = new google.maps.Marker({\n| |
| 162 | + str << %Q| position: geoLat,\n| |
| 163 | + str << %Q| map: gMap,\n| |
| 164 | + str << %Q| title: '#{hash[:title]}'\n| |
| 165 | + str << %Q| });\n| |
| 166 | + # set InfoWindow |
| 167 | + if hash[:html] |
| 168 | + str << %Q| var infowindow = new google.maps.InfoWindow({\n| |
| 169 | + str << %Q| content: '<span style="color: #000000;">#{hash[:html]}</span>',\n| |
| 170 | + str << %Q| size: new google.maps.Size(350, 200)\n| |
| 171 | + str << %Q| });\n| |
| 172 | + str << %Q| infowindow.open(gMap, marker);\n| |
| 173 | + end # :html |
| 174 | + end # :title |
| 175 | + str << %Q| }else{\n| |
| 176 | + str << %Q| alert("Geocode was not successful for the following reason: " + status)\n| |
| 177 | + str << %Q| }\n| |
| 178 | + str << %Q| });\n| |
| 179 | + str << %Q| }\n| |
| 180 | + str << %Q| }\n| |
| 181 | + str << %Q|});\n| |
| 182 | + |
| 183 | + str |
172 | 184 | end |
173 | 185 |
|
174 | 186 | # Local Variables: |
|
0 commit comments