0

私のウェブサイトでバナーに地理位置情報を使用する必要があります。良いと思われるこのライブラリを見つけました:

私のページにこのコードを追加すると、ブラウザに位置の取得を許可するアラートが表示されます。

コード:

<script type="text/javascript">
    if(geo_position_js.init()){
        geo_position_js.getCurrentPosition(success_callback,error_callback,{enableHighAccuracy:true});
    }
    else{
        alert("Functionality not available");
    }

    function success_callback(p)
    {

        var latitudine = +p.coords.latitude.toFixed(2);
        var longitudine = p.coords.longitude.toFixed(2);

        alert(latitudine+' - '+longitudine);
    }

    function error_callback(p)
    {
        alert('error='+p.message);
    }                       

</script>

アラートは問題なく表示されますが、市に別のバナーを読み込ませる必要があります。どうやってやるの?

このサービスも見つけました: findNearbyPlaceName

緯度と経度から国などを見つけることができますが、この情報をどのように使用できますか? xml または json を使用したコールバックがあることがわかりました。このコードを使用して URL を取得しようとしましたが、ブラウザーにアラートが表示されないため、間違っています。

<script type="text/javascript">
    if(geo_position_js.init()){
        geo_position_js.getCurrentPosition(success_callback,error_callback,{enableHighAccuracy:true});
    }
    else{
        alert("Functionality not available");
    }

    function success_callback(p)
    {

        var latitudine = +p.coords.latitude.toFixed(2);
        var longitudine = p.coords.longitude.toFixed(2);

        $.getJSON(
                     'http://http://api.geonames.org/findNearbyPlaceNameJSON?lat='+latitudine+'&lng='+longitudine, 
                        function(data) {
                            alert(data);
                        }
        );

    }

    function error_callback(p)
    {
        alert('error='+p.message);
    }                       

</script>
4

2 に答える 2

2

必要なのは「逆ジオコーダー」です。

逆ジオコーダーを使用すると、特定の座標の都市名 (通りの名前も) を取得できます。

非常に簡単な方法は、Google Maps API V2 および V3 で利用可能な Google の Geocoder を使用することです。

  var geocoder;

  function codeLatLng(latitude, longitude) {
    var
        geocoder = new google.maps.Geocoder(),
        latlng = new google.maps.LatLng(latitude, longitude);

    geocoder.geocode({'latLng': latlng}, function(results, status) {
      if (status == google.maps.GeocoderStatus.OK) {
        if (results[1]) {
          // results[1].formatted_address contains the city name
        }
      } else {
        alert("Geocoder failed due to: " + status);
      }
    });
  }

例 (ソース): https://developers.google.com/maps/documentation/javascript/geocoding#ReverseGeocoding

お役に立てれば!

于 2012-06-02T09:17:50.050 に答える
0

Geonames.orgは、特に緯度/経度座標の都市を検索できるWeb サービスを運営しています。

http://www.geonames.org/export/web-services.html#findNearbyPlaceName

xml と json の両方をサポートします。私はそれらをお勧めします。追加の JavaScript ダウンロードは必要ありません。

于 2012-06-03T13:14:08.617 に答える