GithubHelp home page GithubHelp logo

recorrido-cl / google-maps-for-rails Goto Github PK

View Code? Open in Web Editor NEW

This project forked from apneadiving/google-maps-for-rails

0.0 3.0 0.0 2.52 MB

Enables easy Google map + overlays creation in Ruby apps

Home Page: http://apneadiving.github.io/

License: MIT License

Ruby 33.27% JavaScript 66.73%

google-maps-for-rails's Introduction

<img src=“https://secure.travis-ci.org/apneadiving/Google-Maps-for-Rails.svg?branch=master” alt=“Build Status” /> <img src=“https://codeclimate.com/github/apneadiving/Google-Maps-for-Rails.svg” /> <img src=“https://badge.fury.io/rb/gmaps4rails.svg” alt=“Gem Version” /> <img src=“http://api.coderwall.com/apneadiving/endorsecount.png” alt=“Endorse” />

Gmaps4rails is developed to simply create a Google Map with overlays (markers, infowindows…). Yet it’s backed on a very flexible codebase which could be prone to accept other map providers.

Use it with any Ruby app (I guess you could simply take the js anywhere if you like).

Here is a quick tutorial on youtube, and my presentation on speaker deck.

For live examples, see here.

A note for < 2.x users

Google-Maps-for-Rails-2.0 is an important rewrite to keep the minimum code and features. If you’re migrating from previous versions, you may want to read the rationale about it.

Requirements

1) Gemfile

gem 'gmaps4rails'

2) HTML

Add a div to bear your map, example:

<div style='width: 800px;'>
  <div id="map" style='width: 800px; height: 400px;'></div>
</div>

3) Javascript Dependencies:

Insert google scripts in your dom:

<script src="//maps.google.com/maps/api/js?v=3.23&sensor=false&client=&key=&libraries=geometry&language=&hl=&region="></script> 
<script src="//cdn.rawgit.com/mahnunchik/markerclustererplus/master/dist/markerclusterer.min.js"></script>
<script src='//cdn.rawgit.com/printercu/google-maps-utility-library-v3-read-only/master/infobox/src/infobox_packed.js' type='text/javascript'></script> <!-- only if you need custom infoboxes -->

You’ll require underscore.js too, see here: underscorejs.org/ (‘lo-dash` is compatible too, your choice!).

4) Javascript source code

If you have the asset pipeline, add this:

//= require underscore
//= require gmaps/google

If you don’t have asset pipeline, you’ll need to import the js OR coffee files:

rails g gmaps4rails:copy_js

rails g gmaps4rails:copy_coffee

5) Javascript code:

Create your map:

handler = Gmaps.build('Google');
handler.buildMap({ provider: {}, internal: {id: 'map'}}, function(){
  markers = handler.addMarkers([
    {
      "lat": 0,
      "lng": 0,
      "picture": {
        "url": "http://people.mozilla.com/~faaborg/files/shiretoko/firefoxIcon/firefox-32.png",
        "width":  32,
        "height": 32
      },
      "infowindow": "hello!"
    }
  ]);
  handler.bounds.extendWith(markers);
  handler.fitMapToBounds();
});

6) Add options:

You’re likely going to want to customize your maps by passing an options object. Using the example above, let’s say you’d like to disable the map controls. This and any other options you can find in the Google Maps API reference can be passed into the ‘provider` options hash like so:

handler = Gmaps.build('Google');
handler.buildMap({
    provider: {
      disableDefaultUI: true
      // pass in other Google Maps API options here
    },
    internal: {
      id: 'map'
    }
  },
  function(){
    markers = handler.addMarkers([
      {
        "lat": 0,
        "lng": 0,
        "picture": {
          "url": "http://people.mozilla.com/~faaborg/files/shiretoko/firefoxIcon/firefox-32.png",
          "width":  32,
          "height": 32
        },
        "infowindow": "hello!"
      }
    ]);
    handler.bounds.extendWith(markers);
    handler.fitMapToBounds();
  }
);

You can see other examples of adding ‘provider` options in the live examples. Also, check the wiki for further documentation on the possible JavaScript methods.

Generating JSON

In your controller:

@users = User.all
@hash = Gmaps4rails.build_markers(@users) do |user, marker|
  marker.lat user.latitude
  marker.lng user.longitude
end

In your view:

<script>
    markers = handler.addMarkers(<%=raw @hash.to_json %>);
</script>

Easily customizable

You can change almost everything with a few lines of code. See details here.

Options

  • Markers with Info window, Custom Picture, RichMarkers (make your own markers with custom html)

  • Circles, Polylines, Polygons, Kml

  • Refresh your map on the fly with Javascript (and Ajax)

  • More details in the Wiki

Todo?

Feel free to contact us, you have your say.

MIT license.

Author: Benjamin Roth

Contributors

google-maps-for-rails's People

Contributors

apneadiving avatar xsve avatar jmcopeland avatar logicalhan avatar kristianmandrup avatar avioing avatar narkoz avatar nithinbekal avatar cameel avatar excid3 avatar davidruru avatar oli-g avatar antonovga avatar oelmekki avatar pascalj avatar abrambailey avatar raid5 avatar turupawn avatar aroop avatar laalex avatar amandaporto avatar baisa avatar bradkollmyer avatar dleve123 avatar fgo avatar ihanli avatar alvarezloaiciga avatar libo avatar lin4ipsum avatar mariovisic avatar

Watchers

Sandro Gomez avatar James Cloos avatar Alejandro Rodríguez 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.