How do I display nearby places in PHP based on geo-location?

ImagesAndWords

StayblCam.com
Nov 7, 2006
2,326
90
0
Scottsdale, Arizona
www.stayblcam.com
I've been playing around with the GeoPlugin script, but I can't get the "nearby places" to work. Basically, it takes your current location and finds nearby towns within a given radius.
geoPlugin's free and easy PHP geolocation webservice explained
geoPlugin's free and easy geolocation webservices explained

I'm using this example:
http://www.geoplugin.com/_media/webservices/example.phps?id=webservices:php&cache=cache

But all I get is one output for my current location, and nothing else.

What I would like is to make a call to the nearby places function when the page loads, and then be able to use the different "places" returned in various spots into the code, dynamically like this:

...You live in Seattle. Some other areas around you would be Everett and Bellevue. But that's not where Mary lives. She lives in Issaquah or Tacoma, I can't remember which...
Can any of code-heads explain to me how I would do this using this script?

Gracias! ;)
 


Here's the original code from the bottom of the script you linked to:

Code:
$nearby = $geoplugin->nearby();

if ( isset($nearby[0]['geoplugin_place']) ) {

	echo "<pre><p>Some places you may wish to visit near " . $geoplugin->city . ": </p>\n";

	foreach ( $nearby as $key => $array ) {
		
		echo ($key + 1) .":<br />";
		echo "\t Place: " . $array['geoplugin_place'] . "<br />";
		echo "\t Region: " . $array['geoplugin_region'] . "<br />";
		echo "\t Latitude: " . $array['geoplugin_latitude'] . "<br />";
		echo "\t Longitude: " . $array['geoplugin_longitude'] . "<br />";
	}
	echo "</pre>\n";
}

Note the following line in the code:

Code:
$nearby = $geoplugin->nearby();

The city names are at $nearby['geoplugin_place']
So the first 3 items would be at
$nearby[0]['geoplugin_place']
$nearby[1]['geoplugin_place']
$nearby[2]['geoplugin_place']

With this code, it won't do the replacement if the number of cities
returned by nearby() is less than a specified number ($want_to_use)

If there are more cities that you wish to use, it will only do the first n cities, where n is $want_to_use

Code:
$str = "You live in |0|. Some other areas around you would be |1| and |2|. But that's not where Mary lives. She lives in |3| or |4|, I can't remember which";

$want_to_use = 5; // change this to the number of cities you want to use
$count_cities = count($nearby);// determine how many nearby cities you have
   $cities = array("|0|","|1|","|2|","|3|","|4|");
   $replacements = array();

if($want_to_use >= $count_cities)//if you don't have enough cities, you'll have to adjust
{
   
   for($i=0;$i<$want_to_use;$i++) // build a new 1-D array
   {
      if ( isset($nearby[$i]['geoplugin_place']) )     
         $replacements[] = $nearby[$i]['geoplugin_place'];
   }

   $replaced_str = str_replace($cities, $replacements, $str);
   
}
else
{
   //if you want to do something different if there are less cities than you need to use, do it here

}
 
  • Like
Reactions: ImagesAndWords
Thank you very much for your help, and providing a new code example. +rep

Unfortunately though, I still can't get any results returned with this - even if I set the $radius as high as 100 (miles to look for cities within).

Does the script find some cities and work on your end?
 
If you used the example code above but didn't have 5 nearby cities in the array, then you would output nothing. If that's the case then you need to add some code to the "else" area in jryan's code.

Have you attempted to print the "nearby" array to see if it contains anything?
 
If you used the example code above but didn't have 5 nearby cities in the array, then you would output nothing. If that's the case then you need to add some code to the "else" area in jryan's code.

Have you attempted to print the "nearby" array to see if it contains anything?

Yeah I did. $count_cities contains 1, but the array seems to be empty. I tried setting the $want_to_use to 1 as well.
 
do this:

Code:
require_once('geoplugin.class.php');
$geoplugin = new geoPlugin();
//$geoplugin->locate();
$nearby = $geoplugin->nearby();
var_dump($nearby);

and see what it prints out. Should be a whole bunch of text. Look at the output in 'View Source' in your browser and it should be more readable.

If it is empty, remove the comment from the 3rd line and try again.
 
do this....
and see what it prints out. Should be a whole bunch of text. Look at the output in 'View Source' in your browser and it should be more readable.

If it is empty, remove the comment from the 3rd line and try again.

When I tried without the 3rd line I got an error about missing longitude and latitude. When I removed the comment, I got this in return (also as page source) :

array(1) {
[0]=>
array(0) {
}
}
I also tried with and without setting the $radius and $maxresults before calling geoplugin->locate.
 
do a var_dump on geoplugin:

Code:
$geoplugin = new geoPlugin();
var_dump($geoplugin);

if it shows empty like above, it's not finding the include file properly because it should, at the very least, show a default $host and $currency.
 
OK, I just put the same script up on my site and am not getting nearby either. The error you were getting with latitude/longitude is because they were probably null. Technically, it checks for a numeric value and won't do the lookup otherwise.

I tried a direct call with the latitude/longitude from my location at :
http://www.geoplugin.net/extras/nearby.gp?lat=XXX&long=YYY&format=php

replacing XXX and YYY and got an empty array, so it look like an issue between the web service and supplied values; maybe they haven't worked out all the bugs in it yet.
 
OK, I just put the same script up on my site and am not getting nearby either. The error you were getting with latitude/longitude is because they were probably null. Technically, it checks for a numeric value and won't do the lookup otherwise.

I tried a direct call with the latitude/longitude from my location at :
http://www.geoplugin.net/extras/nearby.gp?lat=XXX&long=YYY&format=php

replacing XXX and YYY and got an empty array, so it look like an issue between the web service and supplied values; maybe they haven't worked out all the bugs in it yet.

Yeah you know, I actually tested that webservice like two weeks ago but forgot about trying it again. When I first had tested it, I remember now that it did return some cities near me.

Now, like you, I no longer get results from the webservice call either so something must be up on their end. I will contact them to see what's up, and update this thread later.

Thanks for your help so far. ;)
 
I received the following response from geoPlugin today:

About 48 hours ago, we came under a massive attack (150mbs+) by a multitude of IP addresses (many from cloud servers that use many thousands of IPs). This pushed up our monthly hosting bill by some $400 which is was unacceptable for a not-for-profit service.

In an effort to avert the system coming down a lot of IP addresses were blocked at the firewall. It is possible your IP was included in the ones blocked.

Please provide me with your IP address (your server's IP) so I can see if it is listed in the firewall and if so, why.

Additionally, to avert similar problems in the future, we will be requiring all websites that use geoPlugin to credit geoPlugin for its geolocation services with the link back. That way our crawler will auto-block IPs at the firewall for a pre-determined time if a link-back is not found.

Alternatively donations received from users will whitelist their IP against blacklisting (reasonable use policy applies).

Hope this clarifies things
Best regards

----
geoPlugin Support