[要jQuery] ユーザーのIPアドレスから場所情報(Geo Info.)を取得し表示する

説明

ユーザーのIPアドレスから場所情報(Geo Info.)を取得し表示するには以下のWebサイトのサービスを利用します。

http://www.nitinh.com/2009/04/ip-address-geolocation-javascript-api-json/

このサービスはIPアドレスを元にJSONまたはJSONPでの場所情報を返します。jQueryを使えば$.getScript()により手軽にJSONPを使って情報を取得できます。あとは、上記サイトで公開されている情報を元にしてページ上に表示します。

サンプルプログラム

window.onload = function(){
$("#geoButton").click(function(){
var yourIP = $("#ipAdrs").val();
$.getScript("http://www.nitinh.com/ip_query.php?ip="+yourIP+"&callback=displayGeo");
})
}
function displayGeo(data){
var txt = "";
txt += "<br>status = "+data["status"];
txt += "<br>IP = "+data["IP"];
txt += "<br>CountryCode = "+data["CountryCode"];
txt += "<br>CountryName = "+data["CountryName"];
txt += "<br>RegionName = "+data["RegionName"];
txt += "<br>ZipPostalCode = "+data["ZipPostalCode"];
txt += "<br>City = "+data["City"];
txt += "<br>Latitude = "+data["Latitude"];
txt += "<br>Longitude = "+data["Longitude"];
$("#result").html(txt);
}
サンプルを実行
[戻る]