diff --git a/README.md b/README.md index fce7acf..958a1d2 100644 --- a/README.md +++ b/README.md @@ -95,8 +95,28 @@ var map = new mapboxgl.Map({ ``` +### 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! diff --git a/releases.json b/releases.json index d6e3844..7041ddf 100644 --- a/releases.json +++ b/releases.json @@ -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." + ] } ] diff --git a/simplemap/SimpleMapPlugin.php b/simplemap/SimpleMapPlugin.php index 7d465ff..4147c7b 100644 --- a/simplemap/SimpleMapPlugin.php +++ b/simplemap/SimpleMapPlugin.php @@ -24,7 +24,7 @@ public function getDescription() public function getVersion() { - return '1.7.2'; + return '1.8.0'; } public function getSchemaVersion() diff --git a/simplemap/services/SimpleMapService.php b/simplemap/services/SimpleMapService.php index b6ec9d3..2f0b741 100644 --- a/simplemap/services/SimpleMapService.php +++ b/simplemap/services/SimpleMapService.php @@ -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 // ========================================================================= @@ -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; @@ -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', diff --git a/simplemap/variables/SimpleMapVariable.php b/simplemap/variables/SimpleMapVariable.php index 1e3e097..29282ae 100644 --- a/simplemap/variables/SimpleMapVariable.php +++ b/simplemap/variables/SimpleMapVariable.php @@ -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); + } + } \ No newline at end of file