free hit counter

Get latitude and longitude based on location name google maps api

Get latitude and longitude based on location name google maps api

Get latitude and longitude based on location name google maps api

how to get Latitude and Longitude in Google Maps using PHP

 

The idea is simple. Make a request to Google Maps server, and it will return an XML (or JSON). Then, parse the XML to get the Lat and Lon.

  1. function Get_Lat_lng($address=null)  
  2. {  
  3. if(!emptyempty($address)){  
  4. $prepAddr = str_replace(‘ ‘,‘+’,$address);  
  5. $geocode=file_get_contents(‘http://maps.google.com/maps/api/geocode/json?address=’.$prepAddr.‘&sensor=false’);  
  6. $output= json_decode($geocode, true);  
  7. if($output[‘status’] ==‘OK’)  
  8. {  
  9. $lat = $output[‘results’][0][‘geometry’][‘location’][‘lat’];  
  10. $lng = $output[‘results’][0][‘geometry’][‘location’][‘lng’];  
  11. $map_code=array(‘status’=>‘ok’,‘lat’=>$lat,‘lng’=>$lng);  
  12. return $map_code;  
  13. }  
  14. else{  
  15. $map_code=array(‘status’=>‘zero’,‘lat’=>‘00.00’,‘lng’=>‘00.00’);  
  16. return $map_code;  
  17. }  
  18. }else{  
  19. $map_code=array(‘status’=>‘zero’,‘lat’=>‘00.00’,‘lng’=>‘00.00’);  
  20. return $map_code;  
  21. }  
  22. }  

 

Now just call  Get_Lat_lng($address) and you get latitude and longitude  in array 

for eg.   $address= ” 1600 Amphitheatre Parkway Mountain View, CA 94043 USA”;   (google usa office address)

Array
(
    [status] => ok
    [lat] => 37.4221913
    [lng] => -122.0845853
)

Live Demo here





				
			

Leave a Reply