Coder Social home page Coder Social logo

googlemaps-amd's Introduction

googlemaps-amd

Google Maps AMD Loader Plugin

Usage

The googlemaps loader plugin allows you to directly require Google Maps as an AMD dependency.

require(['googlemaps!'], function(gmaps) {
  // google.maps is now available as `gmaps`
  var map = new gmaps.Map('map-canvas');
});

The plugin depends on the async loader plugin. Use a RequireJS paths config to tell googlemaps where to find it. For example:

require.config({
  paths: {
    googlemaps: 'bower_components/googlemaps-amd/src/googlemaps',
    async: 'bower_components/requirejs-plugins/src/async'
  }
});

Why not use the async plugin?

The async plugin also allows you to load google maps, like so:

require(['async!http://maps.google.com/maps/api/js?sensor=false'], function() {
  // google.maps is defined
});

The googlemaps-amd plugin is essentially a wrapper around the async plugin, which allows an alternate syntax. This snippet does the same thing as the one above:

require(['googlemaps!'], function(googleMaps) {
  google.maps === googleMaps;   // true
});

Besides providing a more readable syntax, using the the googlemaps-amd plugin allows you to configure your google maps API keys (and other parameters) from the RequireJS configuration. This is especially useful if you want various consumers of your code to be able to configure their own API keys, without having to modify the source code of your library.

If this is not necessary for your situation, I recommend you do use the RequireJS async plugin.

Install

The easiest way to use the googlemaps plugin is to download the source file

Using Bower

The googlemaps plugin is registered as a Bower module. To install:

bower install googlemaps-amd

And don't forget to configure your paths:

require.config({
  paths: {
    googlemaps: 'bower_components/googlemaps-amd/src/googlemaps',
    async: 'bower_components/requirejs-plugins/src/async'
  }
});

Configuration

By default, the googlemaps loader will pull in the Google Maps library from https://maps.googleapis.com/maps/api/js?sensor=false. However, the plugin can be configured with additional options:

require.config({
  googlemaps: {
    params: {
      key: 'abcd1234',
      libraries: 'geometry'
    }
  }
});

Using the plugin will now load the library from https://maps.googleapis.com/maps/api/js?sensor=false&key=abcd1234&libraries=geometry.

googlemaps-amd's People

Contributors

eschwartz avatar artemgordinskiy avatar

Watchers

Vennkata 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.