ym4r 0.2.1 → 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
data/README CHANGED
@@ -1,12 +1,18 @@
1
1
  =YM4R
2
- This is YM4R 0.2.1. The goal of YM4R (which naturally means Yellow Maps For Ruby...) is to ease the use of the Google Maps and Yahoo! Maps API's (including the Geocoding, Map Image, Traffic and Local Search API's) from Ruby and Rails.
2
+ This is YM4R 0.2.2. The goal of YM4R (which naturally means Yellow Maps For Ruby...) is to ease the use of the Google Maps (including the Geocoding API) and Yahoo! Maps API's (including the Geocoding, Map Image, Traffic and Local Search API's) from Ruby and Rails.
3
3
 
4
4
  ==Operations
5
5
  ===Google Maps
6
6
  You can use the library to display Google maps easily with any ruby-based web framework. The version of the API used is v2. The library is engineered so updates to the map through Ajax are possible. I have made available 2 in-depth tutorials to show some of the functionalities of the library and how it can be integrated with GeoRuby and the Spatial Adapter for Rails:
7
7
  - http://thepochisuperstarmegashow.com/2006/06/02/ym4r-georuby-spatial-adapter-demo/
8
8
  - http://thepochisuperstarmegashow.com/2006/06/03/google-maps-yahoo-traffic-mash-up/
9
- Following is some notes about using the library:
9
+ For the Geocoding API, see the section after this one.
10
+ Following is some notes about manipulating Google Maps with the library:
11
+
12
+ ====Loading the library
13
+ You should load the Google Maps part of YM4R this way:
14
+ require 'ym4r/google_maps'
15
+ include Ym4r::GoogleMaps
10
16
 
11
17
  ====Naming conventions
12
18
  The names of the Ruby class follow the ones in the JavaScript Google Maps API v2, except for GMap2, which in Ruby code is called simply GMap. To know what is possible to do with each class, you should refer to the documentation available on Google website.
@@ -80,9 +86,9 @@ Finally to add a map type to a GMap:
80
86
  If you want to wipe out the existing map types (for example the 3 default ones), you can add a +false+ argument to the +map_type_init+ method and the +map_type+ will be the only one.
81
87
 
82
88
 
83
- ====How to generated tiles for use in pretiled layers
89
+ ====How to generate tiles for use in pretiled layers
84
90
 
85
- The YM4R library provides 2 tools to generate tiles. They are in the +tools+ directory of the YM4R distribution.
91
+ The YM4R library provides 2 tools to generate tiles. They are in the +tools+ directory of the YM4R distribution. These tools can naturally be used to generate tiles even if you don't use the YM4R library to manipulate your Google Maps. You will just have to write the code to fetch the tiles by yourself.
86
92
 
87
93
  =====tile_wms.rb
88
94
  This is to generate tiles from an already existing WMS server. It can be useful if you don't want to setup a permanent WMS server but still want to display your geographic data files (any format compatible with your WMS server can then be used). It can also be used to cache data from public servers, which can sometimes be very slow to answer requests. Run "ruby tile_wms.rb" to know what the options are. These are very similar to the ones passed to the WMSTiler constructor, although the <tt>-g</tt> (<tt>--gmap-setting</tt>) needs to be explained in more details. It uses the data that comes from this tool: http://www.onnyturf.com/google/latlontotile.html. Basically you need to center the map at the zoom you need to get your desired extent. When you are satisfied, the tool will give you the following data: the X and Y values of the upper left corner tile, 17 minus the zoom level (this is because the V2 of the Google Maps API reversed the zoom order), the number of horizontal and vertical tiles that you want. You will then pass to the <tt>-g</tt> options 5 integers in the order previously described, for example:
