您的位置:首页 > 编程语言 > Go语言

Google Map API入门应用

2008-04-15 09:10 344 查看

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">


<head>


<meta http-equiv="content-type" content="text/html; charset=utf-8"/>


<title>GeoIP 搜索者</title>


<!--导入Google Maps API库文件。注意将本代码中的API Key替换为前文申请到的API Key-->


<script src="http://maps.google.com/maps?file=api&v=2&key=ABQIAAAA1j86tnUDFv8OAtC8dZVtKRT2yXp_ZAY8_ufC3CFXhHIE1NvwkxSzmwrQ90SNUILzGRpsBiaa860gfQ" type="text/javascript"></script>




<script type="text/javascript">...


//<![CDATA[


var map; //全局GMap2对象


var marker; //用于标识查询IP的GMarker地标


//初始化


function load()




...{


if (GBrowserIsCompatible())




...{


map = new GMap2(document.getElementById("map"));


map.setCenter(new GLatLng(39.92, 116.46), 2);


//添加相应GControl()控件


map.addControl(new GSmallMapControl());


map.addControl(new GMapTypeControl());


//设定地图类型为混合地图


map.setMapType(G_HYBRID_MAP);


//查询当前访客信息


getGeoInfo("");


}


}




//响应查询栏的回车


//因为FireFox中event不是全局的,所以必须从相应DOM对象里传回event


//如下写法可兼容IE和Firefox


function pressEnter(event, q)




...{


//如果输入了回车,则执行查询


if(event.keyCode==13 || event.keyCode==10)




...{


getGeoInfo(q);


}


}


//查询函数


function getGeoInfo(q)




...{


GDownloadUrl("search.php?q="+q, function(data)




...{


eval(data);


});


}




//服务器端数据调用接口


function loadGeoInfo(q, ip, country_code, country_code3, country, region, city, latitude, longitude)




...{


//新的信息窗口中的内容


var info = "<div align="left" style="overflow:X; font-size:12px">"


+ "<span style="font-size:14px"><strong>" + q + "</strong></span><br />"


+ "<strong>IP:</strong>  " + ip + "<br />"


+ "<strong>国家:</strong>  " + country + "<br />"


+ "<strong>代码:</strong>  " + country_code + "(" + country_code3 + ")" + "<br />"


+ "<strong>省份:</strong>  " + city + "<br />"


+ "<strong>城市:</strong>  " + region + "<br />"


+ "<strong>经度:</strong>  " + longitude + "<br />"


+ "<strong>纬度:</strong>  " + latitude + "<br />"


+ "</div>";


//移动地图中心到新的位置


var point = new GLatLng(latitude, longitude);


map.panTo(point);


//如果创建了marker地标,则关闭当前的信息窗口并移除地标


if(marker)




...{


map.closeInfoWindow();


map.removeOverlay(marker);


}


//创建新的地标


marker = new GMarker(point);


map.addOverlay(marker);


//显示信息窗口


marker.openInfoWindowHtml(info);


}


//]]>


</script>




<style>...




td{...}{


text-align:center;


}


</style>


</head>


<body onload="load()" onunload="GUnload()">


<table cellSpacing="0" cellPadding="0" width="600" border="0" align="center">


<tbody>


<tr>


<td>


<img src="geoipseeker.jpg" title="geoipseeker" alt="geoipseeker" width="450" height="50" style="boder:0" />


<td>


</tr>


<tr>


<td height="25">


<!--此处onsubmit为"return false;"可防止表单提交,因为本例中用AJAX查询,无需提交表单-->


<form onsubmit="return false;">


<!--分别为输入框和按钮都添加了事件监听。回车和点击按钮都可以进行查询-->


<label for="q">在此输入IP或域名<input maxLength="50" size="25" name="q" id="q" onkeypress="pressEnter(event, this.value); " />


<input type="button" value="查找" id="search" onclick="getGeoInfo(q.value)" /></label>


</form>


</td>


</tr>


<tr>


