2016-03-29 5 views
0

私は、最も近い町/市および私のウェブサイトを閲覧している人の郡を見つける方法を探しています。最も近い町/市を探す

$town = "London, England"; 

か何かのように:その後、私はそれはのようなものになりたい

$town = "Bacup, Lancashire GB"; 

Googleはこのような何かをするために管理しています。何かを検索すると、ページの下部に「あなたの村 - インターネットアドレスから」と表示されます。あなたのウェブサイトを見ている人の位置を知りたいので

おかげ

+0

なぜですか?あなたの理由は何ですか? – GeorgeWL

+0

あなたが後にする可能性が最も高いのは「[geolocation](https://en.wikipedia.org/wiki/Geolocation)」です。 GogleにはAPIが用意されています。ザッツあなたは何を意味するか http://stackoverflow.com/questions/4724971/is-there-a-good-php-geolocation-service関連する可能性がある 場合、PHP、HTMLとjQueryのために作られたスニペットやライブラリ準備があります http://www.w3schools.com/html/html5_geolocation.asp http://html5demos.com/geo https://developers.google.com/maps/documentation/javascript/examples/map-geolocation 注意:現代のブラウザは、ジオロケーションを取得しようとする前にユーザーの許可を求めます。 – GeorgeWL

+0

許可が必要な場合はうん、うまくいくでしょう。ユーザーがGPSを搭載したモバイル/タブレット端末を閲覧している場合は、自動的に使用しますか? @ TheGeorgeL – CoolCodeGuy

答えて

1

は、データがクライアントマシンから取得されます。 使用

navigator.geolocation.getCurrentPosition(showPosition); 

クライアントの場所を取得します。 showPositionは渡される関数名なので、この名前の関数を定義します。

function showPosition(position) { 
    var x = document.getElementById("demo"); 
    x.innerHTML = "Latitude: " + position.coords.latitude + 
    "<br>Longitude: " + position.coords.longitude; 
} 

をそれとも、あまりにも自分のサーバーにデータを送信することができます:あなただけの唯一のクライアント・マシン上の場所を表示したい場合は、次のように行います。

あなたは今、あなたは場所名を取得するためにGoogleマップの位置情報サービスAPIを使用し、(どのように正確で、見つける?)の座標を持ってhttps://developer.mozilla.org/en-US/docs/Web/API/Geolocation/getCurrentPosition

でこれについてもう少し情報を得ることができます。 GoogleマップのジオロケーションAPI - https://developers.google.com/maps/documentation/geolocation/intro

+1

もちろん、彼は許可するかどうかを選択するように求められます... –

+0

これは多くのソリューションの1つです。 – GeorgeWL

+0

Chrome 5.0以上、IE 9.0以上、Firefox 3.5以上、Safari 5.0以上、Opera 16用。0 + などのように、検証に入れてください: 'if(navigator.geolocation){ navigator.geolocation.getCurrentPosition(success); } else { エラー( 'ジオロケーションはサポートされていません'); } ' – GeorgeWL

1

ジオ-IP情報をチェックする

要求地理IP-サーバー(netip.de)を取得し、IPが配置されているリターン(ホスト、州、国、町)。

<?php 
    $ip='94.219.40.96'; 
    print_r(geoCheckIP($ip)); 
    //Array ([domain] => dslb-094-219-040-096.pools.arcor-ip.net [country] => DE - Germany [state] => Hessen [town] => Erzhausen) 

    //Get an array with geoip-infodata 
    function geoCheckIP($ip) 
    { 
      //check, if the provided ip is valid 
      if(!filter_var($ip, FILTER_VALIDATE_IP)) 
      { 
        throw new InvalidArgumentException("IP is not valid"); 
      } 

      //contact ip-server 
      [email protected]_get_contents('http://www.netip.de/search?query='.$ip); 
      if (empty($response)) 
      { 
        throw new InvalidArgumentException("Error contacting Geo-IP-Server"); 
      } 

      //Array containing all regex-patterns necessary to extract ip-geoinfo from page 
      $patterns=array(); 
      $patterns["domain"] = '#Domain: (.*?)&nbsp;#i'; 
      $patterns["country"] = '#Country: (.*?)&nbsp;#i'; 
      $patterns["state"] = '#State/Region: (.*?)<br#i'; 
      $patterns["town"] = '#City: (.*?)<br#i'; 

      //Array where results will be stored 
      $ipInfo=array(); 

      //check response from ipserver for above patterns 
      foreach ($patterns as $key => $pattern) 
      { 
        //store the result in array 
        $ipInfo[$key] = preg_match($pattern,$response,$value) && !empty($value[1]) ? $value[1] : 'not found'; 
      } 

      return $ipInfo; 
    } 

?> 
+0

html5ブラウザではこれは廃止されました。これはhttp://www.w3schools.com/html/html5_geolocation.asp要素でのみ可能です。 – GeorgeWL

+0

私は、とにかくユーザーに尋ねることをお勧めします。だから、許可なく行なわれることに怒られることはありません。 – GeorgeWL

+1

要件ごとに。良いもの@TheGeorgeL –