├── GMaps.php
├── README
└── example.php
/GMaps.php:
--------------------------------------------------------------------------------
1 | _key= $key;
76 | $this->_baseUrl= "http://" . self::MAPS_HOST . "/maps/geo?output=xml&key=" . $this->_key;
77 | }
78 | /**
79 | * getInfoLocation
80 | *
81 | * @param string $address
82 | * @param string $city
83 | * @param string $state
84 | * @return boolean
85 | */
86 | public function getInfoLocation ($address) {
87 | if (!empty($address)) {
88 | return $this->_connect($address);
89 | }
90 | return false;
91 | }
92 | /**
93 | * connect to Google Maps
94 | *
95 | * @param string $param
96 | * @return boolean
97 | */
98 | private function _connect($param) {
99 | $request_url = $this->_baseUrl . "&oe=utf-8&q=" . urlencode($param);
100 | $xml = simplexml_load_file($request_url);
101 | if (! empty($xml->Response)) {
102 | $point= $xml->Response->Placemark->Point;
103 | if (! empty($point)) {
104 | $coordinatesSplit = explode(",", $point->coordinates);
105 | // Format: Longitude, Latitude, Altitude
106 | $this->_latitude = $coordinatesSplit[1];
107 | $this->_longitude = $coordinatesSplit[0];
108 | }
109 | $this->_address= $xml->Response->Placemark->address;
110 | $this->_countryName= $xml->Response->Placemark->AddressDetails->Country->CountryName;
111 | $this->_countryNameCode= $xml->Response->Placemark->AddressDetails->Country->CountryNameCode;
112 | $this->_administrativeAreaName= $xml->Response->Placemark->AddressDetails->Country->AdministrativeArea->AdministrativeAreaName;
113 | $administrativeArea= $xml->Response->Placemark->AddressDetails->Country->AdministrativeArea;
114 | if (!empty($administrativeArea->SubAdministrativeArea)) {
115 | $this->_postalCode= $administrativeArea->SubAdministrativeArea->Locality->PostalCode->PostalCodeNumber;
116 | } elseif (!empty($administrativeArea->Locality)) {
117 | $this->_postalCode= $administrativeArea->Locality->PostalCode->PostalCodeNumber;
118 | }
119 | return true;
120 | } else {
121 | return false;
122 | }
123 | }
124 | /**
125 | * get the Postal Code
126 | *
127 | * @return string
128 | */
129 | public function getPostalCode () {
130 | return $this->_postalCode;
131 | }
132 | /**
133 | * get the Address
134 | *
135 | * @return string
136 | */
137 | public function getAddress () {
138 | return $this->_address;
139 | }
140 | /**
141 | * get the Country name
142 | *
143 | * @return string
144 | */
145 | public function getCountryName () {
146 | return $this->_countryName;
147 | }
148 | /**
149 | * get the Country name code
150 | *
151 | * @return string
152 | */
153 | public function getCountryNameCode () {
154 | return $this->_countryNameCode;
155 | }
156 | /**
157 | * get the Administrative area name
158 | *
159 | * @return string
160 | */
161 | public function getAdministrativeAreaName () {
162 | return $this->_administrativeAreaName;
163 | }
164 | /**
165 | * get the Latitude coordinate
166 | *
167 | * @return double
168 | */
169 | public function getLatitude () {
170 | return $this->_latitude;
171 | }
172 | /**
173 | * get the Longitude coordinate
174 | *
175 | * @return double
176 | */
177 | public function getLongitude () {
178 | return $this->_longitude;
179 | }
180 | }
--------------------------------------------------------------------------------
/README:
--------------------------------------------------------------------------------
1 | A light PHP class that uses the Google Maps API to provide geographic information from a generic address (city, country,street+city, etc).
2 |
3 | (C) Copyright 2009 Enrico Zimuel
4 | http://www.zimuel.it
5 |
--------------------------------------------------------------------------------
/example.php:
--------------------------------------------------------------------------------
1 | ';
12 | echo '';
13 |
14 | if (!empty($search)) {
15 |
16 | // Get the Google Maps Object
17 | $GMap = new GMaps($google_key);
18 |
19 | if ($GMap->getInfoLocation($search)) {
20 |
21 | echo 'Address: '.$GMap->getAddress().'
';
22 | echo 'Country name: '.$GMap->getCountryName().'
';
23 | echo 'Country name code: '.$GMap->getCountryNameCode().'
';
24 | echo 'Administrative area name: '.$GMap->getAdministrativeAreaName().'
';
25 | echo 'Postal code: '.$GMap->getPostalCode().'
';
26 | echo 'Latitude: '.$GMap->getLatitude().'
';
27 | echo 'Longitude: '.$GMap->getLongitude().'
';
28 |
29 | } else {
30 | echo "The response of Google Maps is empty";
31 | }
32 |
33 | }
--------------------------------------------------------------------------------