GithubHelp home page GithubHelp logo

zilverline / google-maps Goto Github PK

View Code? Open in Web Editor NEW
55.0 18.0 39.0 135 KB

Ruby wrapper for the Google Maps API

Home Page: https://github.com/zilverline/google-maps

License: MIT License

Ruby 100.00%

google-maps's Introduction

Gem Version Build Status Coverage Status Code Climate

Google Maps

Installation

gem install google-maps

Or add the following line to your Gemfile:

gem 'google-maps'

Configuration

You can either authenticate with an API key or a digital signature.

API key:

Google::Maps.configure do |config|
  config.authentication_mode = Google::Maps::Configuration::API_KEY
  config.api_key = 'xxxxxxxxxxx'
end

Digital signature:

Google::Maps.configure do |config|
  config.authentication_mode = Google::Maps::Configuration::DIGITAL_SIGNATURE
  config.client_id = 'xxxxxxxxxxx'
  config.client_secret = 'xxxxxxxxxxx'
end

Default language

Set the default language.

Google::Maps.configure do |config|
  config.default_language = :nl #dutch
end

Usage Examples

Distance

  Google::Maps.distance("Science Park, Amsterdam", "Deventer")
  #=> "105 km"
  Google::Maps.distance("52.3545543,4.9519029", "Deventer")
  #=> "105 km"

Duration

  Google::Maps.duration("Science Park, Amsterdam", "Deventer")
  #=> "1 hour 12 mins"
  Google::Maps.duration("52.3545543,4.9519029", "Deventer")
  #=> "1 hour 12 mins"

Distance matrix

Google::Maps.distance_matrix("Science Park, Amsterdam", "Deventer").distance
#=> 104478
Google::Maps.distance_matrix("Science Park, Amsterdam", "Deventer").duration
#=> 4374

