How To Validate Address By Google Reverse GeoLocation API
M
ikky is web developer. She is implementing address book application in PHP
programming language in which users updates their address and other users can see those contact addresses y entering user’s name. Here, she wants to validate address entered by users that address entered by user is actual or not. Validating address is difficult as we never have accurate address data of all over the world. There are many free as well as paid map apis available on internet such as Yahoo Place Finder, OpenAddresses, MapQuest, Google Maps API, OpenLayers, Microsoft Bing Maps etc.
Term “Reverse geocoding” is the process of converting geographic coordinates into a human-readable address. You can also get more idea about reverse geocoding from here. Google Maps Geocoding API is easy to use as it provides access reverse geocoding service via an HTTP request.
In this tutorial we will validate address entered by with google’s geocoding api.
Create simple HTML form which inputs address from user.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
<!DOCTYPE html>
<html>
<head>
<title>Geocoding API</title>
</head>
<body>
<form method="post" name="frmProfile">
<div>Your Name:</div>
<div>
<input type="text" name="txtName" />
</div>
<div>Address:</div>
<div>
<input type="text" name="txtAddress" />
</div>
<div>
<input type="submit" name="submit"/>
</div>
</form>
</body>
</html>
|
Here, we have taken two inputs from user. In address, User can enter his/her address with area name, area code, city, zip code, state and country.
Handle form request and validate address.
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
|
<?php
function getGeoLocation($address){
$country = $state = $city ='';
$address = str_replace(" ", "+", "$address");
$gioUrl = "http://maps.google.com/maps/api/geocode/json?address=$address&sensor=false";
$result = file_get_contents($gioUrl);
$json = json_decode($result);
$fullAddress = '';
foreach ($json->results as $result){
foreach($result->address_components as $addressPart) {
if((in_array('locality', $addressPart->types)) && (in_array('political', $addressPart->types))){
$city .= trim($addressPart->long_name).",";
} else if ((in_array('administrative_area_level_1', $addressPart->types) && (in_array('political', $addressPart->types))) || (in_array('administrative_area_level_2', $addressPart->types) && (in_array('political', $addressPart->types)))){
$state .= trim($addressPart->long_name).",";
} else if((in_array('country', $addressPart->types)) && (in_array('political', $addressPart->types))){
$country .= trim($addressPart->long_name).",";
} else {
$fullAddress .= $addressPart->long_name.", ";
}
}
}
$fullAddress = trim($fullAddress, ",");
$response["country"] = $country;
$response["state"] = $state;
$response["city"] = $city;
$response["address"] = $fullAddress;
return $response;
}
if(!empty($_POST['txtName']) && !empty($_POST['txtAddress'])){
$response = getGeoLocation($_POST['txtAddress']);
print_r($response);
}
?>
|
index.php
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
59
60
61
62
63
64
|
<?php
function getGeoLocation($address){
$country = $state = $city ='';
$address = str_replace(" ", "+", "$address");
$gioUrl = "http://maps.google.com/maps/api/geocode/json?address=$address&sensor=false";
$result = file_get_contents($gioUrl);
$json = json_decode($result);
$fullAddress = '';
print_r($json->results); // Print response from api.
foreach ($json->results as $result){
foreach($result->address_components as $addressPart) {
if((in_array('locality', $addressPart->types)) && (in_array('political', $addressPart->types))){
$city .= trim($addressPart->long_name).",";
} else if ((in_array('administrative_area_level_1', $addressPart->types) && (in_array('political', $addressPart->types))) || (in_array('administrative_area_level_2', $addressPart->types) && (in_array('political', $addressPart->types)))){
$state .= trim($addressPart->long_name).",";
} else if((in_array('country', $addressPart->types)) && (in_array('political', $addressPart->types))){
$country .= trim($addressPart->long_name).",";
} else {
$fullAddress .= $addressPart->long_name.", ";
}
}
}
$fullAddress = trim($fullAddress, ",");
$response["country"] = $country;
$response["state"] = $state;
$response["city"] = $city;
$response["address"] = $fullAddress;
return $response;
}
if(!empty($_POST['txtName']) && !empty($_POST['txtAddress'])){
$response = getGeoLocation($_POST['txtAddress']);
echo "<pre>";
print_r($response);
echo "</pre>";
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Geocoding API</title>
</head>
<body>
<form method="post" name="frmProfile">
<div>Your Name:</div>
<div>
<input type="text" name="txtName" />
</div>
<div>Address:</div>
<div>
<input type="text" name="txtAddress" />
</div>
<input type="submit" name="submit">
</form>
</body>
</html>
|
$json->results
holds response from google maps api. Api provides Location details based on the input from including address1, address2, state, country, formatted address, geometry details such as bounds, longitudes and latitude, location type and place id.
place_id – Holds value of place
address_components – standard class object having details of address
formatted_address – Formatted address
geometry – provides geometry details of address such as bounds, latitude, longitude etc.
location_type – type of location Ex. APPROXIMATE
Output:
1
2
3
4
5
6
7
|
Array
(
[country] => India,
[state] => Rajkot,Gujarat,
[city] => Rajkot,
[address] =>
)
|
Suppose we have entered plain address “rajkot,india”. So, GeoCoding api will split input and provides exact address, city, state and country. It also provides other details such as latitude, longitude, Geo Location Area etc. We will use other details in next tutorials.
Read More
jeslin
I simply want to tell you that I’m all new to blogs and truly liked you’re blog site. Very likely I’m likely to bookmark your site .You surely come with remarkable articles. Cheers for sharing your website page.