GithubHelp home page GithubHelp logo

calderhayes / google-maps Goto Github PK

View Code? Open in Web Editor NEW

This project forked from zilverline/google-maps

0.0 0.0 0.0 129 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 cgunther avatar danielzwijnenburg avatar ina6ra avatar lonnv avatar lvonk avatar mikefranken avatar raphaelpra avatar remvee avatar ruben-hartog avatar s0meone avatar stephanvd avatar voska avatar

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.