Skip to content

Instantly share code, notes, and snippets.

@brankoajzele
Created December 7, 2011 11:12
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save brankoajzele/1442434 to your computer and use it in GitHub Desktop.
Save brankoajzele/1442434 to your computer and use it in GitHub Desktop.
Magento Google Maps Geocoding Customer Addresses
<?php
/**
* @author Branko Ajzele <ajzele@gmail.com>
*/
class Inchoo_Extension_Helper_Gmap extends Mage_Core_Helper_Abstract
{
const GOOGLE_MAPS_HOST = 'maps.google.com';
const CONFIG_PATH_GOOGLE_MAPS_API_KEY = 'inchoo_google/maps/api_key';
public function getGoogleMapsApiKey()
{
return Mage::getStoreConfig(self::CONFIG_PATH_GOOGLE_MAPS_API_KEY);
}
/**
*
* @param Mage_Customer_Model_Address $address
* @param boolean $saveCoordinatesToAddress
* @param boolean $forceLookup
* @return array Returns the array of float values like {[0] => float(18.4438156) [1] => float(45.5013644)}
*/
public function fetchAddressGeoCoordinates(Mage_Customer_Model_Address $address, $saveCoordinatesToAddress = true, $forceLookup = false)
{
/**
* USAGE EXAMPLE
$customer = Mage::getModel('customer/customer');
$customer->setWebsiteId(Mage::app()->getWebsite()->getId());
$customer->loadByEmail('some-email@domain.com');
Mage::helper('inchoo/gmap')->fetchAddressGeoCoordinates($customer->getDefaultBillingAddress())
*/
$coordinates = array();
if ($address->getId() && $address->getGmapCoordinatePointX() && $address->getGmapCoordinatePointY() && $forceLookup == false) {
$coordinates = array($address->getGmapCoordinatePointX(), $address->getGmapCoordinatePointY());
} else if (($address->getId() && !$address->getGmapCoordinatePointX() && !$address->getGmapCoordinatePointY())
OR ($address->getId() && $address->getGmapCoordinatePointX() && $address->getGmapCoordinatePointY() && $forceLookup == true)) {
$lineAddress = $address->getStreet1(). ', '.$address->getPostcode().' '.$address->getCity().', '.$address->getCountry();
$client = new Zend_Http_Client();
$client->setUri('http://'.self::GOOGLE_MAPS_HOST.'/maps/geo');
$client->setMethod(Zend_Http_Client::GET);
$client->setParameterGet('output', 'json');
$client->setParameterGet('key', $this->getGoogleMapsApiKey());
$client->setParameterGet('q', $lineAddress);
$response = $client->request();
if ($response->isSuccessful() && $response->getStatus() == 200) {
$_response = json_decode($response->getBody());
$_coordinates = @$_response->Placemark[0]->Point->coordinates;
if (is_array($_coordinates) && count($_coordinates) >= 2) {
$coordinates = array_slice($_coordinates, 0, 2);
if ($saveCoordinatesToAddress) {
try {
$address->setGmapLng($coordinates[0]);
$address->setGmapLat($coordinates[1]);
$address->save();
} catch (Exception $e) {
Mage::logException($e);
}
}
}
}
}
return $coordinates;
}
}
@brankoajzele
Copy link
Author

NOTE: In order for this to work, you need to have "gmap_coordinate_point_x" and "gmap_coordinate_point_y" attributes created on customer.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment