0

I've got some code that takes any address and returns lat and long as a php variable.

if ($_SESSION['where']) { $where = stripslashes($_SESSION['where']); $whereurl = urlencode($where); $location = file("http://maps.google.com/maps/geo?q=new+york+New+york &output=csv&key=xxxxxxxxxxxxxxxxxxxxxxxx"); list ($stat,$acc,$north,$east) = explode(",",$location[0]); $html = "Information for ".$where."<br>"; $html .= "North: $north, East: $east<br>"; $html .= "Accuracy: $acc, Status: $stat<br>"; } else { $html = "Varibles Undefined"; } ?> <?php $_SESSION['lat'] = "$html"; $_SESSION['lon'] = "$whereurl"; echo"<strong>"?><?php echo "$north";?>&deg; North, <?php echo "$east";?>&deg; East</strong> 

I know it works because when I enter

http://maps.google.com/maps/geo?q=$whereurl &output=csv&key=xxxxxxxxxxxxxxxxxxxx 

manually into a browser, it returns

200,4,40.7143528,-74.0059731 

which is what I need saved as a PHP variable. However it's not saving it as $north or $east. Any suggestions on how to fix this? Thank's in advance.

here's what it gives me:

{ "name": "new york new york", "Status": { "code": 200, "request": "geocode" }, "Placemark": [ { "id": "p1", "address": "New York, NY, USA", "AddressDetails": { "Accuracy" : 4, "Country" : { "AdministrativeArea" : { "AdministrativeAreaName" : "NY", "SubAdministrativeArea" : { "Locality" : { "LocalityName" : "New York" }, "SubAdministrativeAreaName" : "New York" } }, "CountryName" : "USA", "CountryNameCode" : "US" } }, "ExtendedData": { "LatLonBox": { "north": 40.8495342, "south": 40.5788964, "east": -73.7498543, "west": -74.2620919 } }, "Point": { "coordinates": [ -74.0059731, 40.7143528, 0 ] } }, { "id": "p2", "address": "Manhattan, New York, NY, USA", "AddressDetails": { "Accuracy" : 4, "Country" : { "AdministrativeArea" : { "AdministrativeAreaName" : "NY", "SubAdministrativeArea" : { "Locality" : { "DependentLocality" : { "DependentLocalityName" : "Manhattan" }, "LocalityName" : "New York" }, "SubAdministrativeAreaName" : "New York" } }, "CountryName" : "USA", "CountryNameCode" : "US" } }, "ExtendedData": { "LatLonBox": { "north": 40.8200450, "south": 40.6980780, "east": -73.9033130, "west": -74.0351490 } }, "Point": { "coordinates": [ -73.9662495, 40.7834345, 0 ] } } ] }

16
  • How is it related to javascript? manually into a browser, it retruns may be it has an empty line in front. Try to replace file with file_get_contents and remove [0] after $location Commented Feb 24, 2012 at 20:54
  • @Cheery Did both those things, still not registering the two variables. Commented Feb 24, 2012 at 21:19
  • In that case we need example of the request to google maps. Or show us the result of var_dump($location); Commented Feb 24, 2012 at 21:21
  • here's an example of the request url maps.google.com/maps/… is that what you mean? Commented Feb 24, 2012 at 22:27
  • And it works perfectly. Check what do you actually have in the script as the result of request, insert var_dump($location); after the line with file and check the output. Commented Feb 24, 2012 at 22:29

1 Answer 1

0

Use file_get_contents instead (which returns you a string).

Short Example:

$location = file_get_contents("http://maps.google.com/maps/geo?q=$whereurl&output=csv"); $test = explode(",",$location); print_r($test); 

Full Solution here:

http://www.ece.uvic.ca/~casualt/random.php?location=Bermuda

Source:

$theLocation = "new+york"; $locIn = $_GET['location']; if($locIn) { $theLocation = urlencode($locIn); //Just to be safe (even though this got passed in via get } $location = file_get_contents('http://maps.google.com/maps/geo?q='.$theLocation.'&output=csv&key=AIzaSyAQtiJHUt5KiL-aGONLlACQQ3OdwpbvFzI'); $test = explode(",",$location); //print_r($test); $lat = $test[2]; $long = $test[3]; echo '<h1>Magical Location of '.$theLocation.':</h1>'; echo '<ul>'; echo '<li>Longitude: '.$long.'</li>'; echo '<li>Latitude: '.$lat.'</li>'; echo '</ul>'; 

That should do the trick for you. :)

Sign up to request clarification or add additional context in comments.

1 Comment

Still not working... When I echo $html it produces Information for New york, New york: , East: Accuracy: , Status: and Session Where is set properly.