From a5219379947630d70e937c36339cf886b7d34310 Mon Sep 17 00:00:00 2001 From: Austin Grigg Date: Wed, 31 Jul 2019 15:13:19 -0400 Subject: [PATCH 1/3] Geocode address on save --- src/fields/VzAddressField.php | 6 +++- src/models/VzAddressModel.php | 58 ++++++++++++++++++++++++++++++++--- 2 files changed, 58 insertions(+), 6 deletions(-) diff --git a/src/fields/VzAddressField.php b/src/fields/VzAddressField.php index 681b3f7..a1b31c4 100644 --- a/src/fields/VzAddressField.php +++ b/src/fields/VzAddressField.php @@ -95,7 +95,11 @@ public function normalizeValue($value, ElementInterface $element = null) return new VzAddressModel(); } - return new VzAddressModel($value); + $model = new VzAddressModel($value); + + $model->updateCoordinates(); + + return $model; } /** diff --git a/src/models/VzAddressModel.php b/src/models/VzAddressModel.php index ee2124f..bf8abec 100644 --- a/src/models/VzAddressModel.php +++ b/src/models/VzAddressModel.php @@ -38,6 +38,8 @@ class VzAddressModel extends Model public $postalCode; public $country; public $countryName; + public $latitude; + public $longitude; // Public Methods // ========================================================================= @@ -195,17 +197,28 @@ public function dynamicMap($params = [], $icon = []) $settings = VzAddress::$plugin->getSettings(); $key = $settings->googleApiKey; } + if (empty($key)) { return 'A Google API key is required.'; } // include the javascript api from google's cdn using our api key Craft::$app->getView()->includeJsFile("https://maps.googleapis.com/maps/api/js?key={$key}"); - // geocode our address into coordinates - $address = $this->toArray(); - // remove the name from the address as it throws the geocoder off - unset($address['name']); - $coords = $this->_geocodeAddress(implode($address, ' '), $key); + + if(!empty($this->latitude) && !empty($this->longitude)){ + $coords = [ + 'lat' => $this->latitude, + 'lng' => $this->longitude, + ]; + } + else { + // geocode our address into coordinates + $address = $this->toArray(); + // remove the name from the address as it throws the geocoder off + unset($address['name']); + $coords = $this->_geocodeAddress(implode($address, ' '), $key); + } + $width = isset($params['width']) ? strtolower($params['width']) : '400'; unset($params['width']); $height = isset($params['height']) ? strtolower($params['height']) : '200'; @@ -242,6 +255,41 @@ public function getCountryName() return Craft::$app->getI18n()->getLocaleById($this->country)->getDisplayName(); } + /** + * Virtual Attributes + */ + public function updateCoordinates(){ + if (empty($settings->googleApiKey)) { + // No Google API Key Set + return; + } + + $key = $settings->googleApiKey; + + // geocode our address into coordinates + $address = $this->toArray(); + + if(empty($address['street']) && empty($address['city']) && empty($address['postalCode'])){ + $this->latitude = null; + $this->longitude = null; + return; + } + + // remove the name from the address as it throws the geocoder off + unset($address['name']); + + $coords = $this->_geocodeAddress(implode($address, ' '), $key); + + if($coords){ + $this->latitude = $coords[0]; + $this->longitude = $coords[1]; + } + else { + $this->latitude = null; + $this->longitude = null; + } + } + /** * Method to geocode the given address string into a lat/lng coordinate pair * From 8ed713a8914390d2c2d5ef770544f337258a8c76 Mon Sep 17 00:00:00 2001 From: Austin Grigg Date: Tue, 6 Aug 2019 14:46:49 -0400 Subject: [PATCH 2/3] Only update lat lng if address has changed --- src/models/VzAddressModel.php | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/src/models/VzAddressModel.php b/src/models/VzAddressModel.php index bf8abec..237e108 100644 --- a/src/models/VzAddressModel.php +++ b/src/models/VzAddressModel.php @@ -40,6 +40,7 @@ class VzAddressModel extends Model public $countryName; public $latitude; public $longitude; + public $hash; // Public Methods // ========================================================================= @@ -259,8 +260,11 @@ public function getCountryName() * Virtual Attributes */ public function updateCoordinates(){ + $settings = VzAddress::$plugin->getSettings(); + if (empty($settings->googleApiKey)) { // No Google API Key Set + Craft::warning("No Google API Key Found"); return; } @@ -269,20 +273,32 @@ public function updateCoordinates(){ // geocode our address into coordinates $address = $this->toArray(); - if(empty($address['street']) && empty($address['city']) && empty($address['postalCode'])){ + if(empty($address['street']) && empty($address['city']) && empty($address['postalCode'])) + { $this->latitude = null; $this->longitude = null; return; } - + // remove the name from the address as it throws the geocoder off unset($address['name']); + + $searchAddress = implode($address, ' '); + + $addrHash = hash('ripemd160', $searchAddress); - $coords = $this->_geocodeAddress(implode($address, ' '), $key); + if($this->hash === $addrHash){ + Craft::info("Hash is the same so don't update"); + return; + } + + $this->hash = $addrHash; + + $coords = $this->_geocodeAddress($searchAddress, $key); if($coords){ - $this->latitude = $coords[0]; - $this->longitude = $coords[1]; + $this->latitude = $coords['lat']; + $this->longitude = $coords['lng']; } else { $this->latitude = null; @@ -307,6 +323,7 @@ private function _geocodeAddress($address, $key) if ($response['status'] == 'OK') { $lat = $response['results'][0]['geometry']['location']['lat']; $lng = $response['results'][0]['geometry']['location']['lng']; + // verify if data is complete if ($lat && $lng) { return [ @@ -319,6 +336,7 @@ private function _geocodeAddress($address, $key) } } else { + Craft::warning("Invalid status {$response['status']} from Google geocode for address: {$address}"); return false; } } From bb7c3302939b879569b63c3eb9eae6686f229a23 Mon Sep 17 00:00:00 2001 From: Austin Grigg Date: Fri, 10 Feb 2023 17:01:58 -0500 Subject: [PATCH 3/3] Update VzAddressModel.php Fix implode for php 8 --- src/models/VzAddressModel.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/models/VzAddressModel.php b/src/models/VzAddressModel.php index 237e108..8159e7d 100644 --- a/src/models/VzAddressModel.php +++ b/src/models/VzAddressModel.php @@ -217,7 +217,7 @@ public function dynamicMap($params = [], $icon = []) $address = $this->toArray(); // remove the name from the address as it throws the geocoder off unset($address['name']); - $coords = $this->_geocodeAddress(implode($address, ' '), $key); + $coords = $this->_geocodeAddress(implode(' ', $address), $key); } $width = isset($params['width']) ? strtolower($params['width']) : '400'; @@ -283,7 +283,7 @@ public function updateCoordinates(){ // remove the name from the address as it throws the geocoder off unset($address['name']); - $searchAddress = implode($address, ' '); + $searchAddress = implode(' ', $address); $addrHash = hash('ripemd160', $searchAddress); @@ -395,7 +395,7 @@ private function _styleString($style) $declaration[] .= "{$key}:{$value}"; } } - $output .= '&style=' . implode($declaration, '|'); + $output .= '&style=' . implode('|', $declaration); } return $output;