下午看了看Google提供的API,发现Google现在提供的API是越来越多了,什么方面的都有,数据免费让你用,测绘地图的卫星也免费让你用……
没事做就学用了一下Google Map API,给的函数不是特别多,但实际应用中数据的类型和源才是重点,用了几个基本的函数做了这个小东西,通过在输入框中输入一个IP或者域名即可在地图上定位显示该IP或域名对应的主机坐在的位置,同时再给出一个国家、城市、经纬度等信息……
在此用到了GEOIP免费IP数据库,可以去http://www.maxmind.com/download/geoip下载
第一次尝试Web API,以后可能还会尝试其他的API,也会对Google Map API做更深一步的学习,用别人的东西感觉挺好的……
PHP代码
- <?php
- header('Content-type: text/html;charset=GB2312');
- include("geoipcity.inc");
- include("geoipregionvars.php");
- function getip(){
- if (getenv("HTTP_CLIENT_IP") && strcasecmp(getenv("HTTP_CLIENT_IP"), "unknown"))
- $ip = getenv("HTTP_CLIENT_IP");
- else if (getenv("HTTP_X_FORWARDED_FOR") && strcasecmp(getenv("HTTP_X_FORWARDED_FOR"), "unknown"))
- $ip = getenv("HTTP_X_FORWARDED_FOR");
- else if (getenv("REMOTE_ADDR") && strcasecmp(getenv("REMOTE_ADDR"), "unknown"))
- $ip = getenv("REMOTE_ADDR");
- else if (isset($_SERVER["REMOTE_ADDR"]) && $_SERVER["REMOTE_ADDR"] && strcasecmp($_SERVER["REMOTE_ADDR"], "unknown"))
- $ip = $_SERVER["REMOTE_ADDR"];
- else
- $ip = "unknown";
- return $ip;
- }
- if(emptyempty($_GET['q'])) {
- $ip = getip();
- if($ip == "unknown") {
- echo 'alert("无法取得客户端IP");';
- die();
- }
- } else {
- $pattern = "/^((1?\d?\d|(2([0-4]\d|5[0-5])))\.){3}(1?\d?\d|(2([0-4]\d|5[0-5])))$/";
- if(preg_match($pattern,$_GET['q'])) {
- $ip = $_GET['q'];
- } else {
- $ip = gethostbyname($_GET['q']);
- if(preg_match($pattern,$_GET['q'])) {
- echo 'alert("查询地址有误");';
- die();
- }
- }
- }
- $gi = geoip_open("./GeoLiteCity.dat",GEOIP_STANDARD);
- $record = GeoIP_record_by_addr($gi,$ip);
- geoip_close($gi);
- if($record) {
- echo 'loadInfo('.
- '"'.$_GET['q'].'",'.
- '"'.$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("数据库里不存在该IP的信息");';
- }
- ?>
XML/HTML代码
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
- <title>Google Maps JavaScript API Example</title>
- <script src="js/jquery/jquery.pack.js"
- type="text/javascript"></script>
- <script charset="utf-8" src="http://maps.google.com/maps?file=api&v=2&key=ABQIAAAAHNhEpt8X-NNdY903GRqUZxSXPx9oqQLJ7Iscw6Eb65jR2kkxxxSwxSxR-C6LwGyWiSUfXSiLCnV8jw"
- type="text/javascript"></script>
- <script type="text/javascript">
- var map,marker;
- $(document).ready(function () {
- if (GBrowserIsCompatible()) {
- map = new GMap2(document.getElementById("map"));
- map.setCenter(new GLatLng(39.92,116.46), 18);
- map.addControl(new GSmallMapControl());
- map.addControl(new GMapTypeControl());
- map.setMapType(G_HYBRID_MAP);
- getInfo("");
- }
- });
- function parseEnter(event,q) {
- if(event.keyCode == 13 || event.keyCode == 10)
- getInfo(q);
- }
- function getInfo(q) {
- //GDownloadUrl("search.php?q="+q, function (data) {eval(data);});
- $("#loading").ajaxStart(function(){$(this).show();}).ajaxComplete(function(){$(this).hide();});
- $.ajax({
- type: "get",
- url: "search.php?q="+q,
- beforeSend: function() {
- },
- success: function(data) {
- eval(data);
- }
- });
- }
- function loadInfo(q,ip,country_code,country_code3,country,region,city,latitude,longtitude) {
- var info = "<div align=left style=overflow:x;font-size:12px>"
- + "<span style=font-size:14px;><b><a target=_blank href=http://" + q + ">" + q + "</a></b></span><br>"
- + "<b>IP:</b> " + ip + "<br>"
- + "<b>国家:</b> " + country + "<br>"
- + "<b>代码:</b> " + country_code + "<br>"
- + "<b>省份:</b> " + city + "<br>"
- + "<b>城市:</b> " + region + "<br>"
- + "<b>经度:</b> " + longtitude + "<br>"
- + "<b>纬度:</b> " + latitude + "<br>"
- + "<span align=right><b><a target=_blank href=http://lizhijin.com>LiZhiJin.com</a></b></span><br>";
- var point = new GLatLng(latitude,longtitude);
- map.panTo(point);
- if(marker) {
- map.closeInfoWindow();
- map.removeOverlay(marker);
- }
- marker = new GMarker(point);
- map.addOverlay(marker);
- marker.openInfoWindowHtml(info);
- }
- </script>
- </head>
- <body onunload="GUnload()" style="text-align:center">
- <h3>域名或IP在Google Map上的定位(首次加载显示您的位置)</h3>
- <div id="result">
- <form onsubmit="return false;">
- 输入IP或域名(不需要http://):<input type="text" maxlength="100" size="25" name="q" id="q" onkeypress="parseEnter(event,this.value)" /> <input type="button" value="查询" onclick="getInfo(q.value)" /> <span id="loading" style="display:none">正在查询...</span>
- </form>
- </div>
- <div id="map" style="width: 500px; height: 300px"></div>
- </body>
- </html>


厚积薄发,有的放矢――李开复博士给中国计算机系学生的建议
Large
Middle
Small
Attachment:
views(4339)
view comments(18)
add comment


Comments:
eval函数应该是没错的,具体怎么错我也不是很清楚,你可以用firefox下得firebug插件调试一下代码