Class: BingLocator
- Inherits:
-
Object
- Object
- BingLocator
- Defined in:
- lib/location.rb
Overview
This class is a wrapper for the Bing Maps API (API Documentation) that makes it easy to get more information about a location or create maps.
Currently the class is built for dealing with one object (point on a map/location).
Example Usage
my_loc = BingLocator.new()
my_loc.api_key = "YOU-BING-MAPS-API-KEY"
my_loc.country_region = 'US'
my_loc.admin_district = 'PA'
my_loc.locality = 'Pittsburgh'
my_loc.address_line = '430 Atwood St'
my_loc.obj_type = 'json'
my_loc.get_obj_by_address
puts my_loc.object
my_loc = BingLocator.new()
my_loc.api_key = "YOU-BING-MAPS-API-KEY"
my_loc.query = 'University of Pittsburgh'
puts my_loc.get_img_url_by_query(800,600)
Instance Attribute Summary (collapse)
-
- (Object) address_line
Returns the value of attribute address_line.
-
- (Object) admin_district
Returns the value of attribute admin_district.
-
- (Object) admin_district_2
Returns the value of attribute admin_district_2.
-
- (Object) api_key
Returns the value of attribute api_key.
-
- (Object) confidence
Returns the value of attribute confidence.
-
- (Object) country_region
Returns the value of attribute country_region.
-
- (Object) formatted_address
Returns the value of attribute formatted_address.
-
- (Object) image
Returns the value of attribute image.
-
- (Object) latitude
Returns the value of attribute latitude.
-
- (Object) locality
Returns the value of attribute locality.
-
- (Object) longitude
Returns the value of attribute longitude.
-
- (Object) map_type
Returns the value of attribute map_type.
-
- (Object) name
Returns the value of attribute name.
-
- (Object) obj_type
Returns the value of attribute obj_type.
-
- (Object) object
Returns the value of attribute object.
-
- (Object) point
Returns the value of attribute point.
-
- (Object) postal_code
Returns the value of attribute postal_code.
-
- (Object) query
Returns the value of attribute query.
-
- (Object) show_traffic
Returns the value of attribute show_traffic.
-
- (Object) zoom_level
Returns the value of attribute zoom_level.
Instance Method Summary (collapse)
-
- (Object) get_img_by_point(width, height)
Uses the object's latitude and longitude to get an image file from the Bing API.
-
- (Object) get_img_by_query(width, height)
Uses the object's query to get an image file from the Bing API.
-
- (Object) get_img_url_by_point(width, height)
Uses the object's latitude and longitude to get an image url from the Bing API.
-
- (Object) get_img_url_by_query(width, height)
Uses the object's query to get an image url from the Bing API.
-
- (Object) get_obj_by_address
Uses the object's address to get a Location object from the Bing API.
-
- (Object) get_obj_by_point
Uses the object's latitude and longitude to get a Location object from the Bing API.
-
- (Object) get_obj_by_query
Uses the object's query to get a Location object from the Bing API.
- - (Object) parse_obj
-
- (Object) set_defaults
Makes assumptions for required fields that are not yet defined.
Instance Attribute Details
- (Object) address_line
Returns the value of attribute address_line
28 29 30 |
# File 'lib/location.rb', line 28 def address_line @address_line end |
- (Object) admin_district
Returns the value of attribute admin_district
28 29 30 |
# File 'lib/location.rb', line 28 def admin_district @admin_district end |
- (Object) admin_district_2
Returns the value of attribute admin_district_2
28 29 30 |
# File 'lib/location.rb', line 28 def admin_district_2 @admin_district_2 end |
- (Object) api_key
Returns the value of attribute api_key
28 29 30 |
# File 'lib/location.rb', line 28 def api_key @api_key end |
- (Object) confidence
Returns the value of attribute confidence
28 29 30 |
# File 'lib/location.rb', line 28 def confidence @confidence end |
- (Object) country_region
Returns the value of attribute country_region
28 29 30 |
# File 'lib/location.rb', line 28 def country_region @country_region end |
- (Object) formatted_address
Returns the value of attribute formatted_address
28 29 30 |
# File 'lib/location.rb', line 28 def formatted_address @formatted_address end |
- (Object) image
Returns the value of attribute image
28 29 30 |
# File 'lib/location.rb', line 28 def image @image end |
- (Object) latitude
Returns the value of attribute latitude
28 29 30 |
# File 'lib/location.rb', line 28 def latitude @latitude end |
- (Object) locality
Returns the value of attribute locality
28 29 30 |
# File 'lib/location.rb', line 28 def locality @locality end |
- (Object) longitude
Returns the value of attribute longitude
28 29 30 |
# File 'lib/location.rb', line 28 def longitude @longitude end |
- (Object) map_type
Returns the value of attribute map_type
28 29 30 |
# File 'lib/location.rb', line 28 def map_type @map_type end |
- (Object) name
Returns the value of attribute name
28 29 30 |
# File 'lib/location.rb', line 28 def name @name end |
- (Object) obj_type
Returns the value of attribute obj_type
28 29 30 |
# File 'lib/location.rb', line 28 def obj_type @obj_type end |
- (Object) object
Returns the value of attribute object
28 29 30 |
# File 'lib/location.rb', line 28 def object @object end |
- (Object) point
Returns the value of attribute point
28 29 30 |
# File 'lib/location.rb', line 28 def point @point end |
- (Object) postal_code
Returns the value of attribute postal_code
28 29 30 |
# File 'lib/location.rb', line 28 def postal_code @postal_code end |
- (Object) query
Returns the value of attribute query
28 29 30 |
# File 'lib/location.rb', line 28 def query @query end |
- (Object) show_traffic
Returns the value of attribute show_traffic
28 29 30 |
# File 'lib/location.rb', line 28 def show_traffic @show_traffic end |
- (Object) zoom_level
Returns the value of attribute zoom_level
28 29 30 |
# File 'lib/location.rb', line 28 def zoom_level @zoom_level end |
Instance Method Details
- (Object) get_img_by_point(width, height)
Uses the object's latitude and longitude to get an image file from the Bing API.
The file will be returned as a File object. To save the file use something like the following:
File.rename(my_loc.get_img_by_point(800,600).path,'map.jpeg')
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 |
# File 'lib/location.rb', line 168 def get_img_by_point(width,height) self.set_defaults return 'No latitude or longitude set for location' unless self.latitude && self.longitude # Build request URL params = "#{self.map_type}/#{self.latitude},#{self.longitude}/#{self.zoom_level}" params += "?mapSize=#{width},#{height}&pushpin=#{self.latitude},#{self.longitude}" params += "&mapLayer=TrafficFlow" if self.show_traffic == TRUE request_url = $img_base_url+params+"&key=#{self.api_key}" # REST request ! begin tmp = Tempfile.new('map_image.jpeg') tmp.write Net::HTTP.get_response(URI.parse(request_url)).body return tmp.flush rescue return FALSE end end |
- (Object) get_img_by_query(width, height)
Uses the object's query to get an image file from the Bing API.
The file will be returned as a File object. To save the file use something like the following:
File.rename(my_loc.get_img_by_query(800,600).path,'map.jpeg')
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/location.rb', line 121 def get_img_by_query(width,height) self.set_defaults return 'No query set for location' unless self.query query = self.query.gsub(' ','%20') # Build request URL params = "#{self.map_type}/#{query}?mapSize=#{width},#{height}" params += "&mapLayer=TrafficFlow" if self.show_traffic == TRUE request_url = $img_base_url+params+"&key=#{self.api_key}" # REST request ! begin tmp = Tempfile.new('map_image.jpeg') tmp.write Net::HTTP.get_response(URI.parse(request_url)).body return tmp.flush rescue return FALSE end end |
- (Object) get_img_url_by_point(width, height)
Uses the object's latitude and longitude to get an image url from the Bing API. Using the Bing URL is discouraged.
153 154 155 156 157 158 159 160 161 162 |
# File 'lib/location.rb', line 153 def get_img_url_by_point(width,height) self.set_defaults return 'No latitude or longitude set for location' unless self.latitude && self.longitude # Build request URL params = "#{self.map_type}/#{self.latitude},#{self.longitude}/#{self.zoom_level}" params += "?mapSize=#{width},#{height}&pushpin=#{self.latitude},#{self.longitude}" params += "&mapLayer=TrafficFlow" if self.show_traffic == TRUE request_url = $img_base_url+params+"&key=#{self.api_key}" return request_url end |
- (Object) get_img_url_by_query(width, height)
Uses the object's query to get an image url from the Bing API. Using the Bing URL is discouraged.
140 141 142 143 144 145 146 147 148 149 |
# File 'lib/location.rb', line 140 def get_img_url_by_query(width,height) self.set_defaults return 'No query set for location' unless self.query query = self.query.gsub(' ','%20') # Build request URL params = "#{self.map_type}/#{query}?mapSize=#{width},#{height}" params += "&mapLayer=TrafficFlow" if self.show_traffic == TRUE request_url = $img_base_url+params+"&key=#{self.api_key}" return request_url end |
- (Object) get_obj_by_address
Uses the object's address to get a Location object from the Bing API. The object contains a normalized address, extra information, and latitude, longitude information
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/location.rb', line 61 def get_obj_by_address self.set_defaults return 'No address set for location' if !self.country_region && !self.admin_district && !self.postal_code && !self.locality && !self.address_line # Reformat strings address_line = self.address_line.gsub(' ','%20') locality = self.locality.gsub(' ','%20') # Build request URL params = "#{self.country_region}/#{self.admin_district}/#{self.postal_code}/" params += "#{locality}/#{address_line}?o=#{self.obj_type}" request_url = $obj_base_url+params+"&key=#{self.api_key}" # REST request ! begin self.object = Net::HTTP.get_response(URI.parse(request_url)).body return self.object rescue return FALSE end end |
- (Object) get_obj_by_point
Uses the object's latitude and longitude to get a Location object from the Bing API. The object contains a normalized address, extra information, and latitude, longitude information
83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/location.rb', line 83 def get_obj_by_point self.set_defaults return 'No latitude or longitude set for location' unless self.latitude && self.longitude # Build request URL params = +"#{self.latitude},#{self.longitude}?o=#{self.obj_type}" request_url = $obj_base_url+params+"&key=#{self.api_key}" # REST request ! begin self.object = Net::HTTP.get_response(URI.parse(request_url)).body return self.object rescue return FALSE end end |
- (Object) get_obj_by_query
Uses the object's query to get a Location object from the Bing API. The object contains a normalized address, extra information, and latitude, longitude information
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/location.rb', line 101 def get_obj_by_query self.set_defaults return 'No query set for location' if !self.query query = self.query.gsub(' ','%20') # Build request URL params = "#{query}?o=#{self.obj_type}" request_url = $obj_base_url+params+"&key=#{self.api_key}" # REST request ! begin self.object = Net::HTTP.get_response(URI.parse(request_url)).body return self.object rescue return FALSE end end |
- (Object) parse_obj
186 187 188 |
# File 'lib/location.rb', line 186 def parse_obj # TODO - WRITE THIS end |
- (Object) set_defaults
Makes assumptions for required fields that are not yet defined. Assumptions can be overridden by assigning values
- country_region
-
Country or region, i.e. 'US'
- admin_district
-
Administrative District. In the US this is State i.e. 'PA'
- postal_code
-
Postal code or zip code. i.e. '15213'
- locality
-
Locality The locality, such as the city or neighborhood, that corresponds to an address i.e. 'Pittsburgh'
- address_line
-
The official street line of an address relative to the area i.e. '430 Atwood St'
- obj_type
-
Type of object returned by REST request
- map_type
-
Imagery to use for maps
- zoom_level
-
Zoom level of a map
45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/location.rb', line 45 def set_defaults self.country_region = '-' unless self.country_region self.admin_district = '-' unless self.admin_district self.postal_code = '-' unless self.postal_code self.locality = '-' unless self.locality self.address_line = '-' unless self.address_line # Allowed values - json, xml self.obj_type = 'json' unless self.obj_type # Allowed values - Aerial, AerialWithLabels, Road self.map_type = 'Road' unless self.map_type # Allowed values - An integer between 1 and 22 self.zoom_level = '11' unless self.zoom_level end |