@@ -47,8 +47,6 @@ public function getRegions($countryCode)
47
47
/**
48
48
* Get latitude and longitude of an address and country.
49
49
*
50
- * Uses Demac_Geocoding library if it is available.
51
- *
52
50
* @param $postalCode
53
51
* @param $country
54
52
*
@@ -57,39 +55,30 @@ public function getRegions($countryCode)
57
55
* @TODO Move to the location helper.
58
56
* @TODO Isolate curl functionality so it is less exposed. (maybe use Zend_Http as part of this)
59
57
* @TODO Come up with a better solution to sleep(1), at the very least sleep in microseconds.
60
- * @TODO if we keep sleep related functionality add comments explaining that it is for the google api timeouts.
61
58
*/
62
59
public function getLatLong ($ postalCode , $ country )
63
60
{
61
+ //This is used to avoid being rate limited by the google maps api.
64
62
sleep (1 );
65
63
$ postalCode = urlencode ($ postalCode );
66
64
$ country = urlencode ($ country );
67
65
$ url = "http://maps.google.com/maps/api/geocode/json?address= $ postalCode&sensor=false®ion= $ country " ;
68
- $ ch = curl_init ();
69
- curl_setopt ($ ch , CURLOPT_URL , $ url );
70
- curl_setopt ($ ch , CURLOPT_RETURNTRANSFER , 1 );
71
- curl_setopt ($ ch , CURLOPT_SSL_VERIFYHOST , 0 );
72
- curl_setopt ($ ch , CURLOPT_SSL_VERIFYPEER , 0 );
73
- $ response = curl_exec ($ ch );
74
- curl_close ($ ch );
75
- $ response_a = json_decode ($ response );
76
-
77
- //@TODO handle invalid api responses that don't contain this element.
78
- $ lat = $ response_a ->results [0 ]->geometry ->location ->lat ;
79
- $ long = $ response_a ->results [0 ]->geometry ->location ->lng ;
80
-
81
- return array ($ lat , $ long );
82
- }
83
-
84
- /**
85
- * Checks if the Demac_Geocoding extension is available (recommended).
86
- *
87
- * @returns Boolean
88
- * @TODO Move this to the Location helper.
89
- */
90
- public function geocodingAvailable ()
91
- {
92
- return (bool ) Mage::getModel ('geocoding/geocode ' );
66
+ try {
67
+ $ ch = curl_init ();
68
+ curl_setopt ($ ch , CURLOPT_URL , $ url );
69
+ curl_setopt ($ ch , CURLOPT_RETURNTRANSFER , 1 );
70
+ curl_setopt ($ ch , CURLOPT_SSL_VERIFYHOST , 0 );
71
+ curl_setopt ($ ch , CURLOPT_SSL_VERIFYPEER , 0 );
72
+ $ response = curl_exec ($ ch );
73
+ curl_close ($ ch );
74
+ $ response_a = json_decode ($ response );
75
+ $ lat = $ response_a ->results [0 ]->geometry ->location ->lat ;
76
+ $ long = $ response_a ->results [0 ]->geometry ->location ->lng ;
77
+ return array ($ lat , $ long );
78
+ } catch (Exception $ e ) {
79
+ //@TODO return false and methods calling this to accept false as a response
80
+ return array (0 , 0 );
81
+ }
93
82
}
94
83
95
84
0 commit comments