php+js實(shí)現(xiàn)百度地圖多點(diǎn)標(biāo)注的方法
本文實(shí)例講述了php+js實(shí)現(xiàn)百度地圖多點(diǎn)標(biāo)注的方法。分享給大家供大家參考,具體如下:
1.php創(chuàng)建json數(shù)據(jù)
$products = $this->product_db->select($where); $products_json = json_encode($products);
2.js傳入json數(shù)據(jù)
類(lèi)似于這樣的結(jié)構(gòu)
var markerArr = [{ title: "名稱(chēng):廣州火車(chē)站", point: "113.264531,23.157003", address: "廣東省廣州市廣州火車(chē)站", tel: "12306" }, { title: "名稱(chēng):廣州塔(赤崗塔)", point: "113.330934,23.113401", address: "廣東省廣州市廣州塔(赤崗塔) ", tel: "18500000000" }, { title: "名稱(chēng):廣州動(dòng)物園", point: "113.312213,23.147267", address: "廣東省廣州市廣州動(dòng)物園", tel: "18500000000" }, { title: "名稱(chēng):天河公園", point: "113.372867,23.134274", address: "廣東省廣州市天河公園", tel: "18500000000" }];
js擅長(zhǎng)處理json數(shù)據(jù)
<script> var products_json = {$products_json}; // 百度地圖 var citymap = new citymap(products_json,'宿遷'); </script>
3.處理地圖
document.write('<script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=1We8imivxZnaKMujZIrlDZ0v"></script>'); function citymap(markerArr, cityName){ this.markerArr = markerArr; this.cityName = cityName; this.initMap = function() { this.createMap();//創(chuàng)建地圖 this.setMapEvent();//設(shè)置地圖事件 this.addMapControl();//向地圖添加控件 }; this.createMap = function() { var map = new BMap.Map("dituContent");//在百度地圖容器中創(chuàng)建一個(gè)地圖 map.centerAndZoom(cityName,'13'); window.map = map;//將map變量存儲(chǔ)在全局 // 繪制點(diǎn) for (var i = 0; i < markerArr.length; i++) { var p0 = markerArr[i].baidu_lng; var p1 = markerArr[i].baidu_lat; var maker = this.addMarker(new window.BMap.Point(p0, p1),markerArr[i],i ); this.addInfoWindow(maker, markerArr[i], i); } }; this.addMarker = function(point,pro,index) { var myIcon = new BMap.Icon("http://api.map.baidu.com/img/markers.png", new BMap.Size(23, 25), { offset: new BMap.Size(10, 25), imageOffset: new BMap.Size(0, 0 - index * 25) }); var marker = new BMap.Marker(point, { icon: myIcon }); map.addOverlay(marker); var label = new BMap.Label(pro.name,{offset:new BMap.Size(20,-10)}); // 設(shè)置label樣式 label.setStyle({ color : "#CC3333", fontSize : "13px", backgroundColor :"#CCFFFF", border :"0", fontWeight :"bold" }); marker.setLabel(label); return marker; }; this.addInfoWindow = function(marker,pro) { //pop彈窗標(biāo)題 var title = '<div style="font-weight:bold;color:#CE5521;font-size:14px"><a href="?m=home&c=product&id='+ pro.id +'">' + pro.name + '</a></div>'; //pop彈窗信息 var html = []; html.push('<table cellspacing="0" style="table-layout:fixed;width:100%;font:12px arial,simsun,sans-serif"><tbody>'); html.push('<tr>'); html.push('<td style="vertical-align:top;line-height:16px;width:38px;white-space:nowrap;word-break:keep-all">地址:</td>'); html.push('<td style="vertical-align:top;line-height:16px">' + pro.address + ' </td>'); html.push('</tr>'); html.push('</tbody></table>'); var infoWindow = new BMap.InfoWindow(html.join(""), { title: title, width: 200 }); var openInfoWinFun = function() { marker.openInfoWindow(infoWindow); }; marker.addEventListener("click", openInfoWinFun); return openInfoWinFun; } this.setMapEvent = function() { map.enableDragging();//啟用地圖拖拽事件,默認(rèn)啟用(可不寫(xiě)) // map.enableScrollWheelZoom();//啟用地圖滾輪放大縮小 map.enableDoubleClickZoom();//啟用鼠標(biāo)雙擊放大,默認(rèn)啟用(可不寫(xiě)) map.enableKeyboard();//啟用鍵盤(pán)上下左右鍵移動(dòng)地圖 }; this.addMapControl = function() { //向地圖中添加縮放控件 var ctrl_nav = new BMap.NavigationControl({anchor:BMAP_ANCHOR_TOP_LEFT,type:BMAP_NAVIGATION_CONTROL_LARGE}); map.addControl(ctrl_nav); //向地圖中添加縮略圖控件 var ctrl_ove = new BMap.OverviewMapControl({anchor:BMAP_ANCHOR_BOTTOM_RIGHT,isOpen:1}); map.addControl(ctrl_ove); //向地圖中添加比例尺控件 var ctrl_sca = new BMap.ScaleControl({anchor:BMAP_ANCHOR_BOTTOM_LEFT}); map.addControl(ctrl_sca); }; this.initMap(); }
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專(zhuān)題:《php curl用法總結(jié)》、《PHP數(shù)組(Array)操作技巧大全》、《php排序算法總結(jié)》、《PHP常用遍歷算法與技巧總結(jié)》、《PHP數(shù)據(jù)結(jié)構(gòu)與算法教程》、《php程序設(shè)計(jì)算法總結(jié)》、《PHP數(shù)學(xué)運(yùn)算技巧總結(jié)》、《php正則表達(dá)式用法總結(jié)》、《PHP運(yùn)算與運(yùn)算符用法總結(jié)》、《php字符串(string)用法總結(jié)》及《php常見(jiàn)數(shù)據(jù)庫(kù)操作技巧匯總》
希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助。
相關(guān)文章
php解析字符串函數(shù)sscanf的實(shí)用方法
在PHP編程中,有時(shí)需要對(duì)字符串進(jìn)行解析,而sscanf函數(shù)就是一種非常方便的解析工具,本文詳細(xì)介紹了sscanf函數(shù)的用法,包括基本用法和高級(jí)用法,通過(guò)大量的示例代碼,展示了如何使用sscanf函數(shù)解析各種不同格式的字符串2023-09-09Zend framework處理一個(gè)http請(qǐng)求的流程分析
Zend framework處理一個(gè)http請(qǐng)求的流程分析,有助于大家提高知識(shí)面。2010-02-02php使用PhpSpreadsheet導(dǎo)出Excel表格的實(shí)例詳解
這篇文章將給大家介紹php使用PhpSpreadsheet導(dǎo)出Excel表格的實(shí)例,文中通過(guò)代碼示例給大家講解的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2024-01-01php解決注冊(cè)并發(fā)問(wèn)題并提高QPS
這篇文章主要為大家介紹了php解決注冊(cè)并發(fā)問(wèn)題并提高QPS實(shí)現(xiàn)方法示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-05-05php常用經(jīng)典函數(shù)集錦【數(shù)組、字符串、棧、隊(duì)列、排序等】
這篇文章主要介紹了php常用經(jīng)典函數(shù),總結(jié)分析了php數(shù)組、字符串、棧、隊(duì)列、排序等相關(guān)函數(shù)功能與使用技巧,需要的朋友可以參考下2019-08-08詳解WordPress中用于合成數(shù)組的wp_parse_args()函數(shù)
這篇文章主要介紹了WordPress中用于合成數(shù)組的wp_parse_args()函數(shù),轉(zhuǎn)換成數(shù)組通常是為了方便查詢(xún),需要的朋友可以參考下2015-12-12PHP魔術(shù)方法之__call與__callStatic使用方法
這篇文章主要介紹了PHP魔術(shù)方法之__call與__callStatic方法,需要的朋友可以參考下2017-07-07