Skip to content

Commit

Permalink
Added parts value to output
Browse files Browse the repository at this point in the history
  • Loading branch information
Tam committed May 31, 2016
1 parent 23b5cef commit 56c07d8
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 9 deletions.
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 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.1.0';
return '1.1.1';
}

public function getSchemaVersion()
Expand Down
8 changes: 8 additions & 0 deletions releases.json
Original file line number Diff line number Diff line change
Expand Up @@ -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."
]
}
]
33 changes: 27 additions & 6 deletions resources/SimpleMap_Map.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand All @@ -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();
});

});
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
11 changes: 10 additions & 1 deletion templates/map-fieldtype.twig
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,13 @@
<input type="hidden" name="{{ name }}[lat]" value="{{ value is not null and value.lat is defined ? value.lat }}" id="{{ id }}-input-lat">
<input type="hidden" name="{{ name }}[lng]" value="{{ value is not null and value.lng is defined ? value.lng }}" id="{{ id }}-input-lng">
<input type="hidden" name="{{ name }}[zoom]" value="{{ value is not null and value.zoom is defined ? value.zoom }}" id="{{ id }}-input-zoom">
<input type="hidden" name="{{ name }}[address]" value="{{ value is not null and value.address is defined ? value.address }}" id="{{ id }}-input-address">
<input type="hidden" name="{{ name }}[address]" value="{{ value is not null and value.address is defined ? value.address }}" id="{{ id }}-input-address">
<input type="hidden" name="{{ name }}[parts]" id="{{ id }}-input-parts-base">

<div id="{{ id }}-input-address-parts" data-name="{{ name }}">
{% if value is not null and value.parts is defined %}
{% for key, val in value.parts %}
<input type="hidden" name="{{ name }}[parts][{{ key }}]" value="{{ val }}">
{% endfor %}
{% endif %}
</div>

0 comments on commit 56c07d8

Please sign in to comment.