Skip to content

Commit

Permalink
v1.8.0
Browse files Browse the repository at this point in the history
- The maps `parts` now contains all available options from [here](https://developers.google.com/maps/documentation/geocoding/intro#Types) (including the `_small` variants). Any options without values are returned as empty strings.
- Added `craft.simpleMap.getLatLngFromAddress($addressString[, $country])`.
  • Loading branch information
Tam committed Nov 30, 2017
1 parent 4423ec8 commit 8b2bbb8
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 1 deletion.
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,28 @@ var map = new mapboxgl.Map({
</script>
```

### Converting an address to Lat/Lng
If you need to convert a string address to a Lat/Lng you can do so using the
`craft.simpleMap.getLatLngFromAddress($addressString[, $country])` variable.
An example of this would be wanting to convert a customers delivery address to a
Lat/Lng, to display it on a map.

- `$address` - The string address you want to convert.
- `$country` - *Optional.* Restrict the conversion to a specific country (useful for non-specific searches, i.e. town name). Must be valid [2-letter ISO code](https://en.wikipedia.org/wiki/ISO_3166-1#Current_codes) (recommended), or full country name.

```twig
{% set location = craft.simpleMap.getLatLngFromAddress("Ether Creative, Maidstone", "GB") %}
{{ location.lat }}
{{ location.lng }}
```

## Changelog

### 1.8.0
- The maps `parts` now contains all available options from [here](https://developers.google.com/maps/documentation/geocoding/intro#Types) (including the `_small` variants). Any options without values are returned as empty strings.
- Added `craft.simpleMap.getLatLngFromAddress($addressString[, $country])`.

### 1.7.2
- Added ability to restrict location search by country
- New icon!
Expand Down
9 changes: 9 additions & 0 deletions releases.json
Original file line number Diff line number Diff line change
Expand Up @@ -192,5 +192,14 @@
"[Added] Added ability to restrict location search by country",
"[Changed] New icon!"
]
},
{
"version": "1.8.0",
"downloadUrl": "https://github.com/ethercreative/simplemap/archive/v1.8.0.zip",
"date": "2017-11-30T10:47:00-08:00",
"notes": [
"[Added] Added `craft.simpleMap.getLatLngFromAddress($addressString[, $country])`.",
"[Improved] The maps `parts` now contains all available options from [here](https://developers.google.com/maps/documentation/geocoding/intro#Types) (including the `_small` variants). Any options without values are returned as empty strings."
]
}
]
2 changes: 1 addition & 1 deletion simplemap/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.7.2';
return '1.8.0';
}

public function getSchemaVersion()
Expand Down
56 changes: 56 additions & 0 deletions simplemap/services/SimpleMapService.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,46 @@ class SimpleMapService extends BaseApplicationComponent {
public $searchEarthRad;
public $searchDistanceUnit;

private static $_parts = [
'room',
'floor',
'establishment',
'subpremise',
'premise',
'street_number',
'postal_code',
'street_address',
'colloquial_area',
'neighborhood',
'route',
'intersection',
'postal_town',
'sublocality_level_5',
'sublocality_level_4',
'sublocality_level_3',
'sublocality_level_2',
'sublocality_level_1',
'sublocality',
'locality',
'political',
'administrative_area_level_5',
'administrative_area_level_4',
'administrative_area_level_3',
'administrative_area_level_2',
'administrative_area_level_1',
'ward',
'country',
'parking',
'post_box',
'point_of_interest',
'natural_feature',
'park',
'airport',
'bus_station',
'train_station',
'transit_station',
];

// Public
// =========================================================================

Expand Down Expand Up @@ -63,6 +103,8 @@ public function getField (SimpleMap_MapFieldType $fieldType, $value)
$model = new SimpleMap_MapModel;
}

$model->parts = $this->_padParts($model);

$model->distance = $this->_calculateDistance($model);

return $model;
Expand Down Expand Up @@ -384,6 +426,20 @@ private function _calculateDistance (SimpleMap_MapModel $model)
// return ($this->searchEarthRad * acos(cos(deg2rad($lt1)) * cos(deg2rad($lt2)) * cos(deg2rad($ln2) - deg2rad($ln1)) + sin(deg2rad($lt1)) * sin(deg2rad($lt2))));
}

private function _padParts (SimpleMap_MapModel $model)
{
$parts = $model->parts;

foreach (self::$_parts as $part) {
if (!array_key_exists($part, $parts)) {
$parts[$part] = '';
$parts[$part . '_short'] = '';
}
}

return $parts;
}

private function _formatLocaleForMap ($locale) {
$locale = array_map(
'strtolower',
Expand Down
5 changes: 5 additions & 0 deletions simplemap/variables/SimpleMapVariable.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,9 @@ public function apiKey () {
return craft()->plugins->getPlugin('simpleMap')->getSettings()->browserApiKey;
}

public function getLatLngFromAddress ($address, $country = null)
{
return SimpleMapService::getLatLngFromAddress($address, $country);
}

}

0 comments on commit 8b2bbb8

Please sign in to comment.