Google Map API 結(jié)合PHP實(shí)現(xiàn)登錄map定位
Jun 13, 2016 am 10:56 AM
Google Map API 結(jié)合PHP實(shí)現(xiàn)登錄地圖定位
Google Map API 結(jié)合PHP實(shí)現(xiàn)登錄定位主要思想是在用戶登錄系統(tǒng)時(shí)記錄下IP地址,在通過相關(guān)的地址索引由IP轉(zhuǎn)換到物理地址,最后通過Google Map API將取得的物理地址標(biāo)示在Google地圖上。
?
php平臺(tái)由ThinkPHP框架搭建。具體流程圖如下:
?
這里用到了CoralWry這個(gè)數(shù)據(jù)文件,網(wǎng)上有的下載的,以前的彩虹QQ什么的都是用這個(gè)文件解析的,不過可能有時(shí)候需要更新IP的緣故會(huì)對(duì)此文件進(jìn)行更新,下面有下載包。這個(gè)文件放在根目錄下
?
繼續(xù),在TP中引入一下兩個(gè)文件作為外鏈調(diào)用庫,可以直接放入TP核心文件夾的Lib下的ORG擴(kuò)展庫中
IpLocation類,這個(gè)擴(kuò)展類是從DAT文件中將IP提取地址所用
?
class IpLocation {var $fp;var $firstip; //第一條ip索引的偏移地址var $lastip; //最后一條ip索引的偏移地址var $totalip; //總ip數(shù)//*//構(gòu)造函數(shù),初始化一些變量//$datfile 的值為純真IP數(shù)據(jù)庫的名子,可自行修改.//*function ipLocation($datfile = "CoralWry.Dat"){ $this->fp=fopen($datfile,'rb')or die("CoralWry.Dat不存在,請(qǐng)去網(wǎng)上下載純真IP數(shù)據(jù)庫, 'CoralWry.dat' 放到當(dāng)前目錄下"); //二制方式打開 $this->firstip = $this->get4b(); //第一條ip索引的絕對(duì)偏移地址 $this->lastip = $this->get4b(); //最后一條ip索引的絕對(duì)偏移地址 $this->totalip =($this->lastip - $this->firstip)/7 ; //ip總數(shù) 索引區(qū)是定長的7個(gè)字節(jié),在此要除以7, register_shutdown_function(array($this,"closefp")); //為了兼容php5以下版本,本類沒有用析構(gòu)函數(shù),自動(dòng)關(guān)閉ip庫.}//*//關(guān)閉ip庫//*function closefp(){fclose($this->fp);}//*//讀取4個(gè)字節(jié)并將解壓成long的長模式//*function get4b(){ $str=unpack("V",fread($this->fp,4)); return $str[1];}//*//讀取重定向了的偏移地址//*function getoffset(){ $str=unpack("V",fread($this->fp,3).chr(0)); return $str[1];}//*//讀取ip的詳細(xì)地址信息//*function getstr(){ $str=""; $split=fread($this->fp,1); while (ord($split)!=0) { $str .=$split; $split=fread($this->fp,1); } return $str;}//*//將ip通過ip2long轉(zhuǎn)成ipv4的互聯(lián)網(wǎng)地址,再將他壓縮成big-endian字節(jié)序//用來和索引區(qū)內(nèi)的ip地址做比較//*function iptoint($ip){ return pack("N",intval(ip2long($ip)));}//*//獲取客戶端ip地址//注意:如果你想要把ip記錄到服務(wù)器上,請(qǐng)?jiān)趯憥鞎r(shí)先檢查一下ip的數(shù)據(jù)是否安全.//*function getIP() { if (getenv('HTTP_CLIENT_IP')) { $ip = getenv('HTTP_CLIENT_IP'); } elseif (getenv('HTTP_X_FORWARDED_FOR')) { //獲取客戶端用代理服務(wù)器訪問時(shí)的真實(shí)ip 地址 $ip = getenv('HTTP_X_FORWARDED_FOR'); } elseif (getenv('HTTP_X_FORWARDED')) { $ip = getenv('HTTP_X_FORWARDED'); } elseif (getenv('HTTP_FORWARDED_FOR')) { $ip = getenv('HTTP_FORWARDED_FOR'); } elseif (getenv('HTTP_FORWARDED')) { $ip = getenv('HTTP_FORWARDED'); } else { $ip = $_SERVER['REMOTE_ADDR']; } return $ip;}//*//獲取地址信息//*function readaddress(){ $now_offset=ftell($this->fp); //得到當(dāng)前的指針位址 $flag=$this->getflag(); switch (ord($flag)){ case 0: $address=""; break; case 1: case 2: fseek($this->fp,$this->getoffset()); $address=$this->getstr(); break; default: fseek($this->fp,$now_offset); $address=$this->getstr(); break; } return $address;}//*//獲取標(biāo)志1或2//用來確定地址是否重定向了.//*function getflag(){ return fread($this->fp,1);}//*//用二分查找法在索引區(qū)內(nèi)搜索ip//*function searchip($ip){ $ip=gethostbyname($ip); //將域名轉(zhuǎn)成ip $ip_offset["ip"]=$ip; $ip=$this->iptoint($ip); //將ip轉(zhuǎn)換成長整型 $firstip=0; //搜索的上邊界 $lastip=$this->totalip; //搜索的下邊界 $ipoffset=$this->lastip; //初始化為最后一條ip地址的偏移地址 while ($firstip fp,$this->firstip + $i * 7); //定位指針到中間記錄 $startip=strrev(fread($this->fp,4)); //讀取當(dāng)前索引區(qū)內(nèi)的開始ip地址,并將其little-endian的字節(jié)序轉(zhuǎn)換成big-endian的字節(jié)序 if ($ip fp,$this->getoffset()); $endip=strrev(fread($this->fp,4)); if ($ip > $endip){ $firstip=$i + 1; } else { $ip_offset["offset"]=$this->firstip + $i * 7; break; } } } return $ip_offset;}//*//獲取ip地址詳細(xì)信息//*function getaddress($ip){ $ip_offset=$this->searchip($ip); //獲取ip 在索引區(qū)內(nèi)的絕對(duì)編移地址 $ipoffset=$ip_offset["offset"]; $address["ip"]=$ip_offset["ip"]; fseek($this->fp,$ipoffset); //定位到索引區(qū) $address["startip"]=long2ip($this->get4b()); //索引區(qū)內(nèi)的開始ip 地址 $address_offset=$this->getoffset(); //獲取索引區(qū)內(nèi)ip在ip記錄區(qū)內(nèi)的偏移地址 fseek($this->fp,$address_offset); //定位到記錄區(qū)內(nèi) $address["endip"]=long2ip($this->get4b()); //記錄區(qū)內(nèi)的結(jié)束ip 地址 $flag=$this->getflag(); //讀取標(biāo)志字節(jié) switch (ord($flag)) { case 1: //地區(qū)1地區(qū)2都重定向 $address_offset=$this->getoffset(); //讀取重定向地址 fseek($this->fp,$address_offset); //定位指針到重定向的地址 $flag=$this->getflag(); //讀取標(biāo)志字節(jié) switch (ord($flag)) { case 2: //地區(qū)1又一次重定向, fseek($this->fp,$this->getoffset()); $address["area1"]=$this->getstr(); fseek($this->fp,$address_offset+4); //跳4個(gè)字節(jié) $address["area2"]=$this->readaddress(); //地區(qū)2有可能重定向,有可能沒有 break; default: //地區(qū)1,地區(qū)2都沒有重定向 fseek($this->fp,$address_offset); //定位指針到重定向的地址 $address["area1"]=$this->getstr(); $address["area2"]=$this->readaddress(); break; } break; case 2: //地區(qū)1重定向 地區(qū)2沒有重定向 $address1_offset=$this->getoffset(); //讀取重定向地址 fseek($this->fp,$address1_offset); $address["area1"]=$this->getstr(); fseek($this->fp,$address_offset+8); $address["area2"]=$this->readaddress(); break; default: //地區(qū)1地區(qū)2都沒有重定向 fseek($this->fp,$address_offset+4); $address["area1"]=$this->getstr(); $address["area2"]=$this->readaddress(); break; } //*過濾一些無用數(shù)據(jù) if (strpos($address["area1"],"CZ88.NET")!=false){ $address["area1"]="未知"; } if (strpos($address["area2"],"CZ88.NET")!=false){ $address["area2"]=" "; } return $address; }}
?
?
mapService類,方法類
?
?
/** * Google Map Service * 2011.4.8 */import("ORG.IPA.IpLocation");class mapService{ public static function getIPaddress($ip){ //返回格式 $format = "text";//默認(rèn)text,json,xml,js //返回編碼 $charset = "utf8"; //默認(rèn)utf-8,gbk或gb2312 $ip_l=new IpLocation(); $address=$ip_l->getaddress($ip); $address["area1"] = iconv('GB2312','utf-8',$address["area1"]); $address["area2"] = iconv('GB2312','utf-8',$address["area2"]); $add=$address["area1"].$address["area2"]; if($add=="本機(jī)地址 "){ $add="杭州"; } return $add; }}?
然后就可以在登陸的Action接口函數(shù)中調(diào)用兩個(gè)文件類了:
?
?
import("ORG.IPA.MapService"); $ipaddress = get_client_ip(); $adrInfo=MapService::getIPaddress($ipaddress);?
?
接下去就可以將上述信息記錄到數(shù)據(jù)庫中了,而在用戶進(jìn)入地圖頁面便可以將數(shù)據(jù)從數(shù)據(jù)庫導(dǎo)出,并發(fā)送到view層進(jìn)行顯示,并在前端層調(diào)用Google Map API
?
后臺(tái)控制層代碼很簡單:
?
?
public function map(){ parent::islogin(); $model = D("Topicview"); $list = $model->field('id,tid,imgid,avatar,address,create_time,topic_from,content,nickname,rootid,homepage') ->where("Topic.status=1 and Topic.type='first'") ->order("id desc") ->find(); //dump($list); $this->assign('addrList',$list); parent::showSiteInfo("Lab前端實(shí)驗(yàn)室 - Map Position"); $this->display(); }?
?
接下來是前臺(tái)模板層js代碼:
?
?
if (typeof flowg == "undefined" || !flowg) { var flowg = {};}flowg.initMap = (function(){ var htmlString = '<div style="overflow: auto;">' + '<div style="width:300px;overflow:hidden;" class="map-item">' + '<div class="map-left">' + '<img src="/static/imghw/default1.png" data-src="{:getUserAvatar($addrList[" class="lazy" avatar alt="{$addrList.nickname}" style="max-width:90%">' + '</div> <div class="map-right">' + '<div class="map-content">{$addrList.nickname} : {$addrList.content}</div>' + '<div class="time">他在{$addrList.address}</div>' + '</div>' + '</div>'; var geocoder; var map; var oldinfo = null; function initialize(){ geocoder = new google.maps.Geocoder(); var latlng = new google.maps.LatLng(34.016, 103.535); var myOptions = { zoom: 8, center: latlng, mapTypeId: google.maps.MapTypeId.ROADMAP }; map = new google.maps.Map(document.getElementById("map_canvas"), myOptions); codeAddress("{$addrList.address}", htmlString); } function codeAddress(address, html){ geocoder.geocode({ 'address': address }, function(results, status){ if (status == google.maps.GeocoderStatus.OK) { map.setCenter(results[0].geometry.location); var marker = new google.maps.Marker({ map: map, position: results[0].geometry.location }); var contentString = html; var infowindow = new google.maps.InfoWindow({ content: contentString }); infowindow.open(map, marker); if (oldinfo != null) { oldinfo.close(); } oldinfo = infowindow; } else { return false; } }); } return initialize; })();$(flowg.initMap);?<p>?</p> <p>上面直接調(diào)用了google map的marker功能對(duì)于信息進(jìn)行展示,效果圖:</p> <p>?</p> <p><br><img src="/static/imghw/default1.png" data-src="/img/2012/10/19/1332391838.png" class="lazy" alt=""></p> <div class="clear"> </div> </div>

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Many users will choose the Huawei brand when choosing smart watches. Among them, Huawei GT3pro and GT4 are very popular choices. Many users are curious about the difference between Huawei GT3pro and GT4. Let’s introduce the two to you. . What are the differences between Huawei GT3pro and GT4? 1. Appearance GT4: 46mm and 41mm, the material is glass mirror + stainless steel body + high-resolution fiber back shell. GT3pro: 46.6mm and 42.9mm, the material is sapphire glass + titanium body/ceramic body + ceramic back shell 2. Healthy GT4: Using the latest Huawei Truseen5.5+ algorithm, the results will be more accurate. GT3pro: Added ECG electrocardiogram and blood vessel and safety

Function means function. It is a reusable code block with specific functions. It is one of the basic components of a program. It can accept input parameters, perform specific operations, and return results. Its purpose is to encapsulate a reusable block of code. code to improve code reusability and maintainability.

Why Snipping Tool Not Working on Windows 11 Understanding the root cause of the problem can help find the right solution. Here are the top reasons why the Snipping Tool might not be working properly: Focus Assistant is On: This prevents the Snipping Tool from opening. Corrupted application: If the snipping tool crashes on launch, it might be corrupted. Outdated graphics drivers: Incompatible drivers may interfere with the snipping tool. Interference from other applications: Other running applications may conflict with the Snipping Tool. Certificate has expired: An error during the upgrade process may cause this issu simple solution. These are suitable for most users and do not require any special technical knowledge. 1. Update Windows and Microsoft Store apps

In this article, we will learn about enumerate() function and the purpose of “enumerate()” function in Python. What is the enumerate() function? Python's enumerate() function accepts a data collection as a parameter and returns an enumeration object. Enumeration objects are returned as key-value pairs. The key is the index corresponding to each item, and the value is the items. Syntax enumerate(iterable,start) Parameters iterable - The passed in data collection can be returned as an enumeration object, called iterablestart - As the name suggests, the starting index of the enumeration object is defined by start. if we ignore

Detailed explanation of the role and function of the MySQL.proc table. MySQL is a popular relational database management system. When developers use MySQL, they often involve the creation and management of stored procedures (StoredProcedure). The MySQL.proc table is a very important system table. It stores information related to all stored procedures in the database, including the name, definition, parameters, etc. of the stored procedures. In this article, we will explain in detail the role and functionality of the MySQL.proc table

Part 1: Initial Troubleshooting Steps Checking Apple’s System Status: Before delving into complex solutions, let’s start with the basics. The problem may not lie with your device; Apple's servers may be down. Visit Apple's System Status page to see if the AppStore is working properly. If there's a problem, all you can do is wait for Apple to fix it. Check your internet connection: Make sure you have a stable internet connection as the "Unable to connect to AppStore" issue can sometimes be attributed to a poor connection. Try switching between Wi-Fi and mobile data or resetting network settings (General > Reset > Reset Network Settings > Settings). Update your iOS version:

php提交表單通過后,彈出的對(duì)話框怎樣在當(dāng)前頁彈出php提交表單通過后,彈出的對(duì)話框怎樣在當(dāng)前頁彈出而不是在空白頁彈出?想實(shí)現(xiàn)這樣的效果:而不是空白頁彈出:------解決方案--------------------如果你的驗(yàn)證用PHP在后端,那么就用Ajax;僅供參考:HTML code

This article will help you interpret the vue source code and introduce why you can use this to access properties in various options in Vue2. I hope it will be helpful to everyone!
