diff --git a/README.md b/README.md index 01a3b7c..878d977 100644 --- a/README.md +++ b/README.md @@ -8,12 +8,20 @@ Clone this repo into `craft/plugins/simplemap`. ## Usage Create the field as you would any other. -The field type will return an array containing `lat`, `lng`, `zoom`, and `address`. This means you can use `{{ myMapField.lat }}`. +The field type will return an array containing `lat`, `lng`, `zoom`, `address`, and `parts`. This means you can use `{{ myMapField.lat }}`. + +**`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). ![How it looks](resources/preview.png) ## Changelog +### 1.1.1 +- Added `parts` to the fieldtype output. + ### 1.1.0 - Merged \#2 & \#3 from @cballenar - \#2 - Field now stores map zoom diff --git a/SimpleMapPlugin.php b/SimpleMapPlugin.php index 62a3d81..dac8fff 100644 --- a/SimpleMapPlugin.php +++ b/SimpleMapPlugin.php @@ -24,7 +24,7 @@ public function getDescription() public function getVersion() { - return '1.1.0'; + return '1.1.1'; } public function getSchemaVersion() diff --git a/releases.json b/releases.json index bb58d1d..b700961 100644 --- a/releases.json +++ b/releases.json @@ -34,5 +34,13 @@ "#3 - Improved handling of unknown locations", "Improved error message display" ] + }, + { + "version": "1.1.1", + "downloadUrl": "https://github.com/ethercreative/SimpleMap/archive/v1.1.1.zip", + "date": "2016-05-31T10:00:00-08:00", + "notes": [ + "Added `parts` to the fieldtype output." + ] } ] \ No newline at end of file diff --git a/resources/SimpleMap_Map.js b/resources/SimpleMap_Map.js index 70227cf..b656bae 100644 --- a/resources/SimpleMap_Map.js +++ b/resources/SimpleMap_Map.js @@ -8,11 +8,13 @@ var SimpleMap = function (mapId, settings) { lat: document.getElementById(mapId + '-input-lat'), lng: document.getElementById(mapId + '-input-lng'), zoom: document.getElementById(mapId + '-input-zoom'), - address: document.getElementById(mapId + '-input-address') + address: document.getElementById(mapId + '-input-address'), + parts: document.getElementById(mapId + '-input-address-parts'), + 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) { + 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; } @@ -137,7 +139,7 @@ SimpleMap.prototype.setupMap = function () { lng = latLng[1]; if (!isNaN(lat) && !isNaN(lng)) { - self.update(parseFloat(lat), parseFloat(lng)).center(); + self.update(parseFloat(lat), parseFloat(lng)).sync().center(); return; } @@ -150,7 +152,7 @@ SimpleMap.prototype.setupMap = function () { lat = place.geometry.location.lat(); lng = place.geometry.location.lng(); - self.update(lat, lng).center(); + self.update(lat, lng).sync().center(); return; } @@ -160,7 +162,7 @@ SimpleMap.prototype.setupMap = function () { var lat = loc.geometry.location.lat(), lng = loc.geometry.location.lng(); - self.update(lat, lng).center(); + self.update(lat, lng).sync().center(); }); }); @@ -221,12 +223,28 @@ SimpleMap.prototype.sync = function (update) { // Update address / lat / lng based off marker location this.geo(pos, function (loc) { - // if loc, set address to formatted_location, else to postion + // if loc, set address to formatted_location, else to position var address = loc ? loc.formatted_address : pos.lat() + ", " + pos.lng(); // update address value self.address.value = address; self.inputs.address.value = address; + + // update address parts + while (self.inputs.parts.firstChild) + self.inputs.parts.removeChild(self.inputs.parts.firstChild); + + var name = self.inputs.partsBase.name; + loc.address_components.forEach(function (el) { + var input = document.createElement('input'), + n = el.types[0]; + if (!n) return; + if (n === 'postal_code_prefix') n = 'postal_code'; + input.type = 'hidden'; + input.name = name + '[' + n + ']'; + input.value = el.long_name; + self.inputs.parts.appendChild(input); + }); }); if (update) return this.update(pos.lat, pos.lng, true); @@ -263,6 +281,9 @@ SimpleMap.prototype.clear = function () { this.inputs.lng.value = ''; this.inputs.zoom.value = ''; this.inputs.address.value = ''; + + while (this.inputs.parts.firstChild) + this.inputs.parts.removeChild(this.inputs.parts.firstChild); }; window.SimpleMap = SimpleMap; \ No newline at end of file diff --git a/templates/map-fieldtype.twig b/templates/map-fieldtype.twig index 90da5ad..7aa62ba 100644 --- a/templates/map-fieldtype.twig +++ b/templates/map-fieldtype.twig @@ -12,4 +12,13 @@ - \ No newline at end of file + + + +
+ {% if value is not null and value.parts is defined %} + {% for key, val in value.parts %} + + {% endfor %} + {% endif %} +
\ No newline at end of file