diff --git a/README.md b/README.md index 5499b45..c8c7421 100644 --- a/README.md +++ b/README.md @@ -14,7 +14,8 @@ The field type will return an array containing `lat`, `lng`, `zoom`, `address`, **`parts`** This contains the locations address, broken down into its constituent parts. All values are optional so you'll need to have checks on any you use to make sure they exist. -A list of the available values can be found [here](https://developers.google.com/maps/documentation/geocoding/intro#Types). +A list of the available values can be found [here](https://developers.google.com/maps/documentation/geocoding/intro#Types). +To access the short version of any part, append `_short` to the end of its name. E.g. `{{ myMapField.country_short }}`. **Searching and Sorting** @@ -40,6 +41,13 @@ You can access your browser API key in templates using `craft.simpleMap.apiKey`. ## Changelog +### 1.3.0 +- Added option to hide the map, leaving only the address input +- Added ability to restrict auto-complete by country, address type, and boundary +- Added `_short` prefix to all parts, returning the short value. **You will need to re-save your fields for this to take effect.** +- Fixed map JS erroring when in globals +- Merged API keys into one + ### 1.2.4 - The address input automatically updates the map after paste diff --git a/SimpleMapPlugin.php b/SimpleMapPlugin.php index e00ea89..cb7fed1 100644 --- a/SimpleMapPlugin.php +++ b/SimpleMapPlugin.php @@ -24,12 +24,12 @@ public function getDescription() public function getVersion() { - return '1.2.4'; + return '1.3.0'; } public function getSchemaVersion() { - return '0.0.5'; + return '0.0.6'; } public function getDeveloper() @@ -55,8 +55,7 @@ public function getReleaseFeedUrl() protected function defineSettings() { return array( - 'browserApiKey' => array(AttributeType::String), - 'serverApiKey' => array(AttributeType::String) + 'browserApiKey' => array(AttributeType::String) ); } diff --git a/fieldtypes/SimpleMap_MapFieldType.php b/fieldtypes/SimpleMap_MapFieldType.php index 2c507dd..82414f4 100644 --- a/fieldtypes/SimpleMap_MapFieldType.php +++ b/fieldtypes/SimpleMap_MapFieldType.php @@ -25,11 +25,23 @@ public function getInputHtml($name, $value) if (!$settings->lng) $settings->lng = '0.514951'; if (!$settings->zoom) $settings->zoom = '15'; if (!$settings->height) $settings->height = '400'; + $settings->hideMap = $settings->hideMap ? 'true' : 'false'; + if (!$settings->typeRestriction) $settings->typeRestriction = ''; + + $boundary = "''"; + + if ($settings->boundaryRestrictionNElat && $settings->boundaryRestrictionNElng && $settings->boundaryRestrictionSWlat && $settings->boundaryRestrictionSWlng) { + $ne = array('lat' => $settings->boundaryRestrictionNElat, 'lng' => $settings->boundaryRestrictionNElng); + $sw = array('lat' => $settings->boundaryRestrictionSWlat, 'lng' => $settings->boundaryRestrictionSWlng); + + $boundary = JsonHelper::encode(array('ne' => $ne, 'sw' => $sw)); + } + $key = craft()->plugins->getPlugin('SimpleMap')->getSettings()->browserApiKey; craft()->templates->includeJsResource('simplemap/SimpleMap_Map.js'); - craft()->templates->includeJs("new SimpleMap('{$key}', '{$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}', hideMap: {$settings->hideMap}, country: '{$settings->countryRestriction}', type: '{$settings->typeRestriction}', boundary: {$boundary}});"); craft()->templates->includeCssResource('simplemap/SimpleMap_Map.css'); @@ -48,13 +60,285 @@ protected function defineSettings() 'lng' => array(AttributeType::Mixed, 'min' => 0), 'zoom' => array(AttributeType::Number, 'min' => 0), 'height' => array(AttributeType::Number, 'min' => 100), + 'hideMap' => array(AttributeType::Bool, 'default' => false), + 'countryRestriction' => array(AttributeType::String), + 'typeRestriction' => array(AttributeType::String), + + 'boundaryRestrictionNElat' => array(AttributeType::Mixed, 'min' => 0), + 'boundaryRestrictionNElng' => array(AttributeType::Mixed, 'min' => 0), + + 'boundaryRestrictionSWlat' => array(AttributeType::Mixed, 'min' => 0), + 'boundaryRestrictionSWlng' => array(AttributeType::Mixed, 'min' => 0), ); } public function getSettingsHtml() { + $countries = array( + array('value' => '', 'label' => 'All Countries'), + array('value' => "af", 'label' => "Afghanistan"), + array('value' => "ax", 'label' => "Åland Islands"), + array('value' => "al", 'label' => "Albania"), + array('value' => "dz", 'label' => "Algeria"), + array('value' => "as", 'label' => "American Samoa"), + array('value' => "ad", 'label' => "Andorra"), + array('value' => "ao", 'label' => "Angola"), + array('value' => "ai", 'label' => "Anguilla"), + array('value' => "aq", 'label' => "Antarctica"), + array('value' => "ag", 'label' => "Antigua and Barbuda"), + array('value' => "ar", 'label' => "Argentina"), + array('value' => "am", 'label' => "Armenia"), + array('value' => "aw", 'label' => "Aruba"), + array('value' => "au", 'label' => "Australia"), + array('value' => "at", 'label' => "Austria"), + array('value' => "az", 'label' => "Azerbaijan"), + array('value' => "bs", 'label' => "Bahamas"), + array('value' => "bh", 'label' => "Bahrain"), + array('value' => "bd", 'label' => "Bangladesh"), + array('value' => "bb", 'label' => "Barbados"), + array('value' => "by", 'label' => "Belarus"), + array('value' => "be", 'label' => "Belgium"), + array('value' => "bz", 'label' => "Belize"), + array('value' => "bj", 'label' => "Benin"), + array('value' => "bm", 'label' => "Bermuda"), + array('value' => "bt", 'label' => "Bhutan"), + array('value' => "bo", 'label' => "Bolivia, Plurinational State of"), + array('value' => "bq", 'label' => "Bonaire, Sint Eustatius and Saba"), + array('value' => "ba", 'label' => "Bosnia and Herzegovina"), + array('value' => "bw", 'label' => "Botswana"), + array('value' => "bv", 'label' => "Bouvet Island"), + array('value' => "br", 'label' => "Brazil"), + array('value' => "io", 'label' => "British Indian Ocean Territory"), + array('value' => "bn", 'label' => "Brunei Darussalam"), + array('value' => "bg", 'label' => "Bulgaria"), + array('value' => "bf", 'label' => "Burkina Faso"), + array('value' => "bi", 'label' => "Burundi"), + array('value' => "kh", 'label' => "Cambodia"), + array('value' => "cm", 'label' => "Cameroon"), + array('value' => "ca", 'label' => "Canada"), + array('value' => "cv", 'label' => "Cape Verde"), + array('value' => "ky", 'label' => "Cayman Islands"), + array('value' => "cf", 'label' => "Central African Republic"), + array('value' => "td", 'label' => "Chad"), + array('value' => "cl", 'label' => "Chile"), + array('value' => "cn", 'label' => "China"), + array('value' => "cx", 'label' => "Christmas Island"), + array('value' => "cc", 'label' => "Cocos (Keeling) Islands"), + array('value' => "co", 'label' => "Colombia"), + array('value' => "km", 'label' => "Comoros"), + array('value' => "cg", 'label' => "Congo"), + array('value' => "cd", 'label' => "Congo, the Democratic Republic of the"), + array('value' => "ck", 'label' => "Cook Islands"), + array('value' => "cr", 'label' => "Costa Rica"), + array('value' => "ci", 'label' => "Côte d'Ivoire"), + array('value' => "hr", 'label' => "Croatia"), + array('value' => "cu", 'label' => "Cuba"), + array('value' => "cw", 'label' => "Curaçao"), + array('value' => "cy", 'label' => "Cyprus"), + array('value' => "cz", 'label' => "Czech Republic"), + array('value' => "dk", 'label' => "Denmark"), + array('value' => "dj", 'label' => "Djibouti"), + array('value' => "dm", 'label' => "Dominica"), + array('value' => "do", 'label' => "Dominican Republic"), + array('value' => "ec", 'label' => "Ecuador"), + array('value' => "eg", 'label' => "Egypt"), + array('value' => "sv", 'label' => "El Salvador"), + array('value' => "gq", 'label' => "Equatorial Guinea"), + array('value' => "er", 'label' => "Eritrea"), + array('value' => "ee", 'label' => "Estonia"), + array('value' => "et", 'label' => "Ethiopia"), + array('value' => "fk", 'label' => "Falkland Islands (Malvinas)"), + array('value' => "fo", 'label' => "Faroe Islands"), + array('value' => "fj", 'label' => "Fiji"), + array('value' => "fi", 'label' => "Finland"), + array('value' => "fr", 'label' => "France"), + array('value' => "gf", 'label' => "French Guiana"), + array('value' => "pf", 'label' => "French Polynesia"), + array('value' => "tf", 'label' => "French Southern Territories"), + array('value' => "ga", 'label' => "Gabon"), + array('value' => "gm", 'label' => "Gambia"), + array('value' => "ge", 'label' => "Georgia"), + array('value' => "de", 'label' => "Germany"), + array('value' => "gh", 'label' => "Ghana"), + array('value' => "gi", 'label' => "Gibraltar"), + array('value' => "gr", 'label' => "Greece"), + array('value' => "gl", 'label' => "Greenland"), + array('value' => "gd", 'label' => "Grenada"), + array('value' => "gp", 'label' => "Guadeloupe"), + array('value' => "gu", 'label' => "Guam"), + array('value' => "gt", 'label' => "Guatemala"), + array('value' => "gg", 'label' => "Guernsey"), + array('value' => "gn", 'label' => "Guinea"), + array('value' => "gw", 'label' => "Guinea-Bissau"), + array('value' => "gy", 'label' => "Guyana"), + array('value' => "ht", 'label' => "Haiti"), + array('value' => "hm", 'label' => "Heard Island and McDonald Islands"), + array('value' => "va", 'label' => "Holy See (Vatican City State)"), + array('value' => "hn", 'label' => "Honduras"), + array('value' => "hk", 'label' => "Hong Kong"), + array('value' => "hu", 'label' => "Hungary"), + array('value' => "is", 'label' => "Iceland"), + array('value' => "in", 'label' => "India"), + array('value' => "id", 'label' => "Indonesia"), + array('value' => "ir", 'label' => "Iran, Islamic Republic of"), + array('value' => "iq", 'label' => "Iraq"), + array('value' => "ie", 'label' => "Ireland"), + array('value' => "im", 'label' => "Isle of Man"), + array('value' => "il", 'label' => "Israel"), + array('value' => "it", 'label' => "Italy"), + array('value' => "jm", 'label' => "Jamaica"), + array('value' => "jp", 'label' => "Japan"), + array('value' => "je", 'label' => "Jersey"), + array('value' => "jo", 'label' => "Jordan"), + array('value' => "kz", 'label' => "Kazakhstan"), + array('value' => "ke", 'label' => "Kenya"), + array('value' => "ki", 'label' => "Kiribati"), + array('value' => "kp", 'label' => "Korea, Democratic People's Republic of"), + array('value' => "kr", 'label' => "Korea, Republic of"), + array('value' => "kw", 'label' => "Kuwait"), + array('value' => "kg", 'label' => "Kyrgyzstan"), + array('value' => "la", 'label' => "Lao People's Democratic Republic"), + array('value' => "lv", 'label' => "Latvia"), + array('value' => "lb", 'label' => "Lebanon"), + array('value' => "ls", 'label' => "Lesotho"), + array('value' => "lr", 'label' => "Liberia"), + array('value' => "ly", 'label' => "Libya"), + array('value' => "li", 'label' => "Liechtenstein"), + array('value' => "lt", 'label' => "Lithuania"), + array('value' => "lu", 'label' => "Luxembourg"), + array('value' => "mo", 'label' => "Macao"), + array('value' => "mk", 'label' => "Macedonia, the former Yugoslav Republic of"), + array('value' => "mg", 'label' => "Madagascar"), + array('value' => "mw", 'label' => "Malawi"), + array('value' => "my", 'label' => "Malaysia"), + array('value' => "mv", 'label' => "Maldives"), + array('value' => "ml", 'label' => "Mali"), + array('value' => "mt", 'label' => "Malta"), + array('value' => "mh", 'label' => "Marshall Islands"), + array('value' => "mq", 'label' => "Martinique"), + array('value' => "mr", 'label' => "Mauritania"), + array('value' => "mu", 'label' => "Mauritius"), + array('value' => "yt", 'label' => "Mayotte"), + array('value' => "mx", 'label' => "Mexico"), + array('value' => "fm", 'label' => "Micronesia, Federated States of"), + array('value' => "md", 'label' => "Moldova, Republic of"), + array('value' => "mc", 'label' => "Monaco"), + array('value' => "mn", 'label' => "Mongolia"), + array('value' => "me", 'label' => "Montenegro"), + array('value' => "ms", 'label' => "Montserrat"), + array('value' => "ma", 'label' => "Morocco"), + array('value' => "mz", 'label' => "Mozambique"), + array('value' => "mm", 'label' => "Myanmar"), + array('value' => "na", 'label' => "Namibia"), + array('value' => "nr", 'label' => "Nauru"), + array('value' => "np", 'label' => "Nepal"), + array('value' => "nl", 'label' => "Netherlands"), + array('value' => "nc", 'label' => "New Caledonia"), + array('value' => "nz", 'label' => "New Zealand"), + array('value' => "ni", 'label' => "Nicaragua"), + array('value' => "ne", 'label' => "Niger"), + array('value' => "ng", 'label' => "Nigeria"), + array('value' => "nu", 'label' => "Niue"), + array('value' => "nf", 'label' => "Norfolk Island"), + array('value' => "mp", 'label' => "Northern Mariana Islands"), + array('value' => "no", 'label' => "Norway"), + array('value' => "om", 'label' => "Oman"), + array('value' => "pk", 'label' => "Pakistan"), + array('value' => "pw", 'label' => "Palau"), + array('value' => "ps", 'label' => "Palestine, State of"), + array('value' => "pa", 'label' => "Panama"), + array('value' => "pg", 'label' => "Papua New Guinea"), + array('value' => "py", 'label' => "Paraguay"), + array('value' => "pe", 'label' => "Peru"), + array('value' => "ph", 'label' => "Philippines"), + array('value' => "pn", 'label' => "Pitcairn"), + array('value' => "pl", 'label' => "Poland"), + array('value' => "pt", 'label' => "Portugal"), + array('value' => "pr", 'label' => "Puerto Rico"), + array('value' => "qa", 'label' => "Qatar"), + array('value' => "re", 'label' => "Réunion"), + array('value' => "ro", 'label' => "Romania"), + array('value' => "ru", 'label' => "Russian Federation"), + array('value' => "rw", 'label' => "Rwanda"), + array('value' => "bl", 'label' => "Saint Barthélemy"), + array('value' => "sh", 'label' => "Saint Helena, Ascension and Tristan da Cunha"), + array('value' => "kn", 'label' => "Saint Kitts and Nevis"), + array('value' => "lc", 'label' => "Saint Lucia"), + array('value' => "mf", 'label' => "Saint Martin (French part)"), + array('value' => "pm", 'label' => "Saint Pierre and Miquelon"), + array('value' => "vc", 'label' => "Saint Vincent and the Grenadines"), + array('value' => "ws", 'label' => "Samoa"), + array('value' => "sm", 'label' => "San Marino"), + array('value' => "st", 'label' => "Sao Tome and Principe"), + array('value' => "sa", 'label' => "Saudi Arabia"), + array('value' => "sn", 'label' => "Senegal"), + array('value' => "rs", 'label' => "Serbia"), + array('value' => "sc", 'label' => "Seychelles"), + array('value' => "sl", 'label' => "Sierra Leone"), + array('value' => "sg", 'label' => "Singapore"), + array('value' => "sx", 'label' => "Sint Maarten (Dutch part)"), + array('value' => "sk", 'label' => "Slovakia"), + array('value' => "si", 'label' => "Slovenia"), + array('value' => "sb", 'label' => "Solomon Islands"), + array('value' => "so", 'label' => "Somalia"), + array('value' => "za", 'label' => "South Africa"), + array('value' => "gs", 'label' => "South Georgia and the South Sandwich Islands"), + array('value' => "ss", 'label' => "South Sudan"), + array('value' => "es", 'label' => "Spain"), + array('value' => "lk", 'label' => "Sri Lanka"), + array('value' => "sd", 'label' => "Sudan"), + array('value' => "sr", 'label' => "Suriname"), + array('value' => "sj", 'label' => "Svalbard and Jan Mayen"), + array('value' => "sz", 'label' => "Swaziland"), + array('value' => "se", 'label' => "Sweden"), + array('value' => "ch", 'label' => "Switzerland"), + array('value' => "sy", 'label' => "Syrian Arab Republic"), + array('value' => "tw", 'label' => "Taiwan, Province of China"), + array('value' => "tj", 'label' => "Tajikistan"), + array('value' => "tz", 'label' => "Tanzania, United Republic of"), + array('value' => "th", 'label' => "Thailand"), + array('value' => "tl", 'label' => "Timor-Leste"), + array('value' => "tg", 'label' => "Togo"), + array('value' => "tk", 'label' => "Tokelau"), + array('value' => "to", 'label' => "Tonga"), + array('value' => "tt", 'label' => "Trinidad and Tobago"), + array('value' => "tn", 'label' => "Tunisia"), + array('value' => "tr", 'label' => "Turkey"), + array('value' => "tm", 'label' => "Turkmenistan"), + array('value' => "tc", 'label' => "Turks and Caicos Islands"), + array('value' => "tv", 'label' => "Tuvalu"), + array('value' => "ug", 'label' => "Uganda"), + array('value' => "ua", 'label' => "Ukraine"), + array('value' => "ae", 'label' => "United Arab Emirates"), + array('value' => "gb", 'label' => "United Kingdom"), + array('value' => "us", 'label' => "United States"), + array('value' => "um", 'label' => "United States Minor Outlying Islands"), + array('value' => "uy", 'label' => "Uruguay"), + array('value' => "uz", 'label' => "Uzbekistan"), + array('value' => "vu", 'label' => "Vanuatu"), + array('value' => "ve", 'label' => "Venezuela, Bolivarian Republic of"), + array('value' => "vn", 'label' => "Viet Nam"), + array('value' => "vg", 'label' => "Virgin Islands, British"), + array('value' => "vi", 'label' => "Virgin Islands, U.S."), + array('value' => "wf", 'label' => "Wallis and Futuna"), + array('value' => "eh", 'label' => "Western Sahara"), + array('value' => "ye", 'label' => "Yemen"), + array('value' => "zm", 'label' => "Zambia"), + array('value' => "zw", 'label' => "Zimbabwe"), + ); + $types = array( + array('label' => "Any Type", 'value' => ''), + array('label' => "Non-business Locations", 'value' => 'geocode'), + array('label' => "Precise Addresses", 'value' => 'address'), + array('label' => "Businesses Only", 'value' => 'establishment'), + array('label' => "Regions (Countries, States, Counties, Postal Codes, etc...)", 'value' => '(regions)'), + array('label' => "Towns & Cities", 'value' => '(cities)'), + ); + return craft()->templates->render('simplemap/map-settings', array( - 'settings' => $this->getSettings() + 'settings' => $this->getSettings(), + 'countries'=> $countries, + 'types'=> $types, )); } diff --git a/releases.json b/releases.json index c38e0fe..29bcd6d 100644 --- a/releases.json +++ b/releases.json @@ -92,5 +92,17 @@ "notes": [ "[Improved] The address input automatically updates the map after paste" ] + }, + { + "version": "1.3.0", + "downloadUrl": "https://github.com/ethercreative/SimpleMap/archive/v1.3.0.zip", + "date": "2016-09-07T10:00:00-08:00", + "notes": [ + "[Added] Added option to hide the map, leaving only the address input", + "[Added] Added ability to restrict auto-complete by country, address type, and boundary", + "[Added] Added `_short` prefix to all parts, returning the short value. **You will need to re-save your fields for this to take effect.**", + "[Fixed] Fixed map JS erroring when in globals", + "[Improved] Merged API keys into one" + ] } ] \ No newline at end of file diff --git a/resources/SimpleMap_Map.js b/resources/SimpleMap_Map.js index 45e0f0b..857a432 100644 --- a/resources/SimpleMap_Map.js +++ b/resources/SimpleMap_Map.js @@ -18,18 +18,17 @@ var SimpleMap = function (key, mapId, settings) { partsBase: document.getElementById(mapId + '-input-parts-base') }; - // Check we have everything we need - if (!this.mapEl || !this.address || !this.inputs.lat || !this.inputs.lng || !this.inputs.address || !this.inputs.parts) { - SimpleMap.Fail('Map inputs with id ' + mapId + ' not found!'); - return; - } - // Setup settings this.settings = { height: this.settings.height, lat: parseFloat(this.settings.lat), lng: parseFloat(this.settings.lng), - zoom: parseInt(this.settings.zoom) + zoom: parseInt(this.settings.zoom), + hideMap: this.settings.hideMap, + + country: this.settings.country, + type: this.settings.type, + boundary: this.settings.boundary }; // Stop submission on address field enter @@ -37,6 +36,17 @@ var SimpleMap = function (key, mapId, settings) { if (e.keyCode === 13) e.preventDefault(); }); + if (this.settings.hideMap) { + this.AutoCompleteOnly(key); + return; + } + + // Check we have everything we need + if (!this.mapEl || !this.address || !this.inputs.lat || !this.inputs.lng || !this.inputs.address || !this.inputs.parts) { + SimpleMap.Fail('Map inputs with id ' + mapId + ' not found!'); + return; + } + var self = this; // Load Google APIs if they aren't already @@ -53,17 +63,36 @@ var SimpleMap = function (key, mapId, settings) { }); // Re-draw map on tab change - [].slice.call(document.getElementById('tabs').getElementsByTagName('a')).forEach(function (el) { - el.addEventListener('click', function () { - var x = self.map.getZoom(), - c = self.map.getCenter(); - - setTimeout(function () { - google.maps.event.trigger(self.map,'resize'); - self.map.setZoom(x); - self.map.setCenter(c); - }, 1); + if (document.getElementById('tabs')) { + [].slice.call(document.getElementById('tabs').getElementsByTagName('a')).forEach(function (el) { + el.addEventListener('click', function () { + var x = self.map.getZoom(), + c = self.map.getCenter(); + + setTimeout(function () { + google.maps.event.trigger(self.map, 'resize'); + self.map.setZoom(x); + self.map.setCenter(c); + }, 1); + }); }); + } +}; + +SimpleMap.prototype.AutoCompleteOnly = function (key) { + var self = this; + + // Load Google APIs if they aren't already + if (typeof google === "undefined") { + 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(key); + } else { + if (!self.setup) self.setupAutoComplete(); + } + + document.addEventListener('SimpleMapsGAPILoaded', function () { + if (!self.setup) self.setupAutoComplete(); }); }; @@ -93,11 +122,21 @@ SimpleMap.LoadGoogleAPI.LoadMapsApi = function (key) { }}); }; +SimpleMap.prototype.formatBoundary = function () { + if (this.settings.boundary !== '') { + var ne = new google.maps.LatLng(this.settings.boundary.ne.lat, this.settings.boundary.ne.lng), + sw = new google.maps.LatLng(this.settings.boundary.sw.lat, this.settings.boundary.sw.lng); + this.settings.boundary = new google.maps.LatLngBounds(ne, sw); + } +}; + // Setup Map SimpleMap.prototype.setupMap = function () { this.setup = true; var self = this; + this.formatBoundary(); + // Geocoder (for address search) this.geocoder = new google.maps.Geocoder(); @@ -111,21 +150,7 @@ SimpleMap.prototype.setupMap = function () { mapTypeId: google.maps.MapTypeId.ROADMAP }); - // Setup address search - var autocomplete = new google.maps.places.Autocomplete(this.address); - autocomplete.map = this.map; - autocomplete.bindTo('bounds', this.map); - - // Update map on paste - this.address.addEventListener('paste', function () { - setTimeout(function () { - google.maps.event.trigger(autocomplete, 'place_changed'); - }, 1); - }); - - this.address.addEventListener('input', function () { - if (this.value === '') self.clear(); - }); + this.setupAutoComplete(); // Add marker this.map.marker = new google.maps.Marker({ @@ -145,6 +170,64 @@ SimpleMap.prototype.setupMap = function () { // Update map to saved zoom this.map.setZoom(parseInt(zoom)); + // When the marker is dropped + google.maps.event.addListener(this.map.marker, 'dragend', function () { + self.sync(true); + }); + + // When map is clicked + google.maps.event.addListener(this.map, 'click', function (e) { + + var lat = e.latLng.lat(), + lng = e.latLng.lng(); + + self.update(lat, lng).sync(); + }); + + // When the zoom is changed + google.maps.event.addListener(this.map, 'zoom_changed', function () { + var zoom = this.getZoom(); + + self.updateZoom(zoom).center(); + }); +}; + +SimpleMap.prototype.setupAutoComplete = function () { + if (!this.setup) { + this.setup = true; + this.formatBoundary(); + } + if (!this.geocoder) this.geocoder = new google.maps.Geocoder(); + var self = this; + + // Setup address search + var opts = {}; + if (this.settings.country !== '') opts.componentRestrictions = {country: this.settings.country}; + if (this.settings.type !== '') opts.types = [this.settings.type]; + if (this.settings.boundary !== '') opts.bounds = this.settings.boundary; + + var autocomplete = new google.maps.places.Autocomplete(this.address, opts); + if (!this.settings.hideMap) { + autocomplete.map = this.map; + autocomplete.bindTo('bounds', this.map); + } + + // Initial Update + setTimeout(function () { + google.maps.event.trigger(autocomplete, 'place_changed'); + }, 1); + + // Update map on paste + this.address.addEventListener('paste', function () { + setTimeout(function () { + google.maps.event.trigger(autocomplete, 'place_changed'); + }, 1); + }); + + this.address.addEventListener('input', function () { + if (this.value === '') self.clear(); + }); + // When the auto-complete place changes google.maps.event.addListener(autocomplete, 'place_changed', function () { var address = self.address.value, lat, lng; @@ -184,27 +267,6 @@ SimpleMap.prototype.setupMap = function () { }); }); - - // When the marker is dropped - google.maps.event.addListener(this.map.marker, 'dragend', function () { - self.sync(true); - }); - - // When map is clicked - google.maps.event.addListener(this.map, 'click', function (e) { - - var lat = e.latLng.lat(), - lng = e.latLng.lng(); - - self.update(lat, lng).sync(); - }); - - // When the zoom is changed - google.maps.event.addListener(this.map, 'zoom_changed', function () { - var zoom = this.getZoom(); - - self.updateZoom(zoom).center(); - }); }; SimpleMap.prototype.update = function (lat, lng, leaveMarker, leaveFields) { @@ -215,7 +277,7 @@ SimpleMap.prototype.update = function (lat, lng, leaveMarker, leaveFields) { this.inputs.lng.value = lng; } - if (!leaveMarker) { + if (!leaveMarker && !this.settings.hideMap) { this.map.marker.setPosition(latLng); this.map.marker.setVisible(true); } @@ -230,13 +292,15 @@ SimpleMap.prototype.updateZoom = function (zoom) { }; SimpleMap.prototype.center = function () { + if (this.settings.hideMap) return this; + this.map.setCenter(this.map.marker.getPosition()); return this; }; SimpleMap.prototype.sync = function (update) { - var pos = this.map.marker.getPosition(), + var pos = this.settings.hideMap ? new google.maps.LatLng(this.inputs.lat.value, this.inputs.lng.value) : this.map.marker.getPosition(), self = this; // Update address / lat / lng based off marker location @@ -253,6 +317,7 @@ SimpleMap.prototype.sync = function (update) { self.inputs.parts.removeChild(self.inputs.parts.firstChild); var name = self.inputs.partsBase.name; + console.log(loc.address_components); loc.address_components.forEach(function (el) { var input = document.createElement('input'), n = el.types[0]; @@ -262,6 +327,11 @@ SimpleMap.prototype.sync = function (update) { input.name = name + '[' + n + ']'; input.value = el.long_name; self.inputs.parts.appendChild(input); + + var inputS = input.cloneNode(true); + inputS.name = name + '[' + n + '_short]'; + inputS.value = el.short_name; + self.inputs.parts.appendChild(inputS); }); }); diff --git a/services/SimpleMapService.php b/services/SimpleMapService.php index 35b42a0..1ecf6b5 100644 --- a/services/SimpleMapService.php +++ b/services/SimpleMapService.php @@ -167,10 +167,10 @@ private function _searchLocation (DbCommand &$query, $params) */ private function _getLatLngFromAddress ($address) { - if (!$this->settings['serverApiKey']) return null; + if (!$this->settings['browserApiKey']) return null; $url = 'https://maps.googleapis.com/maps/api/geocode/json?address=' . rawurlencode($address) - . '&key=' . $this->settings['serverApiKey']; + . '&key=' . $this->settings['browserApiKey']; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); diff --git a/templates/map-fieldtype.twig b/templates/map-fieldtype.twig index 94dfc4f..c8fd860 100644 --- a/templates/map-fieldtype.twig +++ b/templates/map-fieldtype.twig @@ -7,7 +7,9 @@ value: value is not null and value.address ? value.address : '', }) }} +{% if settings.hideMap != "true" %}
+{% endif %} diff --git a/templates/map-settings.twig b/templates/map-settings.twig index 1175979..07f3c1c 100644 --- a/templates/map-settings.twig +++ b/templates/map-settings.twig @@ -8,7 +8,7 @@ id: 'lat', name: 'lat', value: settings.lat, - type: 'text' + type: 'number' }) }} {{ forms.textField({ @@ -19,7 +19,7 @@ id: 'lng', name: 'lng', value: settings.lng, - type: 'text' + type: 'number' }) }} {{ forms.textField({ @@ -42,4 +42,78 @@ name: 'height', value: settings.height, type: 'number' -}) }} \ No newline at end of file +}) }} + +{{ forms.lightswitchField({ + label: "Hide Map"|t, + instructions: "When on, the map will be hidden leaving just the address auto-complete field"|t, + id: 'hideMap', + name: 'hideMap', + on: settings.hideMap +}) }} + +