<td height="20" id="info"></td>


</tr>


<tr>


<td>


<div id="map" style="width:580px;height:350px"></div>


</td>


</tr>


<tr>


<td height="20" id="link">


<a href="http://blog.gmap2.net">Power by <strong>GMap2.net</strong></a>


</td>


</tr>


</tbody>


</table>


</body>


</html>




<?php


//导入库文件


include("geoipcity.inc");


include("geoipregionvars.php");






//获取客户端IP的函数


function getClientIP()


{


if($HTTP_SERVER_VARS["HTTP_X_FORWARDED_FOR"])


{


$ip = $HTTP_SERVER_VARS["HTTP_X_FORWARDED_FOR"];


}


elseif($HTTP_SERVER_VARS["HTTP_CLIENT_IP"])


{


$ip = $HTTP_SERVER_VARS["HTTP_CLIENT_IP"];


}


elseif ($HTTP_SERVER_VARS["REMOTE_ADDR"])


{


$ip = $HTTP_SERVER_VARS["REMOTE_ADDR"];


}


elseif (getenv("HTTP_X_FORWARDED_FOR"))


{


$ip = getenv("HTTP_X_FORWARDED_FOR");


}


elseif (getenv("HTTP_CLIENT_IP"))


{


$ip = getenv("HTTP_CLIENT_IP");


}


elseif (getenv("REMOTE_ADDR"))


{


$ip = getenv("REMOTE_ADDR");


}


else


{


$ip = false;


}


return $ip;


}




//如果传递的查询参数为空


if(empty($_GET['q']))


{


$ip = getClientIP();


if(!$ip)


{


echo 'alert("Cannot get your IP address!");';


die();


}


}




//如果传递的查询参数不为空


else


{ //探测字符串是否为IP的正则表达式


$pattern = "/^(d{1,2}|1dd|2[0-4]d|25[0-5]).(d{1,2}|1dd|2[0-4]d|25[0-5]).(d{1,2}|1dd|2[0-4]d|25[0-5]).(d{1,2}|1dd|2[0-4]d|25[0-5])$/";




if(preg_match($pattern, $_GET['q'])) //如果查询的信息为IP


{


$ip = $_GET['q'];


}


else //如果查询的信息为域名


{


$ip = gethostbyname($_GET['q']); //若域名查询成功,则返回的字符串为IP


if(!preg_match($pattern, $ip)) //检测是否返回IP,若未返回IP,即域名查询失败,报错


{


echo 'alert("Invalid Input!"); ';


die();


}


}


}




//如果取消下一行将使用共享内存打开GeoLiteCity数据库,可加快查询。但前提是服务器支持共享内存


//$gi = geoip_open("./GeoLiteCity.dat",GEOIP_MEMORY_CACHE);




//以常规方式打开GeoLiteCity数据库,一般情况下都可以使用


$gi = geoip_open("./GeoLiteCity.dat",GEOIP_STANDARD);




//以上是假定GeoLiteCity数据库和PHP文件在同一个目录,故使用"./GeoLiteCity.dat"为路径


//如果不是,请改为实际目录




//获取IP的信息


$record = geoip_record_by_addr($gi, $ip);




//关闭数据库


geoip_close($gi);




//如果获取了相关数据


if($record)


{


echo 'loadGeoInfo('.


'"'. $_GET['q'] .'",'. //原始查询信息


'"'. $ip .'",'. //被查IP


'"'. $record->country_code .'",'. //国家代码(两位)


'"'. $record->country_code3 .'",'. //国家代码(三位)


'"'. $record->country_name .'",'. //国家名称


'"'. $GEOIP_REGION_NAME[$record->country_code][$record->region] .'",'. //地区名称


'"'. $record->city .'",'. //城市名称


$record->latitude .','. //纬度


$record->longitude .')'; //经度


}


//如果数据库中不存在相关数据


else


{


echo 'alert("The Information for '.$ip.' is not available now!"); ';


}




?>

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: