├── .npmignore ├── .travis.yml ├── .gitignore ├── index.js ├── .jscsrc ├── .editorconfig ├── package.json ├── test └── url-builder.spec.js ├── LICENSE.md ├── lib └── url-builder.js ├── src └── google-maps-api-loader.js └── README.md /.npmignore: -------------------------------------------------------------------------------- 1 | karma.conf.js 2 | test/ 3 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - "8" 4 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | npm-debug.log 3 | node_modules/* 4 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | module.exports = require('./src/google-maps-api-loader.js'); 2 | -------------------------------------------------------------------------------- /.jscsrc: -------------------------------------------------------------------------------- 1 | { 2 | "preset": "google", 3 | "maximumLineLength": 100 4 | } 5 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | indent_style = space 5 | indent_size = 4 6 | end_of_line = lf 7 | charset = utf-8 8 | trim_trailing_whitespace = true 9 | insert_final_newline = true 10 | 11 | [package.json] 12 | indent_style = space 13 | indent_size = 2 14 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "google-maps-api-loader", 3 | "version": "1.1.1", 4 | "description": "Conditionally load the Google Maps API in an ES6 promise", 5 | "main": "index.js", 6 | "scripts": { 7 | "lint": "./node_modules/.bin/jscs index.js src/ lib/", 8 | "test": "npm run lint && mocha" 9 | }, 10 | "repository": { 11 | "type": "git", 12 | "url": "git+ssh://git@github.com/laurencedorman/google-maps-api-loader.git" 13 | }, 14 | "keywords": [ 15 | "google-maps", 16 | "google-maps-api", 17 | "es6", 18 | "promise" 19 | ], 20 | "author": "laurencedorman", 21 | "license": "MIT", 22 | "bugs": { 23 | "url": "https://github.com/laurencedorman/google-maps-api-loader/issues" 24 | }, 25 | "homepage": "https://github.com/laurencedorman/google-maps-api-loader#readme", 26 | "dependencies": { 27 | "es6-promise": "^4.0.5" 28 | }, 29 | "devDependencies": { 30 | "chai": "^4.1.2", 31 | "chai-as-promised": "^7.1.1", 32 | "jscs": "^3.0.3", 33 | "mocha": "^5.0.1" 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /test/url-builder.spec.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var chai = require('chai'); 4 | var expect = chai.expect; 5 | 6 | var urlBuilder = require('../lib/url-builder.js'); 7 | 8 | describe('urlBuilder', function() { 9 | 10 | var builtUrl; 11 | 12 | before(function() { 13 | builtUrl = urlBuilder({ 14 | base: 'https://maps.googleapis.com/maps/api/js', 15 | libraries: ['places', 'geometry'], 16 | language: 'en', 17 | version: 3, 18 | callback: 'apiLoaded' 19 | }); 20 | }); 21 | 22 | it('Should return a string', function() { 23 | expect(builtUrl).to.be.a('string'); 24 | }); 25 | 26 | it('Should match the param structure', function() { 27 | var paramSection = builtUrl.split('?')[1]; 28 | 29 | expect(paramSection).to.be.a('string'); 30 | expect(paramSection).to.have.string('libraries=places,geometry'); 31 | expect(paramSection).to.have.string('&language=en'); 32 | expect(paramSection).to.have.string('&v=3'); 33 | expect(paramSection).to.have.string('&callback=apiLoaded'); 34 | }); 35 | }); 36 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Laurence Dorman 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /lib/url-builder.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | /** 4 | * urlBuilder 5 | * 6 | * @param {object} params 7 | * @param {string} params.base the base url 8 | * @param {array} params.libraries an array of the libraries to be requested 9 | * @param {string} params.callback the callback function 10 | * 11 | * @return {string} 12 | */ 13 | function urlBuilder(params) { 14 | var builtUrl = params.base; 15 | 16 | builtUrl += '?'; 17 | 18 | if (params.apiKey) { 19 | builtUrl += 'key=' + params.apiKey + '&'; 20 | } 21 | 22 | if (params.client) { 23 | builtUrl += 'client=' + params.client + '&'; 24 | } 25 | 26 | if (params.libraries.length > 0) { 27 | builtUrl += 'libraries='; 28 | 29 | params.libraries.forEach(function(library, index) { 30 | builtUrl += library; 31 | 32 | if (index !== params.libraries.length - 1) { 33 | builtUrl += ','; 34 | } 35 | }); 36 | 37 | builtUrl += '&'; 38 | } 39 | 40 | if (params.language) { 41 | builtUrl += 'language=' + params.language + '&'; 42 | } 43 | 44 | if (params.version) { 45 | builtUrl += 'v=' + params.version + '&'; 46 | } 47 | 48 | builtUrl += 'callback=' + params.callback; 49 | 50 | return builtUrl; 51 | } 52 | 53 | module.exports = urlBuilder; 54 | -------------------------------------------------------------------------------- /src/google-maps-api-loader.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var Promise = require('es6-promise').Promise; 4 | var urlBuilder = require('../lib/url-builder.js'); 5 | 6 | var googleApi; 7 | 8 | function loadAutoCompleteAPI(params) { 9 | var script = document.createElement('script'); 10 | 11 | script.type = 'text/javascript'; 12 | 13 | script.src = urlBuilder({ 14 | base: 'https://maps.googleapis.com/maps/api/js', 15 | libraries: params.libraries || [], 16 | callback: 'googleMapsAutoCompleteAPILoad', 17 | apiKey: params.apiKey, 18 | client: params.client, 19 | language: params.language, 20 | version: params.version 21 | }); 22 | 23 | document.querySelector('head').appendChild(script); 24 | } 25 | 26 | /** 27 | * googleMapsApiLoader 28 | * 29 | * @param {object} params 30 | * @param {object} params.libraries 31 | * 32 | * @return {promise} 33 | */ 34 | function googleMapsApiLoader(params) { 35 | if (googleApi) { 36 | return Promise.resolve(googleApi); 37 | } 38 | 39 | return new Promise(function(resolve, reject) { 40 | loadAutoCompleteAPI(params); 41 | 42 | window.googleMapsAutoCompleteAPILoad = function() { 43 | googleApi = window.google; 44 | resolve(googleApi); 45 | }; 46 | 47 | setTimeout(function() { 48 | if (!window.google) { 49 | reject(new Error('Loading took too long')); 50 | } 51 | }, 5000); 52 | }); 53 | } 54 | 55 | module.exports = googleMapsApiLoader; 56 | 57 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | # Google Maps API Loader 3 | 4 | ## DEPRECATED 18/02/2021 5 | 6 | Use the official [googlemaps/js-api-loader](https://github.com/googlemaps/js-api-loader) library instead. 7 | 8 | --- 9 | 10 | [![npm](https://img.shields.io/npm/v/google-maps-api-loader.svg)](http://npm.im/google-maps-api-loader) 11 | [![travis](https://travis-ci.org/laurencedorman/google-maps-api-loader.svg?branch=master)](https://travis-ci.org/laurencedorman/google-maps-api-loader) 12 | [![Dependency Status](https://david-dm.org/laurencedorman/google-maps-api-loader.svg?style=flat)](https://david-dm.org/laurencedorman/google-maps-api-loader) 13 | [![devDependency Status](https://david-dm.org/laurencedorman/google-maps-api-loader/dev-status.svg?style=flat)](https://david-dm.org/laurencedorman/google-maps-api-loader#info=devDependencies) 14 | 15 | 16 | Provides a convenient wrapper for the Google Maps API, allowing it to be called in the promise syntax. 17 | 18 | ### Installation 19 | 20 | ``` 21 | $ npm install --save google-maps-api-loader 22 | ``` 23 | 24 | ### Usage 25 | 26 | ```js 27 | 28 | var GoogleMapsApiLoader = require('google-maps-api-loader'); 29 | 30 | GoogleMapsApiLoader({ 31 | libraries: ['places'], 32 | apiKey: 'your-api-key' // optional 33 | }) 34 | .then(function(googleApi) { 35 | var autocomplete = new googleApi.maps.places.AutocompleteService(); 36 | }, function(err) { 37 | console.error(err); 38 | }); 39 | 40 | ``` 41 | 42 | ### License 43 | 44 | MIT 45 | --------------------------------------------------------------------------------