google api 获得客户端ip的地理位置

通过Google API 获得客户端ip地址的地理位置

首先要引用google提供的JavaScript接口文件

<script type=”text/javascript” src=”http://j.maxmind.com/app/geoip.js”></script>

下面是次接口的用法:

eg.

<html xmlns=”http://www.w3.org/1999/xhtml” >
<head runat=”server”>
<title>无标题页</title>
<script type=”text/javascript” src=”http://j.maxmind.com/app/geoip.js”></script>
</head>
<body>
<form id=”form1″ runat=”server”>
<div>
<script type=”text/javascript”>
var area = “”;
var country=””;
var pro=””;
var city=””;
try
{
if(geoip_city() != “” && geoip_city() != null)
area += geoip_city()+” “;
city=geoip_city(); //城市名称
if(geoip_region_name() != “” && geoip_region_name() != null)
area += geoip_region_name()+” “;
pro=geoip_region_name();//地区名称
if(geoip_country_name() != “” && geoip_country_name() != null)
area += geoip_country_name();
country=geoip_country_name();//国家名称
}
catch(err)
{
area=””;
}
if(area!=””)
{
if(pro!=city)//判断是否是直辖市,如上海
{
document.write(“Welocome you, come from the friend of “+ area);
}
else
{
document.write(“Welocome you, come from the friend of “+ city +” “+ country);
}
}
</script>
</div>
</form>
</body>
</html>
其中:还有另外3个方法可以调用,分别是:geoip_country_code():表示国家代码
geoip_latitude():表示经度 ? ? ? geoip_longitude()表示纬度

本文原创,转载请注明来源:http://happysnail.org/index.php/archives/371

未经允许不得转载:WEIXING.ME » google api 获得客户端ip的地理位置

相关文章

评论 (0)