Still talking about web GIS here..
Reverse geocoder is a way of converting GPS coordinate (latitude, longitude) into human readable address.
Instead of JavaScript, we also can call reverse geocoder feature from Google Maps API via PHP. This is useful if you are somehow need to get user position in human readable format not only as latitude and longitude pair. You can then save this return value to database or just looking for a place name that has address similar to the returned value (addresses).
This is working example code in PHP:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | $lat='-7.620643645487575'; //latitude $lng='108.07807827343754'; //longitude $endpoint="http://maps.googleapis.com/maps/api/geocode/json?latlng=".trim($lat).",".trim($lng)."&sensor=false"; $raw=@file_get_contents($endpoint); $json_data=json_decode($raw); if ($json_data->status=="OK") { $fAddress=explode(",", $json_data->results[count($json_data->results)-2]->formatted_address); //this is human readable address --> getting province name var_dump($json_data->results); //dumping result } else { //if no result found, status would be ZERO_RESULTS echo $json_data->status; } |
Example result:
array(7) { [0]=> object(stdClass)#2 (4) { ["address_components"]=> array(6) { [0]=> object(stdClass)#3 (3) { ["long_name"]=> string(20) "Jalan Raya Cipatujah" ["short_name"]=> string(20) "Jalan Raya Cipatujah" ["types"]=> array(1) { [0]=> string(5) "route" } } [1]=> object(stdClass)#4 (3) { ["long_name"]=> string(10) "Tobongjaya" ["short_name"]=> string(10) "Tobongjaya" ["types"]=> array(2) { [0]=> string(11) "sublocality" [1]=> string(9) "political" } } [2]=> object(stdClass)#5 (3) { ["long_name"]=> string(9) "Cipatujah" ["short_name"]=> string(9) "Cipatujah" ["types"]=> array(2) { [0]=> string(8) "locality" [1]=> string(9) "political" } } [3]=> object(stdClass)#6 (3) { ["long_name"]=> string(11) "Tasikmalaya" ["short_name"]=> string(11) "Tasikmalaya" ["types"]=> array(2) { [0]=> string(27) "administrative_area_level_2" [1]=> string(9) "political" } } [4]=> object(stdClass)#7 (3) { ["long_name"]=> string(9) "West Java" ["short_name"]=> string(9) "West Java" ["types"]=> array(2) { [0]=> string(27) "administrative_area_level_1" [1]=> string(9) "political" } } [5]=> object(stdClass)#8 (3) { ["long_name"]=> string(9) "Indonesia" ["short_name"]=> string(2) "ID" ["types"]=> array(2) { [0]=> string(7) "country" [1]=> string(9) "political" } } } ["formatted_address"]=> string(42) "Jalan Raya Cipatujah, Cipatujah, Indonesia" ["geometry"]=> object(stdClass)#9 (4) { ["bounds"]=> object(stdClass)#10 (2) { ["northeast"]=> object(stdClass)#11 (2) { ["lat"]=> float(-7.634724) ["lng"]=> float(108.0905555) } ["southwest"]=> object(stdClass)#12 (2) { ["lat"]=> float(-7.6387314) ["lng"]=> float(108.0874122) } } ["location"]=> object(stdClass)#13 (2) { ["lat"]=> float(-7.636487) ["lng"]=> float(108.0886863) } ["location_type"]=> string(11) "APPROXIMATE" ["viewport"]=> object(stdClass)#14 (2) { ["northeast"]=> object(stdClass)#15 (2) { ["lat"]=> float(-7.634724) ["lng"]=> float(108.0905555) } ["southwest"]=> object(stdClass)#16 (2) { ["lat"]=> float(-7.6387314) ["lng"]=> float(108.0874122) } } } ["types"]=> array(1) { [0]=> string(5) "route" } } //[ ... cut here, too long ....] |
As you can see, Google Maps give you several level of human readable address. You may interested in “formatted_address” of results object. It is the human readable address that you can show directly to user. A way of calling it (example):
1 | $json_data->results[$the_index]->formatted_address; |
Other interesting object is address_components, you can, for example, get visitor’s state/province by looping of types array under each address_components object.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 | ["address_components"]=> array(4) { [0]=> object(stdClass)#45 (3) { ["long_name"]=> string(13) "Bantar Kalong" ["short_name"]=> string(13) "Bantar Kalong" ["types"]=> array(2) { [0]=> string(8) "locality" [1]=> string(9) "political" } } [1]=> object(stdClass)#46 (3) { ["long_name"]=> string(11) "Tasikmalaya" ["short_name"]=> string(11) "Tasikmalaya" ["types"]=> array(2) { [0]=> string(27) "administrative_area_level_2" [1]=> string(9) "political" } } [2]=> object(stdClass)#47 (3) { ["long_name"]=> string(9) "West Java" ["short_name"]=> string(9) "West Java" ["types"]=> array(2) { [0]=> string(27) "administrative_area_level_1" [1]=> string(9) "political" } } [3]=> object(stdClass)#48 (3) { ["long_name"]=> string(9) "Indonesia" ["short_name"]=> string(2) "ID" ["types"]=> array(2) { [0]=> string(7) "country" [1]=> string(9) "political" } } |
I tried your code above by saving it as .php file…But i am getting an error as
( ! ) Notice: Trying to get property of non-object in C:\wamp\www\index.php on line 10
Call Stack
# Time Memory Function Location
1 0.0008 368112 {main}( ) ..\index.php:0
( ! ) Notice: Trying to get property of non-object in C:\wamp\www\index.php on line 15
Call Stack
# Time Memory Function Location
1 0.0008 368112 {main}( ) ..\index.php:0