地理位置重定向到某个网页

时间:2013-09-16 15:27:25

标签: html5 geolocation

我在这里不知所措。我找到了所有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个小时试图找出它并且什么都没有。

编辑:* 在我们的整个服务器上搜索单词geolocation之后,我在旧的客户端主题中找到了这个,但我测试了它并没有给我正确的位置。我不知道它是否有什么事情可以使用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" ); 

}

我以为我已经打了金牌。但我错了: - /

0 个答案:

没有答案