Convert Latitude,Longitude to Address Using Android,Php

We get location and latitude using LocationManager Class in android. We can get address using that also.

Android Code

-------------------------------------------------------------------------------------------
private String getName(String lat,String lng) throws IOException
{
String add="";
Geocoder geocoder = new Geocoder(this, Locale.getDefault());
try
{
               List addresses = geocoder.getFromLocation(lat, lng, 1);
Address obj = addresses.get(0);
             add = obj.getAddressLine(0) +" "+obj.getAddressLine(1)+" "                                                          + obj.getAddressLine(2);
}
catch(Exception ex)
{
Toast.makeText(this,"exception"+ex.getMessage(),Toast.LENGTH_LONG).show();            
}
return add;
}
-------------------------------------------------------------------------------------------

Php Code

Also Using Php , we can get address from longitude,latitude.

-------------------------------------------------------------------------------------------
$lat="21.6265";
$long="73.0118";
$url="http://maps.googleapis.com/maps/api/geocode/json?latlng=".$lat.",".$long."&sensor=true";

$init = curl_init();

curl_setopt($init, CURLOPT_URL, $url);
curl_setopt($init, CURLOPT_HEADER,0);
curl_setopt($init, CURLOPT_USERAGENT, $_SERVER["HTTP_USER_AGENT"]);
curl_setopt($init, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($init);
curl_close($init);

echo $response;


?>
-------------------------------------------------------------------------------------------


(Json Format)

(XML Format)

In Url you can specify whether you want output in json or xml. Once you get output, you need to extract the address using the string operations.

No comments:

Post a Comment