Route

 route = Google::Maps.route("Science Park, Amsterdam", "Deventer")
 route = Google::Maps.route("52.3545543,4.9519029", "Deventer")
 route.distance.text
 #=> "104 km"
 route.duration.text
 #=> "1 hour 12 mins"
 route.distance.value
 #=> 103712
 route.duration.value
 #=> 4337
 route.steps
 #=> [
   {
            "distance" => {
      "text" => "0,1 km",
     "value" => 125
   },
            "duration" => {
      "text" => "1 min.",
     "value" => 35
   },
        "end_location" => {
     "lat" => 52.3556768,
     "lng" => 4.9545739
   },
   "html_instructions" => "Rijd <b>naar het noordwesten</b>, richting het <b>Science Park</b>",
            "polyline" => {
     "points" => "oqp~Hqpf]?@?@?@KNOVEHA@A?s@wAQ]Q_@We@?A?ADI"
   },
      "start_location" => {
     "lat" => 52.3549602,
     "lng" => 4.9538473
   },
         "travel_mode" => "DRIVING"
 },
 {
            "distance" => {
      "text" => "37 m",
      ........

Options

The distance, duration and route methods allow you to pass an options hash to the Directions API. All options can also be set as default_params for these services.

Google::Maps.configure do |config|
  ...
  config.default_params = {
    directions_service: {
      mode: 'transit',
      transit_routing_preference: 'fewer_transfers'
    }
  }
  ...
end

The following options are permitted:

{
  mode: 'driving', #default
  transit_mode: 'train', #only used when mode is set to 'transit'
  transit_routing_preference: 'fewer_transfers',
  avoid: 'tolls',
  arrival_time: 1545397430, # In seconds since midnight, January 1, 1970 UTC.
  departure_time: 1545397430, # Cannot be in the past.
}

# NB! You can specify either departure_time or arrival_time, but not both.
TRAVEL_MODES = [
  'driving', #(Default) indicates standard driving directions using the road network.
  'bicycling', #requests bicycling directions via bicycle paths & preferred streets.
  'transit', #requests directions via public transit routes.
  'walking'
]

TRANSIT_MODES = [
  'bus',
  'subway',
  'train',
  'tram',
  'rail' # This is equivalent to transit_mode=train|tram|subway
]

TRANSIT_ROUTING_PREFERENCES = [
  'less_walking', prefer limited amounts of walking.
  'fewer_transfers' prefer a limited number of transfers.
]

AVOID = [
  'tolls',
  'highways',
  'ferries',
  'indoor', # Avoid indoor steps for walking and transit directions.
]

Places

 places = Google::Maps.places('Amsterdam')
 places.first.text
 #=> "Amsterdam, Nederland"
 places.first.place_id
 #=> "ChIJVXealLU_xkcRja_At0z9AGY"

 place = Google::Maps.place("ChIJVXealLU_xkcRja_At0z9AGY")

 place.place_id
 #=> "ChIJVXealLU_xkcRja_At0z9AGY"

 place.latitude
 #=> "52.3679843"

 place.longitude
 #=> "4.9035614"

 place.address
 #=> "Amsterdam, Nederland"

 place.name
 #=> "Amsterdam"

 place.photos
 #<Google::Maps::Result height, html_attribution[], photo_reference, width>

 place.website
 #=> "http://www.amsterdam.nl/"

 place.url
 #=> "https://maps.google.com/?q=Amsterdam,+Netherlands"

Geocode

  geocodes = Google::Maps.geocode("Science Park, Amsterdam")
  geocodes.first.address = "Science Park, 1012 WX Amsterdam, Nederland"
  geocodes.first.latitude = 52.3545543
  geocodes.first.longitude = 4.9540916

Testing

Run all tests:

  rspec spec/

Copyright

Copyright (c) 2011-2019 Zilverline. See LICENSE for details.

google-maps's People

Contributors

bforma avatar calderhayes avatar cgunther avatar danielzwijnenburg avatar ina6ra avatar lonnv avatar lvonk avatar mikefranken avatar raphaelpra avatar remvee avatar ruben-hartog avatar s0meone avatar stefanahman avatar stephanvd avatar voska avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

google-maps's Issues

Support the new Routes API

Google Maps has a new(ish) service called the Routes API:

Routes API is the next generation, performance optimized version of the existing Directions API and Distance Matrix API. It helps you find the ideal route from A to Z, calculates ETAs and distances for matrices of origin and destination locations, and also offers new features.

Google actively recommends that developers migrate to the new API:

Why migrate to Routes API?

The Routes API provides improved performance for calculating directions, distance, and travel time, making it worthwhile to replace apps that currently use Directions API and Distance Matrix API. Most of the functionality of Routes API is backward compatible with both Directions API and Distance Matrix API.

It would be great for this gem to add support for the new Routes API. It doesn't necessarily mean support needs to be dropped for the older APIs โ€“ as far as I can tell, Google has no plans to shut them down. But I think it would be great to have the flexibility to specify which API the gem uses. It sounds like there's good feature parity between the old and new APIs.

While the new Routes API is supposedly more performant, it's also worth bearing in mind that it's billed differently and needs to be enabled independently of the Directions and Distance Matrix APIs. So it seems reasonable for people to have a preference over which API is used.

Support waypoints for overall distance calculation

Support waypoints for overall distance calculation

Current behaviour:

Google::Maps.route('London', 'Cambridge').distance
 => {"text"=>"61.2 mi", "value"=>98570}
Google::Maps.route('London', 'Cambridge',  waypoints: 'Luton').distance
 => {"text"=>"34.1 mi", "value"=>54902}

#distance method only returns distance from London to Luton

Expected behaviour:
Route with waypoint returns distance 71.1 miles (sum of London-Luton + Luton-Cambridge)

Google::Maps::InvalidResponseException: REQUEST_DENIED: This API project is not authorized to use this API

Enabled api services

Geocoding API
Maps JavaScript API
Distance Matrix API
Places API
Geolocation API
Google+ API
Legacy People API PRIVATE
Maps Static API

Google::Maps.configure do |config|
config.authentication_mode = Google::Maps::Configuration::API_KEY
config.api_key = 'xxxxxxxxxxx'
end

I can access

Distance matrix
Google::Maps.distance_matrix("Science Park, Amsterdam", "Deventer").distance

Getting error
Google::Maps::InvalidResponseException: REQUEST_DENIED: This API project is not authorized to use this API.

for

Distance
Google::Maps.distance("Science Park, Amsterdam", "Deventer")

Duration
Google::Maps.duration("Science Park, Amsterdam", "Deventer")

@Ruben-Hartog
What api service i'm missing here to enable, to get duration and distance ??

Thank you

InvalidResponseException on `places` and `geocode`

๐Ÿ‘‹ Hi there! Thanks for the gem! Looks like google might have made some breaking changes to the api and the library is currently half broken. For example calling geocode and places as shown in readme will result in an error.

 geocodes = Google::Maps.geocode("Science Park, Amsterdam")
Google::Maps::InvalidResponseException: unknown error: getaddrinfo: nodename nor servname provided, or not known (nil-uri-given:80)
from /Users/yuecchen/.rvm/gems/ruby-2.5.3/gems/google-maps-3.0.4/lib/google_maps/api.rb:64:in `rescue in response'
Caused by SocketError: getaddrinfo: nodename nor servname provided, or not known (nil-uri-given:80)
from /Users/yuecchen/.rvm/gems/ruby-2.5.3/gems/httpclient-2.8.3/lib/httpclient/session.rb:625:in `rescue in create_socket'
Caused by SocketError: getaddrinfo: nodename nor servname provided, or not known
from /Users/yuecchen/.rvm/gems/ruby-2.5.3/gems/httpclient-2.8.3/lib/httpclient/session.rb:611:in `initialize'

actual maps?

Just to be confirm, this gem doesn't help creating actual Google maps?
(Looking for something like the old, excellent, still-works, but unmaintained gmaps4rails gem)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.