我在这里不知所措。我找到了所有用于检测用户位置的 HTML5 地理位置项目。但我需要做的是将它们重定向到最近的位置网页。因此,如果他们在圣安东尼奥/附近,可能在一定范围内(50 英里),他们将被发送到/san-antonio/
网站上的页面。美国各地的其他地方也是如此。如果他们没有打开地理定位权限,他们会被定向到通用/locations/
页面。
我找到了这个获得职位的基本代码:
<script>
var x=document.getElementById("demo");
function getLocation()
{
if (navigator.geolocation)
{
navigator.geolocation.getCurrentPosition(showPosition);
}
else{x.innerHTML="Geolocation is not supported by this browser.";}
}
function showPosition(position)
{
x.innerHTML="Latitude: " + position.coords.latitude +
"<br>Longitude: " + position.coords.longitude;
}
</script>
从这里去哪里是一个灰色地带。我认为这将是简单易用的谷歌搜索,但我什么也没想到。
任何帮助都会很棒,因为我只花了 5 个小时试图弄清楚它并没有想出任何东西。
编辑:*在我们的整个服务器中搜索地理定位一词后,我刚刚在一个旧的客户端主题中找到了这个,但我对其进行了测试,但它没有给我正确的位置。我不知道它是否与不再起作用的 where.yahoo api 有关。在 header.php 文件中有:
if (geo_position_js.init()) {
geo_position_js.getCurrentPosition(success, null);
}
function lookup_location() {
geo_position_js.getCurrentPosition(success, show_map_error);
}
function success(loc) {
url='http://www.websiteexample.com/location/?long='+loc.coords.longitude+'&lat='+loc.coords.latitude;
location.href=url;
}
function show_map_error() {
}
然后在functions.php中有这样的:
function calc_distance($point1, $point2) {
$radius = 3958; // Earth's radius (miles)
$deg_per_rad = 57.29578; // Number of degrees/radian (for conversion)
$distance = ($radius * pi() * sqrt(
($point1['lat'] - $point2['lat'])
* ($point1['lat'] - $point2['lat'])
+ cos($point1['lat'] / $deg_per_rad) // Convert these to
* cos($point2['lat'] / $deg_per_rad) // radians for cos()
* ($point1['long'] - $point2['long'])
* ($point1['long'] - $point2['long'])
) / 180);
return $distance; // Returned using the units used for $radius.
}
function get_longlat($address) {
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,'http://where.yahooapis.com/geocode?appid=8gkElW6o&q='.urlencode($address));
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
$content = curl_exec($ch);
curl_close($ch);
$loc = simplexml_load_string($content)->Result;
$geo['lat'] = (string)$loc->latitude;
$geo['long'] = (string)$loc->longitude;
return $geo;
}
function redirect_to_nearest($clientlong, $clientlat) {
$clientgeo['long'] = $clientlong;
$clientgeo['lat'] = $clientlat;
$query = new WP_Query( array( 'post_type' => 'location' ) );
while ( $query->have_posts() ) : $query->the_post();
$custom = get_post_custom();
$slug = basename(get_permalink());
$location[$slug]['permalink'] = get_permalink();
$location[$slug]['address'] = $custom[google_maps_address][0];
$location[$slug]['geo'] = get_longlat($location[$slug][address]);
$location[$slug]['distance'] = calc_distance($clientgeo, $location[$slug]['geo']);
endwhile;
wp_reset_postdata();
usort($location,"cmp");
return $location[0]['permalink'];
}
function cmp($a, $b) {
if ($a['distance'] == $b['distance']) { return 0; }
return ($a['distance'] < $b['distance']) ? -1 : 1;
}
然后在实际的自定义帖子类型存档页面中有这样的:
if(isset($_GET[long]) && isset($_GET[lat])) {
$redirectto = redirect_to_nearest($_GET[long], $_GET[lat]);}
if(isset($redirectto) && preg_match("/http:\/\/www.websiteexample.com\/location\/.*/i", $redirectto)) {
header( "HTTP/1.1 303 See Other" );
header( "Location: $redirectto" );
}
我以为我已经打到金子了。但是我错了 :-/