Position: All -> Works -> 简单的学习了一下Google Map API,实现IP在地图上的定位 | 08-07-10 14:54:22
Font-Size:
Large | Middle | Small

下午看了看Google提供的API,发现Google现在提供的API是越来越多了,什么方面的都有,数据免费让你用,测绘地图的卫星也免费让你用……

没事做就学用了一下Google Map API,给的函数不是特别多,但实际应用中数据的类型和源才是重点,用了几个基本的函数做了这个小东西,通过在输入框中输入一个IP或者域名即可在地图上定位显示该IP或域名对应的主机坐在的位置,同时再给出一个国家、城市、经纬度等信息……

在此用到了GEOIP免费IP数据库,可以去http://www.maxmind.com/download/geoip下载

第一次尝试Web API,以后可能还会尝试其他的API,也会对Google Map API做更深一步的学习,用别人的东西感觉挺好的……

点此或图片进入测试页面

PHP代码
  1. <?php   
  2. header('Content-type: text/html;charset=GB2312');   
  3.   
  4. include("geoipcity.inc");   
  5. include("geoipregionvars.php");   
  6.   
  7. function getip(){    
  8.     if (getenv("HTTP_CLIENT_IP") && strcasecmp(getenv("HTTP_CLIENT_IP"), "unknown"))    
  9.         $ip = getenv("HTTP_CLIENT_IP");    
  10.     else if (getenv("HTTP_X_FORWARDED_FOR") && strcasecmp(getenv("HTTP_X_FORWARDED_FOR"), "unknown"))    
  11.         $ip = getenv("HTTP_X_FORWARDED_FOR");    
  12.     else if (getenv("REMOTE_ADDR") && strcasecmp(getenv("REMOTE_ADDR"), "unknown"))    
  13.         $ip = getenv("REMOTE_ADDR");    
  14.     else if (isset($_SERVER["REMOTE_ADDR"]) && $_SERVER["REMOTE_ADDR"] && strcasecmp($_SERVER["REMOTE_ADDR"], "unknown"))    
  15.         $ip = $_SERVER["REMOTE_ADDR"];    
  16.     else    
  17.         $ip = "unknown";    
  18.     return $ip;    
  19. }    
  20.   
  21. if(emptyempty($_GET['q'])) {   
  22.     $ip = getip();   
  23.     if($ip == "unknown") {   
  24.         echo 'alert("无法取得客户端IP");';   
  25.         die();   
  26.     }   
  27. else {   
  28.     $pattern = "/^((1?\d?\d|(2([0-4]\d|5[0-5])))\.){3}(1?\d?\d|(2([0-4]\d|5[0-5])))$/";   
  29.     if(preg_match($pattern,$_GET['q'])) {   
  30.         $ip = $_GET['q'];   
  31.     } else {   
  32.         $ip = gethostbyname($_GET['q']);   
  33.         if(preg_match($pattern,$_GET['q'])) {   
  34.             echo 'alert("查询地址有误");';   
  35.             die();   
  36.         }    
  37.                
  38.     }   
  39. }   
  40.   
  41. $gi = geoip_open("./GeoLiteCity.dat",GEOIP_STANDARD);   
  42. $record = GeoIP_record_by_addr($gi,$ip);   
  43. geoip_close($gi);   
  44.   
  45. if($record) {   
  46.     echo 'loadInfo('.   
  47.             '"'.$_GET['q'].'",'.   
  48.             '"'.$ip.'",'.   
  49.             '"'.$record->country_code.'",'.   
  50.             '"'.$record->country_code3.'",'.   
  51.             '"'.$record->country_name.'",'.   
  52.             '"'.$GEOIP_REGION_NAME[$record->country_code][$record->region].'",'.   
  53.             '"'.$record->city.'",'.   
  54.             $record->latitude.','.$record->longitude.')';   
  55. else {   
  56.     echo 'alert("数据库里不存在该IP的信息");';   
  57. }   
  58. ?>  

   

XML/HTML代码
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  2. <html xmlns="http://www.w3.org/1999/xhtml">  
  3. <head>  
  4. <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />  
  5.     <title>Google Maps JavaScript API Example</title>  
  6.     <script src="js/jquery/jquery.pack.js"  
  7.       type="text/javascript"></script>  
  8.     <script charset="utf-8" src="http://maps.google.com/maps?file=api&v=2&key=ABQIAAAAHNhEpt8X-NNdY903GRqUZxSXPx9oqQLJ7Iscw6Eb65jR2kkxxxSwxSxR-C6LwGyWiSUfXSiLCnV8jw"  
  9.       type="text/javascript"></script>  
  10.     <script type="text/javascript">  
  11.     var map,marker;   
  12.     $(document).ready(function () {   
  13.       if (GBrowserIsCompatible()) {   
  14.         map = new GMap2(document.getElementById("map"));   
  15.         map.setCenter(new GLatLng(39.92,116.46), 18);   
  16.         map.addControl(new GSmallMapControl());   
  17.         map.addControl(new GMapTypeControl());   
  18.         map.setMapType(G_HYBRID_MAP);   
  19.         getInfo("");   
  20.       }   
  21.     });   
  22.        
  23.     function parseEnter(event,q) {   
  24.         if(event.keyCode == 13 || event.keyCode == 10)   
  25.             getInfo(q);   
  26.     }   
  27.        
  28.     function getInfo(q) {   
  29.         //GDownloadUrl("search.php?q="+q, function (data) {eval(data);});   
  30.         $("#loading").ajaxStart(function(){$(this).show();}).ajaxComplete(function(){$(this).hide();});   
  31.         $.ajax({   
  32.                 type: "get",   
  33.                 url: "search.php?q="+q,   
  34.                 beforeSend: function() {   
  35.                 },   
  36.                 success: function(data) {   
  37.                     eval(data);   
  38.                 }   
  39.         });   
  40.     }   
  41.        
  42.     function loadInfo(q,ip,country_code,country_code3,country,region,city,latitude,longtitude) {   
  43.         var info = "<div align=left style=overflow:x;font-size:12px>"  
  44.             + "<span style=font-size:14px;><b><a target=_blank href=http://" + q + ">" + q + "</a></b></span><br>"   
  45.             + "<b>IP:</b> " + ip + "<br>"   
  46.             + "<b>国家:</b> " + country + "<br>"   
  47.             + "<b>代码:</b> " + country_code + "<br>"   
  48.             + "<b>省份:</b> " + city + "<br>"   
  49.             + "<b>城市:</b> " + region + "<br>"   
  50.             + "<b>经度:</b> " + longtitude + "<br>"   
  51.             + "<b>纬度:</b> " + latitude + "<br>"   
  52.             + "<span align=right><b><a target=_blank href=http://lizhijin.com>LiZhiJin.com</a></b></span><br>";   
  53.         var point = new GLatLng(latitude,longtitude);   
  54.         map.panTo(point);   
  55.         if(marker) {   
  56.             map.closeInfoWindow();   
  57.             map.removeOverlay(marker);   
  58.         }   
  59.         marker = new GMarker(point);   
  60.         map.addOverlay(marker);   
  61.         marker.openInfoWindowHtml(info);   
  62.     }   
  63.     </script>  
  64.   </head>  
  65.   <body onunload="GUnload()" style="text-align:center">  
  66.     <h3>域名或IP在Google Map上的定位(首次加载显示您的位置)</h3>  
  67.     <div id="result">  
  68.     <form onsubmit="return false;">  
  69.     输入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>  
  70.     </form>  
  71.     </div>  
  72.     <div id="map" style="width: 500px; height: 300px"></div>  
  73.   </body>  
  74. </html>  
Attachment: googlemapapiexample.rar
File Info:RAR file, 57.2 k bytes
Download Times:383

Comments:

祛痘产品排行榜10强Jul 28th, 2010 at 1:34 am
#18
学习中。。。
labatterieJul 19th, 2010 at 14:24 pm
#17
It has been truly a great read of the article.
batterieJul 19th, 2010 at 14:23 pm
#16
please your girlfriend with large member.
Rolex watchesJul 19th, 2010 at 14:23 pm
#15
感觉有点难。
JeminJun 2nd, 2010 at 14:33 pm
#14
很不错啊
ds cartesMar 24th, 2010 at 13:28 pm
#13
As for the colors, navy blue, matte white and grays are the ones to go for.
瑜伽视频Jul 15th, 2009 at 1:38 am
#12
好的,我先下载研究下,有问题再来留言资讯你哦~
AJanJul 2nd, 2009 at 8:06 am
#11
很不错,支持,学习
SusanSaysJun 6th, 2009 at 5:21 am
#10
没法实现,因为js不能查数据库
yiqiuJun 5th, 2009 at 4:02 am
#9
想问下不使用php直接用javascript代码可以实现吗?怎么实现
SusanSaysApr 9th, 2009 at 13:34 pm
#8
不好意思我白天在忙

eval函数应该是没错的,具体怎么错我也不是很清楚,你可以用firefox下得firebug插件调试一下代码
mxApr 9th, 2009 at 6:23 am
#7
楼主赶紧回答一下我吧,我运行出错时怎么回事,我不知道是什么问题,我都要抓狂了
mxApr 9th, 2009 at 1:31 am
#6
为什么我下载到自己电脑上运行出错??eval函数出错??是不是要配置什么环境??
etigerFeb 19th, 2009 at 3:29 am
#5
谢谢楼主分享
huoJul 30th, 2008 at 2:23 am
#4
猛回头,大姐,你不是吧。。。遮望眼,只怪我太SB。。。。。
xxJul 17th, 2008 at 6:19 am
#3
虚伪...
SusanSaysJul 12th, 2008 at 13:03 pm
#2
你貌似已经沙发过好几次了嘛……就只是调了几个Google给的函数啊,关键性的JS代码就20来行,应该算是最简单的应用了,没什么刁的……
huoJul 12th, 2008 at 10:44 am
#1
终于沙发一次。一句话:屌得一塌屎啊

Add Comments: