Reputation: 127
I need to show a different phone number on the homepage based on the user's ip location. It will be for a dynamic and changing number of store locations. So if a user visits the site and their ip location is within 50 miles of one of our stores, that store number will be displayed. If we do not have a store within 50 miles of their location, the default number will be displayed. When we open a new store, we will add the phone number and city (or zip, whatever is needed) into a custom field within Wordpress. This data is what I need to use to do the geotargeting.
I have read countless articles and know I need to connect to a database like max mind, but I can't figure out how to do the radius part. Can anyone point me in the right direction? A plugin or free service that can get me most of the way there?
Thank you.
Upvotes: 1
Views: 3720
Reputation: 91
A number of things are required.
Browsers will let the user(s) know that you are attempting to detect their location and give the user(s) an option to decline the geolocation attempt. Should they do so, you need to have a fallback such as a search page for your stores or display a particular store's number instead e.g. the headquarters.
If the user(s) accept(s) the geolocation attempt, you could calculate their nearness based on a number of calculations, such as the one provided in step 3 of this tutorial. For working examples, please check out Bjorn's store locator plugin which works flawlessly and comes with many UI options and a working radius detection visible in the search results of any of the demo pages. You can also dig into his code to see how he calculated the radius or which third party functions he used to do so.
If you are implementing this in wordpress, you might need to write a simple plugin for it and create a simple shortcode to use along with it or add your function to the functions.php file, although it is preferrable to write a plugin and keep your work organized and easier to edit/maintain later on as a result.
Upvotes: 2
Reputation: 10111
take a look at http://www.geoplugin.com/webservices/php
echo var_export(unserialize(file_get_contents('http://www.geoplugin.net/php.gp?ip='.$_SERVER['REMOTE_ADDR'])));
Output
array ( 'geoplugin_request' => '50.143.52.48', 'geoplugin_status' => 200, 'geoplugin_credit' => 'Some of the returned data includes GeoLite data created by MaxMind, available from http://www.maxmind.com.', 'geoplugin_city' => 'Pompano Beach',
'geoplugin_region' => 'FL', 'geoplugin_areaCode' => '954',
'geoplugin_dmaCode' => '528', 'geoplugin_countryCode' => 'US',
'geoplugin_countryName' => 'United States',
'geoplugin_continentCode' => 'NA', 'geoplugin_latitude' => '26.2442', 'geoplugin_longitude' => '-80.205902',
'geoplugin_regionCode' => 'FL', 'geoplugin_regionName' => 'Florida', 'geoplugin_currencyCode' => 'USD', 'geoplugin_currencySymbol' => '$', 'geoplugin_currencySymbol_UTF8' => '$',
'geoplugin_currencyConverter' => '1', )
PHP Class
<?php
require_once('geoplugin.class.php');
$geoplugin = new geoPlugin();
// If we wanted to change the base currency, we would uncomment the following line
// $geoplugin->currency = 'EUR';
$geoplugin->locate();
echo "Geolocation results for {$geoplugin->ip}: <br />\n".
"City: {$geoplugin->city} <br />\n".
"Region: {$geoplugin->region} <br />\n".
"Area Code: {$geoplugin->areaCode} <br />\n".
"DMA Code: {$geoplugin->dmaCode} <br />\n".
"Country Name: {$geoplugin->countryName} <br />\n".
"Country Code: {$geoplugin->countryCode} <br />\n".
"Longitude: {$geoplugin->longitude} <br />\n".
"Latitude: {$geoplugin->latitude} <br />\n".
"Currency Code: {$geoplugin->currencyCode} <br />\n".
"Currency Symbol: {$geoplugin->currencySymbol} <br />\n".
"Exchange Rate: {$geoplugin->currencyConverter} <br />\n";
if ( $geoplugin->currency != $geoplugin->currencyCode ) {
//our visitor is not using the same currency as the base currency
echo "<p>At todays rate, US$100 will cost you " . $geoplugin->convert(100) ." </p>\n";
}
/* find places nearby */
$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 Country Code: " . $array['geoplugin_countryCode'] . "<br />";
echo "\t Region: " . $array['geoplugin_region'] . "<br />";
echo "\t County: " . $array['geoplugin_county'] . "<br />";
echo "\t Latitude: " . $array['geoplugin_latitude'] . "<br />";
echo "\t Longitude: " . $array['geoplugin_longitude'] . "<br />";
echo "\t Distance (miles): " . $array['geoplugin_distanceMiles'] . "<br />";
echo "\t Distance (km): " . $array['geoplugin_distanceKilometers'] . "<br />";
}
echo "</pre>\n";
}
?>
This will output:
Geolocation results for 50.143.52.48:
City: Pompano Beach
Region: FL
Area Code: 954
DMA Code: 528
Country Name: United States
Country Code: US
Longitude: -80.205902
Latitude: 26.2442
Currency Code: USD
Currency Symbol: $
Exchange Rate: 1
Some places you may wish to visit near Pompano Beach:
1:
Place: Whispering Pines Addition Mobile Home Park
Country Code: US
Region: Florida
County:
Latitude: 26.2711000
Longitude: -80.1442900
Distance (miles): 4.25
Distance (km): 6.83
2:
Place: Coral Springs
Country Code: US
Region: Florida
County:
Latitude: 26.2711900
Longitude: -80.2706000
Distance (miles): 4.42
Distance (km): 7.12
3:
Place: Pompano Beach
Country Code: US
Region: Florida
County:
Latitude: 26.2378600
Longitude: -80.1247700
Distance (miles): 5.05
Distance (km): 8.12
4:
Place: Rock Island
Country Code: US
Region: Florida
County:
Latitude: 26.1550900
Longitude: -80.1769900
Distance (miles): 6.41
Distance (km): 10.32
5:
Place: Lighthouse Point
Country Code: US
Region: Florida
County:
Latitude: 26.2756400
Longitude: -80.0872700
Distance (miles): 7.66
Distance (km): 12.34
Upvotes: 1