通過數(shù)據(jù)庫和ajax方法寫出地圖的實(shí)例代碼
ajax教程
AJAX = Asynchronous JavaScript and XML(異步的 JavaScript 和 XML)。
AJAX 不是新的編程語言,而是一種使用現(xiàn)有標(biāo)準(zhǔn)的新方法。
AJAX 是與服務(wù)器交換數(shù)據(jù)并更新部分網(wǎng)頁的藝術(shù),在不重新加載整個(gè)頁面的情況下。
客戶端部分:html、js、css代碼部分:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title></title> <meta charset="UTF-8"/> </head> <!--css樣式部分--> <style type="text/css"> .content_map{ /*border:1px solid blue;*/ width:1349px; height:524px; float:left; margin-top:100px; } .content_map .mLeft{ border:none; border-top:1px solid #fb6c20; width:400px; margin-top:14px; float:left; margin-left:134px; } .content_map>span{ margin-left:20px; margin-right:20px; font-size:28px; font-family: "Microsoft Yahei"; /*font-weight: bold;*/ float:left; } .content_map .mRight{ float:left; border:none; border-top:1px solid #fb6c20; width:400px; margin-top:14px; } #maplist{ margin-top:50px; width:749px; height:524px; /*border:1px solid #fb6c20;*/ background: url("images/diru.png") no-repeat 0 0 ; background-size:contain; position: relative; float:left; } .mapShop img{ position:absolute; /*border:1px solid red;*/ } #map_right{ /*border:1px solid #fb6c20;*/ float:left; /*width:600px;*/ width:594px; height:524px; background-color: #f0f2fe; margin-top: 40px; } .shopMsg img{ width:450px; height:300px; margin-left:72px; margin-top:40px; } .shopMsg .pmname{ color:#000; font-size:20px; margin-top:30px; margin-left:72px; font-family:微軟雅黑; } .shopMsg .address{ color:#000; font-size:20px; margin-top:30px; margin-left:72px; font-family:微軟雅黑; } .shopMsg .phone{ color:#000; font-size:20px; margin-top:30px; margin-left:72px; font-family:微軟雅黑; } </style> <body> <!--html部分--> <div class="content_map"> <!-- 標(biāo)題--> <hr class="mLeft"/> <span>相關(guān)寵物醫(yī)院</span> <hr class="mRight"/> <!-- 左邊部分:地圖--> <div id="maplist"> </div> <!-- 右邊部分點(diǎn)擊左邊要添加的內(nèi)容:以及最開始加入的信息--> <div id="map_right"> <div class="shopMsg"> <img src="images/w_map.png"/> <div class="pmname">寵物店名:Petjoy寵物社區(qū)</div> <div class="address">地址:長寧區(qū)機(jī)旋路1258號(hào)--1260號(hào)</div> <div class="phone">電話號(hào)碼:(021)53018000</div> </div> </div> </div> <!--js代碼部分--> <script type="text/javascript"> window.onload=function(){ getMap(); } // 向地圖添加信息:ajax function getMap(){ //創(chuàng)建對(duì)象 var httpReq; if(window.XMLHttpRequest){ httpReq=new XMLHttpRequest(); }else{ httpReq=new ActiveXObject("Microsoft.XMLHTTP"); } var maplist=document.getElementById("maplist");//獲取地圖列表 maplist.innerHTML='';//清空地圖里在html里面加的信息 // 定義回調(diào)函數(shù),接收從數(shù)據(jù)庫響應(yīng)回來的數(shù)據(jù)。 // onreadystatechange():存儲(chǔ)函數(shù)(或函數(shù)名)。每當(dāng)readyState屬性改變時(shí),就會(huì)調(diào)用該函數(shù) httpReq.onreadystatechange=function(){ if(httpReq.readyState==4&&httpReq.status==200){ var jsonobj=JSON.parse(httpReq.responseText); console.log(jsonobj.length); for (var i = 0; i< jsonobj.length;i++) { maplist.innerHTML+='<div class="mapShop">'+ '<img src="images/fi1.png" style="top:'+jsonobj[i].pmTop+"px"+';left:'+jsonobj[i].pmLeft+"px"+'"/>'+ '<div id="pmcity'+i+'" onclick="getMessage('+i+')" style="top:'+jsonobj[i].pmTop+"px"+';left:'+jsonobj[i].pmLeft+"px"+';position:absolute;padding-top:20px;'+'">' + jsonobj[i].pmCity + '</div>'+ '</div>'; } } } //發(fā)起請(qǐng)求(打開一個(gè)地址) httpReq.open("get", "adress.do", true); //發(fā)送,如果提交方式為get,發(fā)送為null;如果提交方式為post,哪send里寫要發(fā)送的參數(shù),沒得的話,就寫null httpReq.send(null); } //點(diǎn)擊獲取信息 function getMessage(a){ console.log("M----------1"); var httpReq; if(window.XMLHttpRequest){ httpReq=new XMLHttpRequest(); }else{ httpReq=new ActiveXObject("Microsoft.XMLHTTP"); } var map_right=document.getElementById("map_right"); map_right.innerHTML=''; httpReq.onreadystatechange=function(){ if(httpReq.readyState==4&&httpReq.status==200){ var jsonobj=JSON.parse(httpReq.responseText); console.log(jsonobj.length); for(var i=0;i<jsonobj.length;i++){ map_right.innerHTML+='<div class="shopMsg">'+ '<img src="images/'+jsonobj[i].pmImg+'"/>'+ '<div class="pmname">寵物店名:'+jsonobj[i].pmName+'</div>'+ '<div class="address">地址:'+jsonobj[i].pmAddress+'</div>'+ '<div class="phone">電話號(hào)碼:'+jsonobj[i].pmPhone+'</div>'+ '</div>' } } } //發(fā)起請(qǐng)求 httpReq.open("get", "adressMsg.do?pmId="+a, true); //發(fā)送 httpReq.send(null); } </script> </body> </html>
服務(wù)端部分:app.js(一個(gè)JavaScript):
var express=require("express");//引用express var mysql=require("mysql");//引用mysql var app=express();//執(zhí)行express里的全局函數(shù),返回一個(gè)express對(duì)象 app.configure(function(){ app.use(app.router);//路由,配置路由時(shí),先執(zhí)行,用戶定義的攔截地址 app.use(express.static(__dirname+"/public"));//設(shè)置靜態(tài)資源路徑 app.use(express.errorHandler());//開發(fā)者模塊,將錯(cuò)誤顯示在html上 }); app.get("/adress.do",function(req,res){ //console.log("d-----------1"); //建立數(shù)據(jù)庫連接,建立橋梁 var myconn=mysql.createConnection({ host:"localhost", port:"3306", user:"root", password:"123456", database:"pet" }); //打開連接 myconn.connect(); var sql="SELECT * FROM petmap"; //console.log(sql); myconn.query(sql,[],function(err,data){ //console.log(err); //console.log(data); res.send(data); }); //關(guān)閉連接 myconn.end(); }); //城市點(diǎn)擊響應(yīng) app.get("/adressMsg.do",function(req,res){ var pmId=req.query.pmId; console.log(pmId); //建立數(shù)據(jù)庫連接,建立橋梁 var myconn=mysql.createConnection({ host:"localhost", port:"3306", user:"root", password:"123456", database:"pet" }); //打開連接 myconn.connect(); console.log("f------------1"); var sql="SELECT * FROM petmap WHERE pmId=?"; console.log(sql); var id=parseInt(pmId); myconn.query(sql,[id+1],function(err,data){ console.log(err); console.log(data); res.send(data); }); //關(guān)閉連接 myconn.end(); }); //監(jiān)聽端口號(hào) app.listen(8888,function(){//監(jiān)聽 console.log("express監(jiān)聽成功!"); console.log(__dirname); });
數(shù)據(jù)庫mysql信息:
/*創(chuàng)建數(shù)據(jù)庫:pet*/ CREATE DATABASE pet; /*寵物店地圖*/ CREATE TABLE petmap(/*寵物店*/ pmId INT AUTO_INCREMENT PRIMARY KEY,/*寵物店id*/ pmName NVARCHAR(60),/*寵物店名*/ pmCity NVARCHAR(20),/*寵物店所在城市*/ pmAddress NVARCHAR(100),/*寵物店所在詳細(xì)地址*/ pmImg VARCHAR(60),/*寵物店圖片*/ pmPhone VARCHAR(30),/*寵物店電話號(hào)碼*/ pmTop FLOAT,/*寵物店位置上面*/ pmLeft FLOAT/*寵物店位置下面*/ ) /*插入信息*/ INSERT INTO petmap(pmName,pmCity,pmAddress,pmImg,pmPhone,pmTop,pmLeft) VALUES ('邛崍邛臨美多寵物服務(wù)部','成都','成都市邛崍市長松路296號(hào)','map1.png','15202891690',360,320), ('諧和寵物醫(yī)院','德陽','德陽市旌陽區(qū)珠江西路300號(hào)','map2.png','0838-6181255',320,350), ('天寧動(dòng)物醫(yī)院','西安','西安市新城區(qū)韓森路','map3.png','028-81836050',260,240), ('寵美康動(dòng)物醫(yī)院','烏魯木齊','烏魯木齊市天山區(qū)幸福路774號(hào)','map4.png','0991-2654158',210,170), ('綿陽康貝動(dòng)物診所','綿陽','綿陽市游仙區(qū)東津路5-2號(hào)','map5.png','0816-2987186',315,335), ('圣心動(dòng)物醫(yī)院','重慶','重慶市九龍坡區(qū)大公館九龍大廈3-2','map6.png','023-68820999',360,380), ('吉祥寵物醫(yī)院(油榨街店)','貴陽','貴陽市南明區(qū)油榨街花鳥市場寵物區(qū)','map7.png','0851-88275946',400,380), ('常德市武陵區(qū)動(dòng)物醫(yī)院','常德','常德市武陵區(qū)青年路478號(hào)','map8.png','0736-7236814',230,393), ('愛爾寵物','鄭州','鄭州市金水區(qū)金水東路3-6號(hào)','map9.png','0371-69193157',300,453), ('長沙市博旺寵物診所','長沙','長沙市天心區(qū)西牌樓街41號(hào)附近','map10.png','0731-82329801',370,443), ('大嘴狗寵物醫(yī)院','合肥','合肥市廬陽區(qū)北一環(huán)與肥西路交口向南','map11.png','0551-64286773',330,500), ('秦皇島市寵物醫(yī)院','秦皇島','秦皇島市海港區(qū)海陽路9號(hào)','map12.png','0335-3076769',165,540); INSERT INTO petmap(pmName,pmCity,pmAddress,pmImg,pmPhone,pmTop,pmLeft) VALUES ('乖乖寵寵物醫(yī)院','天津','天津市河?xùn)|區(qū)萬東路77號(hào)(近8630醫(yī)院)','map13.png','13820105131',195,510), ('北京寵物醫(yī)院','北京','北京市西城區(qū)百萬莊北里14號(hào)','map14.png','010-88377484',198,490), ('愛寵之家寵物醫(yī)院','哈爾濱','哈爾濱市南崗區(qū)鼎新三道街37號(hào)','map15.png','0451-82516177',80,625); INSERT INTO petmap(pmName,pmCity,pmAddress,pmImg,pmPhone,pmTop,pmLeft) VALUES ('拉薩妙妙安心寵物診所','西藏','拉薩市城關(guān)區(qū)納金路城東工商1樓','map16.png','0891-6223291',360,170);
最終結(jié)果:
以上所述是小編給大家介紹的通過數(shù)據(jù)庫和ajax方法寫出地圖的實(shí)例代碼,希望對(duì)大家有所幫助,如果大家有任何疑問歡迎給我留言,小編會(huì)及時(shí)回復(fù)大家的,在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
Ajax異步請(qǐng)求的五個(gè)步驟及實(shí)戰(zhàn)案例
通過在后臺(tái)與服務(wù)器進(jìn)行少量數(shù)據(jù)交換,Ajax可以使網(wǎng)頁實(shí)現(xiàn)異步更新,下面這篇文章主要給大家介紹了關(guān)于Ajax異步請(qǐng)求的五個(gè)步驟及實(shí)戰(zhàn)案例的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-08-08ajax方式實(shí)現(xiàn)注冊(cè)功能(提交數(shù)據(jù)到后臺(tái)數(shù)據(jù)庫完成交互)
這篇文章主要介紹了ajax方式實(shí)現(xiàn)注冊(cè)功能,提交數(shù)據(jù)到后臺(tái)數(shù)據(jù)庫完成交互,感興趣的小伙伴們可以參考一下2016-08-08jQuery通過Ajax向PHP服務(wù)端發(fā)送請(qǐng)求并返回JSON數(shù)據(jù)
這篇文章主要介紹了jQuery通過Ajax向PHP服務(wù)端發(fā)送請(qǐng)求并返回JSON數(shù)據(jù),設(shè)計(jì)到的知識(shí)點(diǎn)有jquery、ajax、php、json,感興趣的朋友一起學(xué)習(xí)下jquery ajax 返回json2015-10-10重寫 ajax 實(shí)現(xiàn) session 超時(shí)跳轉(zhuǎn)到登錄頁面實(shí)例代碼
這篇文章主要介紹了重寫 ajax 實(shí)現(xiàn) session 超時(shí)跳轉(zhuǎn)到登錄頁面實(shí)例代碼,需要的朋友可以參考下2017-05-05JQuery的ajax的用法在asp中使用$.ajax()實(shí)現(xiàn)
ajax的出現(xiàn)解決了很多的疑難問題,同時(shí)帶來了很多的方便,本文講述一下JQuery的ajax的用法在asp中使用$.ajax()來表示,感興趣的朋友可以了解下,或許本文對(duì)你有所幫助2013-01-01ajax智能提示+textbox動(dòng)態(tài)生成下拉框示例代碼
ajax智能提示+textbox動(dòng)態(tài)生成下拉框,下面有個(gè)不錯(cuò)的示例,感興趣的朋友可以參考下,希望對(duì)大家有所幫助2013-12-12使用FormData進(jìn)行Ajax請(qǐng)求上傳文件的實(shí)例代碼
這篇文章主要介紹了使用FormData進(jìn)行Ajax請(qǐng)求上傳文件的實(shí)例代碼,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-08-08