/ 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.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
<?php function getLatandLong($addr,$city,$state) { global $lat; global $lng; $doc = new DOMDocument(); $doc->load("http://maps.google.com/maps/api/geocode/xml?address=".$addr.",+".$city.",+".$state."&sensor=false"); //input address //traverse the nodes to get to latitude and longitude $results = $doc->getElementsByTagName("result"); $results = $results->item(0); $results = $results->getElementsByTagName("geometry"); $results = $results->item(0); $results = $results->getElementsByTagName("location"); foreach($results as $result) { $lats = $result->getElementsByTagName("lat"); $lat = $lats->item(0)->nodeValue; $lngs = $result->getElementsByTagName("lng"); $lng = $lngs->item(0)->nodeValue; } } ?>