From a3ff3eb81964cd517b00d10109b03bd55b571ea0 Mon Sep 17 00:00:00 2001 From: Tam Date: Tue, 28 Jun 2016 13:16:10 +0100 Subject: [PATCH] Added browser API key setting --- README.md | 3 +++ SimpleMapPlugin.php | 3 ++- fieldtypes/SimpleMap_MapFieldType.php | 4 +++- releases.json | 8 +++++++ resources/SimpleMap_Map.js | 31 ++++++++++++--------------- templates/plugin-settings.twig | 10 +++++++++ 6 files changed, 40 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index 5f1dcab..98bb0d9 100644 --- a/README.md +++ b/README.md @@ -36,6 +36,9 @@ You can search for elements using the location specified in your map field. When ## Changelog +### 1.2.2 +- Added Browser API key setting + ### 1.2.1 - Fixed bug where map would not display correctly when in a secondary tab. diff --git a/SimpleMapPlugin.php b/SimpleMapPlugin.php index 8de3be5..c9210f8 100644 --- a/SimpleMapPlugin.php +++ b/SimpleMapPlugin.php @@ -24,7 +24,7 @@ public function getDescription() public function getVersion() { - return '1.2.1'; + return '1.2.2'; } public function getSchemaVersion() @@ -55,6 +55,7 @@ public function getReleaseFeedUrl() protected function defineSettings() { return array( + 'browserApiKey' => array(AttributeType::String), 'serverApiKey' => array(AttributeType::String) ); } diff --git a/fieldtypes/SimpleMap_MapFieldType.php b/fieldtypes/SimpleMap_MapFieldType.php index d4760a9..2c507dd 100644 --- a/fieldtypes/SimpleMap_MapFieldType.php +++ b/fieldtypes/SimpleMap_MapFieldType.php @@ -26,8 +26,10 @@ public function getInputHtml($name, $value) if (!$settings->zoom) $settings->zoom = '15'; if (!$settings->height) $settings->height = '400'; + $key = craft()->plugins->getPlugin('SimpleMap')->getSettings()->browserApiKey; + craft()->templates->includeJsResource('simplemap/SimpleMap_Map.js'); - craft()->templates->includeJs("new SimpleMap('{$namespacedId}', {lat: '{$settings->lat}', lng: '{$settings->lng}', zoom: '{$settings->zoom}', height: '{$settings->height}'});"); + craft()->templates->includeJs("new SimpleMap('{$key}', '{$namespacedId}', {lat: '{$settings->lat}', lng: '{$settings->lng}', zoom: '{$settings->zoom}', height: '{$settings->height}'});"); craft()->templates->includeCssResource('simplemap/SimpleMap_Map.css'); diff --git a/releases.json b/releases.json index 8f35273..ad42ad9 100644 --- a/releases.json +++ b/releases.json @@ -68,5 +68,13 @@ "notes": [ "- Fixed bug where map would not display correctly when in a secondary tab." ] + }, + { + "version": "1.2.2", + "downloadUrl": "https://github.com/ethercreative/SimpleMap/archive/v1.2.2.zip", + "date": "2016-06-28T10:00:00-08:00", + "notes": [ + "- Added Browser API key setting" + ] } ] \ No newline at end of file diff --git a/resources/SimpleMap_Map.js b/resources/SimpleMap_Map.js index b87990d..58fa9be 100644 --- a/resources/SimpleMap_Map.js +++ b/resources/SimpleMap_Map.js @@ -1,4 +1,9 @@ -var SimpleMap = function (mapId, settings) { +var SimpleMap = function (key, mapId, settings) { + if (!key) { + SimpleMap.Fail('Missing API Key!'); + return; + } + // Vars this.setup = false; this.settings = settings; @@ -36,9 +41,9 @@ var SimpleMap = function (mapId, settings) { // Load Google APIs if they aren't already if (typeof google === "undefined") { - if (!window.simpleMapsLoadingGoogle) SimpleMap.LoadGoogleAPI(); + if (!window.simpleMapsLoadingGoogle) SimpleMap.LoadGoogleAPI(key); } else if (!google.maps || !google.maps.places) { // Load Google Maps APIs if the aren't already - if (!window.simpleMapsLoadingGoogle) SimpleMap.LoadGoogleAPI.LoadMapsApi(); + if (!window.simpleMapsLoadingGoogle) SimpleMap.LoadGoogleAPI.LoadMapsApi(key); } else { if (!self.setup) self.setupMap(); } @@ -67,35 +72,27 @@ SimpleMap.Fail = function (message) { if (window.console) console.error.apply(console, ['%cSimpleMap: %c' + message, 'font-weight:bold;','font-weight:normal;']); }; -SimpleMap.LoadGoogleAPI = function () { +SimpleMap.LoadGoogleAPI = function (key) { window.simpleMapsLoadingGoogle = true; var gmjs = document.createElement('script'); gmjs.type = 'text/javascript'; - gmjs.src = 'https://www.google.com/jsapi';//'https://maps.googleapis.com/maps/api/js?v=3&sensor=false&libraries=places&callback=google.loader.callbacks.maps'; + gmjs.src = 'https://www.google.com/jsapi?key=' + key; gmjs.onreadystatechange = function () { - SimpleMap.LoadGoogleAPI.LoadMapsApi(); + SimpleMap.LoadGoogleAPI.LoadMapsApi(key); }; gmjs.onload = function () { - SimpleMap.LoadGoogleAPI.LoadMapsApi(); + SimpleMap.LoadGoogleAPI.LoadMapsApi(key); }; document.body.appendChild(gmjs); }; -SimpleMap.LoadGoogleAPI.LoadMapsApi = function () { - google.load('maps', '3', { other_params: 'libraries=places', callback: function () {//sensor=false& +SimpleMap.LoadGoogleAPI.LoadMapsApi = function (key) { + google.load('maps', '3', { other_params: 'libraries=places&key='+key, callback: function () { document.dispatchEvent(new Event('SimpleMapsGAPILoaded')); }}); }; -// Load Google Maps API -SimpleMap.prototype.loadMaps = function () { - var self = this; - google.load('maps', '3', { other_params: 'libraries=places', callback: function () {//sensor=false& - self.setupMap(); - }}); -}; - // Setup Map SimpleMap.prototype.setupMap = function () { this.setup = true; diff --git a/templates/plugin-settings.twig b/templates/plugin-settings.twig index b98a47a..8833d24 100644 --- a/templates/plugin-settings.twig +++ b/templates/plugin-settings.twig @@ -1,5 +1,15 @@ {% import "_includes/forms" as forms %} +{{ forms.textField({ + label: "Google API Browser Key"|t, + instructions: 'Get an API key.', + id: 'browserApiKey', + name: 'browserApiKey', + value: settings.browserApiKey, + type: 'text', + required: true +}) }} + {{ forms.textField({ label: "Google API Server Key"|t, instructions: 'Optional. Used for searching and sorting by textual address / location. If left blank, only searching and sorting by Lat/Lng will be available. Get an API key.',