我需要根据用户的ip位置在主页上显示不同的电话号码。这将是一个动态和不断变化的商店位置数量。因此,如果用户访问该网站并且他们的ip位置距离我们的某个商店不到50英里,则将显示该商店编号。如果我们在距离其位置50英里的范围内没有商店,则会显示默认编号。当我们打开一个新商店时,我们会在Wordpress的自定义字段中添加电话号码和城市(或zip,无论需要什么)。这些数据是我需要用来进行地理定位的。
我已阅读过无数文章,并知道我需要连接到像大脑这样的数据库,但我无法弄清楚如何做半径部分。谁能指出我正确的方向?一个插件或免费服务可以让我大部分时间到那里?
谢谢。
答案 0 :(得分:2)
需要做很多事情。
浏览器会让用户知道您正在尝试检测其位置,并向用户提供拒绝地理位置尝试的选项。如果他们这样做,您需要有一个后备,例如商店的搜索页面或显示特定商店的号码,例如总部。
如果用户接受地理定位尝试,您可以根据许多计算来计算其接近度,例如this tutorial的步骤3中提供的计算。有关工作示例,请查看Bjorn's store locator plugin,它可以完美运行,并提供许多UI选项,并在任何演示页面的搜索结果中显示工作半径检测。您还可以深入研究他的代码,看看他是如何计算半径的,或者他曾经使用过哪些第三方函数。
如果你在wordpress中实现这个,你可能需要为它编写一个简单的插件并创建一个简单的短代码以与它一起使用或将你的函数添加到functions.php文件中,尽管它更适合于写一个插件,让你的工作井井有条,以后更容易编辑/维护。
答案 1 :(得分:1)
看看http://www.geoplugin.com/webservices/php
echo var_export(unserialize(file_get_contents('http://www.geoplugin.net/php.gp?ip='.$_SERVER['REMOTE_ADDR'])));
输出
array('geoplugin_request'=> '50 .143.52.48','geoplugin_status' => 200,'geoplugin_credit'=> '一些返回的数据包括MaxMind创建的GeoLite数据,可从中获取 http://www.maxmind.com。','geoplugin_city'=> '庞帕诺海滩',
'geoplugin_region'=> 'FL','geoplugin_areaCode'=> '954',
'geoplugin_dmaCode'=> '528','geoplugin_countryCode'=> '美国',
'geoplugin_countryName'=> '美国',
'geoplugin_continentCode'=> 'NA','geoplugin_latitude'=> '26 .2442','geoplugin_longitude'=> '-80.205902',
'geoplugin_regionCode'=> 'FL','geoplugin_regionName'=> '佛罗里达', '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";
}
?>
这将输出:
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