Skip to content

Commit

Permalink
Added browser API key setting
Browse files Browse the repository at this point in the history
  • Loading branch information
Tam committed Jun 28, 2016
1 parent 7c14c05 commit a3ff3eb
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 19 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
3 changes: 2 additions & 1 deletion SimpleMapPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function getDescription()

public function getVersion()
{
return '1.2.1';
return '1.2.2';
}

public function getSchemaVersion()
Expand Down Expand Up @@ -55,6 +55,7 @@ public function getReleaseFeedUrl()
protected function defineSettings()
{
return array(
'browserApiKey' => array(AttributeType::String),
'serverApiKey' => array(AttributeType::String)
);
}
Expand Down
4 changes: 3 additions & 1 deletion fieldtypes/SimpleMap_MapFieldType.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down
8 changes: 8 additions & 0 deletions releases.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
]
}
]
31 changes: 14 additions & 17 deletions resources/SimpleMap_Map.js
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -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;
Expand Down
10 changes: 10 additions & 0 deletions templates/plugin-settings.twig
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
{% import "_includes/forms" as forms %}

{{ forms.textField({
label: "Google API Browser Key"|t,
instructions: '<a href="https://developers.google.com/maps/documentation/javascript/get-api-key#get-an-api-key" target="_blank">Get an API key.</a>',
id: 'browserApiKey',
name: 'browserApiKey',
value: settings.browserApiKey,
type: 'text',
required: true
}) }}

{{ forms.textField({
label: "Google API Server Key"|t,
instructions: '<em>Optional.</em> Used for searching and sorting by textual address / location. If left blank, only searching and sorting by Lat/Lng will be available. <a href="https://console.developers.google.com/apis/api/geocoding_backend/overview" target="_blank">Get an API key.</a>',
Expand Down

0 comments on commit a3ff3eb

Please sign in to comment.