0

私はここで途方に暮れています。ユーザーの位置を検出するためのすべての HTML5 ジオロケーション アイテムを見つけました。しかし、私がする必要があるのは、それらを最も近い場所の Web ページにリダイレクトすることです。そのため、サンアントニオ内またはその近く、おそらく特定の範囲 (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>

ここから先はグレーゾーンです。これは簡単で簡単なGoogle検索だと思いましたが、何も思いつきませんでした。

私はそれを理解しようとして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" ); 

}

私は金を打ったと思った。しかし、私は間違っていました :-/

4

0 に答える 0