Posted By

tcam27 on 09/23/10


Tagged

google maps


Versions (?)

Who likes this?

6 people have marked this snippet as a favorite

vagrantradio
widgetyegg
mikael12
arucordoba
AllKnightAccess
Priestd09


Use Google Maps API to Get Latitude and Longitude


 / Published in: PHP
 

When creating a custom Google Map, the latitude and longitude for the place of interest are needed. Enter in the Address, City, and State, and the lat and lng pair will be returned.

  1. <?php
  2. function getLatandLong($addr,$city,$state)
  3. {
  4. global $lat;
  5. global $lng;
  6.  
  7. $doc = new DOMDocument();
  8. $doc->load("http://maps.google.com/maps/api/geocode/xml?address=".$addr.",+".$city.",+".$state."&sensor=false"); //input address
  9.  
  10. //traverse the nodes to get to latitude and longitude
  11. $results = $doc->getElementsByTagName("result");
  12. $results = $results->item(0);
  13. $results = $results->getElementsByTagName("geometry");
  14. $results = $results->item(0);
  15. $results = $results->getElementsByTagName("location");
  16.  
  17. foreach($results as $result)
  18. {
  19. $lats = $result->getElementsByTagName("lat");
  20. $lat = $lats->item(0)->nodeValue;
  21.  
  22. $lngs = $result->getElementsByTagName("lng");
  23. $lng = $lngs->item(0)->nodeValue;
  24. }
  25. }
  26. ?>

Report this snippet  

You need to login to post a comment.