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());//開(kāi)發(fā)者模塊,將錯(cuò)誤顯示在html上
});
app.get("/adress.do",function(req,res){
//console.log("d-----------1");
//建立數(shù)據(jù)庫(kù)連接,建立橋梁
var myconn=mysql.createConnection({
host:"localhost",
port:"3306",
user:"root",
password:"123456",
database:"pet"
});
//打開(kāi)連接
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ù)庫(kù)連接,建立橋梁
var myconn=mysql.createConnection({
host:"localhost",
port:"3306",
user:"root",
password:"123456",
database:"pet"
});
//打開(kāi)連接
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)聽(tīng)端口號(hào)
app.listen(8888,function(){//監(jiān)聽(tīng)
console.log("express監(jiān)聽(tīng)成功!");
console.log(__dirname);
});
/*創(chuàng)建數(shù)據(jù)庫(kù):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ù)部','成都','成都市邛崍市長(zhǎng)松路296號(hào)','map1.png','15202891690',360,320),
('諧和寵物醫(yī)院','德陽(yáng)','德陽(yáng)市旌陽(yáng)區(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),
('綿陽(yáng)康貝動(dòng)物診所','綿陽(yáng)','綿陽(yá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ī)院(油榨街店)','貴陽(yáng)','貴陽(yáng)市南明區(qū)油榨街花鳥(niǎo)市場(chǎng)寵物區(qū)','map7.png','0851-88275946',400,380),
('常德市武陵區(qū)動(dòng)物醫(yī)院','常德','常德市武陵區(qū)青年路478號(hào)','map8.png','0736-7236814',230,393),
('愛(ài)爾寵物','鄭州','鄭州市金水區(qū)金水東路3-6號(hào)','map9.png','0371-69193157',300,453),
('長(zhǎng)沙市博旺寵物診所','長(zhǎng)沙','長(zhǎng)沙市天心區(qū)西牌樓街41號(hào)附近','map10.png','0731-82329801',370,443),
('大嘴狗寵物醫(yī)院','合肥','合肥市廬陽(yáng)區(qū)北一環(huán)與肥西路交口向南','map11.png','0551-64286773',330,500),
('秦皇島市寵物醫(yī)院','秦皇島','秦皇島市海港區(qū)海陽(yáng)路9號(hào)','map12.png','0335-3076769',165,540);
INSERT INTO petmap(pmName,pmCity,pmAddress,pmImg,pmPhone,pmTop,pmLeft)
VALUES ('乖乖寵寵物醫(yī)院','天津','天津市河?xùn)|區(qū)萬(wàn)東路77號(hào)(近8630醫(yī)院)','map13.png','13820105131',195,510),
('北京寵物醫(yī)院','北京','北京市西城區(qū)百萬(wàn)莊北里14號(hào)','map14.png','010-88377484',198,490),
('愛(ài)寵之家寵物醫(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);
以上所述是小編給大家介紹的通過(guò)數(shù)據(jù)庫(kù)和ajax方法寫(xiě)出地圖的實(shí)例代碼,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)歡迎給我留言,小編會(huì)及時(shí)回復(fù)大家的,在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!