在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):edwardsamuel/google-maps-services-ruby开源软件地址(OpenSource Url):https://github.com/edwardsamuel/google-maps-services-ruby开源编程语言(OpenSource Language):Ruby 100.0%开源软件介绍(OpenSource Introduction):Ruby gem for Google Maps APIsThis gem is ported from Python Client for Google Maps Services. DescriptionUse Ruby? Want to geocode something? Looking for directions? Maybe matrices of directions? This gem brings the Google Maps API Web Services to your Ruby application. The Ruby gem for Google Maps Web Service APIs is a gem for the following Google Maps APIs:
Keep in mind that the same terms and conditions apply to usage of the APIs when they're accessed through this gem. FeaturesRate LimitingNever sleep between requests again! By default, requests are sent at the expected rate limits for
each web service, typically 10 queries per second for free users. If you want to speed up or slowdown requests, you can do that too, using Retry on FailureAutomatically retry when intermittent failures occur. That is, when any of the retriable 5xx errors are returned from the API. Keys and Client IDsMaps API for Work customers can use their client ID and secret to authenticate. Free customers can use their API key, too. Note: Currently, Roads API does not accept client ID. It requires API key to authenticate the request. Ruby Hash/Array as API ResultThis gem return a Ruby Hash/Array object as the API result. The result format structure is same as in Google Maps API documentation. Requirements
Obtain API keysEach Google Maps Web Service requires an API key or Client ID. API keys are freely available with a Google Account at https://developers.google.com/console. To generate a server key for your project:
Your API key should be 40 characters long, and begin with Important: This key should be kept secret on your server. InstallationAdd this line to your application's Gemfile:
And then execute:
Or install it yourself as:
In your Ruby code, add this line to load this gem:
UsageBefore you request Google Maps API, you must configure the client. You can view the reference documentation. Configure clientrequire 'google_maps_service'
# Setup API keys
gmaps = GoogleMapsService::Client.new(key: 'Add your key here')
# Setup client IDs
gmaps = GoogleMapsService::Client.new(
client_id: 'Add your client id here',
client_secret: 'Add your client secret here'
)
# More complex setup
gmaps = GoogleMapsService::Client.new(
key: 'Add your key here',
retry_timeout: 20, # Timeout for retrying failed request
queries_per_second: 10 # Limit total request per second
) You can also set up the client globally. require 'google_maps_service'
# Setup global parameters
GoogleMapsService.configure do |config|
config.key = 'Add your key here'
config.retry_timeout = 20
config.queries_per_second = 10
end
# Initialize client using global parameters
gmaps = GoogleMapsService::Client.new For more examples and detail (setup proxy, timeout, caching, etc.) while initializing the client, check out Client documentation. Latitude/longitude pairs formatSome APIs require latitude/longitude pair(s) as their parameter(s). This gem accept various format of latitude/longitude pairs: # Array
latlng = [40.714224, -73.961452]
# Hash with symbolized keys
latlng = {lat: 40.714224, lng: -73.961452}
latlng = {latitude: 40.714224, longitude: -73.961452}
# Hash with string keys
latlng = {'lat' => 40.714224, 'lng' => -73.961452}
latlng = {'latitude' => 40.714224, 'longitude' => -73.961452} Directions API# Simple directions
routes = gmaps.directions(
'1600 Amphitheatre Pkwy, Mountain View, CA 94043, USA',
'2400 Amphitheatre Parkway, Mountain View, CA 94043, USA',
mode: 'walking',
alternatives: false) Sample result: [{
:bounds=>{
:northeast=>{:lat=>37.4238004, :lng=>-122.084314},
:southwest=>{:lat=>37.42277989999999, :lng=>-122.0882019}
},
:copyrights=>"Map data ©2015 Google",
:legs=>[
{
:distance=>{:text=>"0.2 mi", :value=>393},
:duration=>{:text=>"5 mins", :value=>287},
:end_address=>"2400 Amphitheatre Pkwy, Mountain View, CA 94043, USA",
:end_location=>{:lat=>37.4238004, :lng=>-122.0882019},
:start_address=>"1600 Amphitheatre Pkwy, Mountain View, CA 94043, USA",
:start_location=>{:lat=>37.42277989999999, :lng=>-122.084314},
:steps=>[
{
:distance=>{:text=>"223 ft", :value=>68},
:duration=>{:text=>"1 min", :value=>49},
:end_location=>{:lat=>37.4228653, :lng=>-122.0850785},
:html_instructions=>"Head <b>west</b>",
:polyline=>{:points=>"kclcF|qchVEdAGx@ALAJ"},
:start_location=>{:lat=>37.42277989999999, :lng=>-122.084314},
:travel_mode=>"WALKING"
}, {
:distance=>{:text=>"108 ft", :value=>33},
:duration=>{:text=>"1 min", :value=>23},
:end_location=>{:lat=>37.423161, :lng=>-122.0850102},
:html_instructions=>"Turn <b>right</b> toward <b>Amphitheatre Pkwy</b>",
:maneuver=>"turn-right",
:polyline=>{:points=>"}clcFvvchVg@IQC"},
:start_location=>{:lat=>37.4228653, :lng=>-122.0850785},
:travel_mode=>"WALKING"
}, {
:distance=>{:text=>"407 ft", :value=>124},
:duration=>{:text=>"2 mins", :value=>90},
:end_location=>{:lat=>37.423396, :lng=>-122.0863768},
:html_instructions=>"Turn <b>left</b> onto <b>Amphitheatre Pkwy</b>",
:maneuver=>"turn-left",
:polyline=>{:points=>"welcFhvchVEf@Eb@C\\EZGp@Il@CRAJAJ"},
:start_location=>{:lat=>37.423161, :lng=>-122.0850102},
:travel_mode=>"WALKING"
}, {
:distance=>{:text=>"0.1 mi", :value=>168},
:duration=>{:text=>"2 mins", :value=>125},
:end_location=>{:lat=>37.4238004, :lng=>-122.0882019},
:html_instructions=>
"Slight <b>right</b> to stay on <b>Amphitheatre Pkwy</b><div style=\"font-size:0.9em\">Destination will be on the right</div>",
:maneuver=>"turn-slight-right",
:polyline=>{:points=>"gglcFz~chVGJADAD?DIh@MhAWhBOxACT"},
:start_location=>{:lat=>37.423396, :lng=>-122.0863768},
:travel_mode=>"WALKING"
}
],
:via_waypoint=>[]
}
],
:overview_polyline=>{:points=>"kclcF|qchVQxCy@MKjA[xCE^IVMz@y@bH"},
:summary=>"Amphitheatre Pkwy",
:warnings=>["Walking directions are in beta. Use caution – This route may be missing sidewalks or pedestrian paths."],
:waypoint_order=>[]
}] For more usage examples and result format, check out gem documentation, test script, and Google Maps Directions API documentation. Distance Matrix API# Multiple parameters distance matrix
origins = ["Bobcaygeon ON", [41.43206, -81.38992]]
destinations = [[43.012486, -83.6964149], {lat: 42.8863855, lng: -78.8781627}]
matrix = gmaps.distance_matrix(origins, destinations,
mode: 'driving',
language: 'en-AU',
avoid: 'tolls',
units: 'imperial') For more usage examples and result format, check out gem documentation, test script, and Google Maps Distance Matrix API documentation. Elevation API# Elevation of some locations
locations = [[40.714728, -73.998672], [-34.397, 150.644]]
results = gmaps.elevation(locations)
# Elevation along path
locations = [[40.714728, -73.998672], [-34.397, 150.644]]
results = gmaps.elevation_along_path(locations, 5) For more usage examples and result format, check out gem documentation, test script, and Google Maps Elevation API documentation. Geocoding API# Geocoding an address
results = gmaps.geocode('1600 Amphitheatre Parkway, Mountain View, CA')
# Look up an address with reverse geocoding
results = gmaps.reverse_geocode([40.714224, -73.961452]) For more usage examples and result format, check out gem documentation, test script, and Google Maps Geocoding API documentation. Roads API# Snap to roads
path = [
[-33.8671, 151.20714],
[-33.86708, 151.20683000000002],
[-33.867070000000005, 151.20674000000002],
[-33.86703, 151.20625]
]
results = gmaps.snap_to_roads(path, interpolate: true)
# Snapped speed limits
path = [
[-33.8671, 151.20714],
[-33.86708, 151.20683000000002],
[-33.867070000000005, 151.20674000000002],
[-33.86703, 151.20625]
]
results = gmaps.snapped_speed_limits(path)
# Speed limits
place_ids = [
'ChIJ0wawjUCuEmsRgfqC5Wd9ARM',
'ChIJ6cs2kkCuEmsRUfqC5Wd9ARM'
]
results = gmaps.speed_limits(place_ids) For more usage examples and result format, check out gem documentation, test script, and Google Maps Roads API documentation. Time Zone API# Current time zone
timezone = gmaps.timezone([39.603481, -119.682251])
# Time zone at certain time
timezone = gmaps.timezone([39.603481, -119.682251], timestamp: Time.at(1608)) For more usage examples and result format, check out gem documentation, test script, and Google Maps Time Zone API documentation. Polyline encoder/decoderGoogle Encoded Polyline is a lossy compression algorithm that allows you to store a series of coordinates as a single string. This format is used in some APIs:
To handle Google Encoded Polyline, this gem provides encoder/decoder: require 'google_maps_service/polyline' # Or, require 'google_maps_service' is enough
# Decode polyline
encoded_path = '_p~iF~ps|U_ulLnnqC_mqNvxq`@'
path = GoogleMapsService::Polyline.decode(encoded_path)
#=> [{:lat=>38.5, :lng=>-120.2}, {:lat=>40.7, :lng=>-120.95}, {:lat=>43.252, :lng=>-126.45300000000002}]
# Encode polyline
path = [[38.5, -120.2], [40.7, -120.95], [43.252, -126.453]]
encoded_path = GoogleMapsService::Polyline.
全部评论
专题导读
上一篇:r-spatial/rgee: Google Earth Engine for R发布时间:2022-06-12下一篇:acgeospatial/GoogleEarthEnginePy: Basic script to order data on Google Earth Eng ...发布时间:2022-06-12热门推荐
热门话题
阅读排行榜
|
请发表评论