@@ -107,6 +113,18 @@ Here is an example of command line execution using the image at http://open.atla
107
113
  ruby tile_image.rb -o ./tiles -z 13..14 -p 2503,3667,13,149,76,1.0 ./input/*
108
114
  This will create tiles of the image for zoom levels 13 and 14. It should take just a few seconds.
109
115
 
116
+ ===Google Maps Geocoding API
117
+ Note that you have to require it separately from the Google Maps API described above.
118
+ Here is an example of request:
119
+ require 'ym4r/google_maps/geocoding'
120
+ include Ym4r::GoogleMaps
121
+ results = Geocoding::get("Rue Clovis Paris")
122
+ +results+ is an array of Geocoding::Placemark objects, with 2 additional attributes: +status+ and +name+. You should check if +status+ equals <tt>Geocoding::GEO_SUCCESS</tt> to know if the request has been successful. You can access the various data elements like this:
123
+ if results.status == Geocoding::GEO_SUCCESS
124
+ a = results[0].address
125
+ b = results[0].latlon
126
+ end
127
+
110
128
  ===Yahoo Maps Building Block API
111
129
  Building Block API's (Geocoding, Map Image, Traffic and Local Search) are supported. You have to pass to the +get+ method of the module a hash whose keys are a rubyfied version of the request parameters detailed in the documentation for these API's. You get back a ruby object, with accessors that let you get the returned data in a easy way. You get an exception if not all the parameters have been passed, if the connection to the service could not be made or if the parameters you have passed are of the incorrect value.
112
130
 
@@ -115,8 +133,8 @@ To know what parameters to pass to the +get+ methods and what results you should
115
133
  Here are some examples and notes about using the different Building Block API's.
116
134
  ====Geocoding
117
135
  Here is an example of request:
118
- require 'ym4r'
119
- include Ym4r::BuildingBlock
136
+ require 'ym4r/yahoo_maps/building_block'
137
+ include Ym4r::YahooMaps::BuildingBlock
120
138
  results = Geocoding::get(:street => "1 Infinite Loop",
121
139
  :city => "Cupertino",
122
140
  :state => "CA",
@@ -125,8 +143,8 @@ Here is an example of request:
125
143
 
126
144
  ====Map Image
127
145
  Here is an example of request:
128
- require 'ym4r'
129
- include Ym4r::BuildingBlock
146
+ require 'ym4r/yahoo_maps/building_block'
147
+ include Ym4r::YahooMaps::BuildingBlock
130
148
  result = MapImage::get(:street => "1 Infinite Loop",
131
149
  :city => "Cupertino",
132
150
  :state => "CA",
@@ -136,8 +154,8 @@ Here is an example of request:
136
154
 
137
155
  ====Traffic
138
156
  Here is an example of request:
139
- require 'ym4r'
140
- include Ym4r::BuildingBlock
157
+ require 'ym4r/yahoo_maps/building_block'
158
+ include Ym4r::YahooMaps::BuildingBlock
141
159
  results = Traffic::get(:street => "1 Infinite Loop",
142
160
  :city => "Cupertino",
143
161
  :state => "CA",
@@ -147,8 +165,8 @@ Here is an example of request:
147
165
 
148
166
  ====Local Search
149
167
  Here is an example of request:
150
- require 'ym4r'
151
- include Ym4r::BuildingBlock
168
+ require 'ym4r/yahoo_maps/building_block'
169
+ include Ym4r::YahooMaps::BuildingBlock
152
170
  results = LocalSearch::get(:street => "1 Infinite Loop",
153
171
  :city => "Cupertino",
154
172
  :state => "CA",
@@ -160,14 +178,13 @@ Here is an example of request:
160
178
  Preliminary support for this API has been added. It works along the same lines as with the Google Maps API but it is not very polished currently.
161
179
 
162
180
  ==Changes since last version
163
- - Addition of support for user-defined layers (WMS and pretiled layers)
164
- - Addition of tools to generate tiles to be used with the pretiled layers
181
+ - Addition of a utility class to use the Google Maps API
165
182
 
166
183
  ==TODO
167
184
  - Add support for easy manipulation of external Google Maps-related libraries: Advanced tooltip manipulation, GeoRSS, Clusterer...
185
+ - Add support to show and hide groups of overlays easily
168
186
  - Documentation! Documentation! Documentation!
169
187
  - Tutorials
170
- - Finish the support for the Yahoo! Maps JS-Flash API
171
188
 
172
189
  ==Disclaimer
173
190
  This software is not endorsed in any way by Yahoo! or Google.
@@ -0,0 +1 @@
1
+ require 'ym4r/google_maps/geocoding/geocoding'
@@ -0,0 +1,78 @@
1
+ require 'ym4r/google_maps/api_key'
2
+ require 'open-uri'
3
+ require 'rexml/document'
4
+
5
+ module Ym4r
6
+ module GoogleMaps
7
+ module Geocoding
8
+
9
+ GEO_SUCCESS = 200
10
+ GEO_MISSING_ADDRESS = 601
11
+ GEO_UNKNOWN_ADDRESS = 602
12
+ GEO_UNAVAILABLE_ADDRESS = 603
13
+ GEO_BAD_KEY = 610
14
+ GEO_TOO_MANY_QUERIES = 620
15
+ GEO_SERVER_ERROR = 500
16
+
17
+ #Gets placemarks by querying the Google Maps Geocoding service with the +request+ string
18
+ def self.get(request)
19
+
20
+ url = "http://maps.google.com/maps/geo?q=#{request}&key=#{API_KEY}&output=xml"
21
+ begin
22
+ xml = open(URI.encode(url)).read
23
+ rescue
24
+ raise ConnectionException.new("Unable to connect to Google Maps Geocoding service")
25
+ end
26
+
27
+ doc = REXML::Document.new(xml)
28
+
29
+ response = doc.elements['//Response']
30
+ placemarks = Placemarks.new(response.elements['name'].text,response.elements['Status/code'].text.to_i)
31
+ response.elements.each("Placemark") do |placemark|
32
+ data = placemark.elements
33
+ data_country = data['//CountryNameCode']
34
+ data_administrative = data['//AdministrativeAreaName']
35
+ data_sub_administrative = data['//SubAdministrativeAreaName']
36
+ data_locality = data['//LocalityName']
37
+ data_thoroughfare = data['//ThoroughfareName']
38
+ data_postal_code = data['//PostalCodeNumber']
39
+ placemarks << Geocoding::Placemark.new(data['address'].text,
40
+ data_country.nil? ? "" : data_country.text,
41
+ data_administrative.nil? ? "" : data_administrative.text,
42
+ data_sub_administrative.nil? ? "" : data_sub_administrative.text,
43
+ data_locality.nil? ? "" : data_locality.text,
44
+ data_thoroughfare.nil? ? "" : data_thoroughfare.text,
45
+ data_postal_code.nil? ? "" : data_postal_code.text,
46
+ *(data['//coordinates'].text.split(",")[0..1].collect {|l| l.to_f }))
47
+ end
48
+ placemarks
49
+ end
50
+
51
+ #Group of placemarks returned by the Geocoding service. If the result is valid the +statius+ attribute should be equal to <tt>Geocoding::GEI_SUCCESS</tt>
52
+ class Placemarks < Array
53
+ attr_accessor :name,:status
54
+
55
+ def initialize(name,status)
56
+ super(0)
57
+ @name = name
58
+ @status = status
59
+ end
60
+ end
61
+
62
+ #A result from the Geocoding service.
63
+ class Placemark < Struct.new(:address,:country_code,:administrative_area,:sub_administrative_area,:locality,:thoroughfare,:postal_code,:longitude,:latitude)
64
+ def lonlat
65
+ [longitude,latitude]
66
+ end
67
+
68
+ def latlon
69
+ [latitude,longitude]
70
+ end
71
+ end
72
+
73
+ #Raised when the connection to the service is not possible
74
+ class ConnectionException < StandardError
75
+ end
76
+ end
77
+ end
78
+ end
data/rakefile.rb CHANGED
@@ -24,7 +24,7 @@ spec = Gem::Specification::new do |s|
24
24
  s.platform = Gem::Platform::RUBY
25
25
 
26
26
  s.name = 'ym4r'
27
- s.version = "0.2.1"
27
+ s.version = "0.2.2"
28
28
  s.summary = "Using Google Maps and Yahoo! Maps from Ruby and Rails"
29
29
  s.description = <<EOF
30
30
  EOF
@@ -1,6 +1,7 @@
1
1
  $:.unshift(File.dirname(__FILE__) + '/../lib')
2
2
 
3
3
  require 'ym4r/google_maps'
4
+ require 'ym4r/google_maps/geocoding'
4
5
  require 'test/unit'
5
6
 
6
7
  include Ym4r::GoogleMaps
@@ -50,5 +51,25 @@ class TestGoogleMaps< Test::Unit::TestCase
50
51
  obj = Variable.new("obj")
51
52
  assert_equal("obj[0]",obj[0].variable)
52
53
  end
54
+
55
+ def test_google_maps_geocoding
56
+ placemarks = Geocoding.get("Rue Clovis Paris")
57
+ assert_equal(Geocoding::GEO_SUCCESS,placemarks.status)
58
+ assert_equal(1,placemarks.length)
59
+ placemark = placemarks[0]
60
+ assert_equal("FR",placemark.country_code)
61
+ assert_equal("Paris",placemark.locality)
62
+ assert_equal("75005",placemark.postal_code)
63
+ end
64
+
65
+ def test_google_maps_pakistan
66
+ placemarks = Geocoding.get("Lahore PK")
67
+ assert_equal(Geocoding::GEO_SUCCESS,placemarks.status)
68
+ assert_equal(1,placemarks.length)
69
+ placemark = placemarks[0]
70
+ assert_equal("PK",placemark.country_code)
71
+ assert_equal("Lahore",placemark.locality)
72
+ assert_equal("",placemark.thoroughfare)
73
+ end
53
74
 
54
75
  end
metadata CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.8.11
3
3
  specification_version: 1
4
4
  name: ym4r
5
5
  version: !ruby/object:Gem::Version
6
- version: 0.2.1
7
- date: 2006-06-12 00:00:00 +05:00
6
+ version: 0.2.2
7
+ date: 2006-06-13 00:00:00 +05:00
8
8
  summary: Using Google Maps and Yahoo! Maps from Ruby and Rails
9
9
  require_paths:
10
10
  - lib
@@ -33,12 +33,14 @@ files:
33
33
  - lib/ym4r/yahoo_maps.rb
34
34
  - lib/ym4r/google_maps/api_key.rb
35
35
  - lib/ym4r/google_maps/control.rb
36
+ - lib/ym4r/google_maps/geocoding.rb
36
37
  - lib/ym4r/google_maps/helper.rb
37
38
  - lib/ym4r/google_maps/layer.rb
38
39
  - lib/ym4r/google_maps/map.rb
39
40
  - lib/ym4r/google_maps/mapping.rb
40
41
  - lib/ym4r/google_maps/overlay.rb
41
42
  - lib/ym4r/google_maps/point.rb
43
+ - lib/ym4r/google_maps/geocoding/geocoding.rb
42
44
  - lib/ym4r/google_maps/tiler/image_tiler.rb
43
45
  - lib/ym4r/google_maps/tiler/wms_tiler.rb
44
46
  - lib/ym4r/yahoo_maps/app_id.rb