這次帶給大家地圖網格的實現,地圖網格實現的注意事項有哪些,下面就是實戰案例,一起來看一下。
前言:最近要使用百度地圖實作樓盤視覺化的功能,因此最基礎的功能就是將地圖網格化以後實現不同地域的樓盤劃分;
1,自行去百度地圖的開放平台申請秘鑰哈,這裡我就把自己的秘鑰貼出來了;ak=A3CklGvnFOjkAzKzay2dySgfdig0GKz4
#2,新建一個簡單頁面,下面我把自己的頁面貼出來
nbsp;html> <meta> <meta> <style> html { height: 100% } body { height: 100%; margin: 0px; padding: 0px } #container { height: 100% } </style> <script></script> <script></script> <p></p> <script> var myMap = new ZMap("container"); </script>
3,其中引入了ziroom-map.js,這是我們公司的名字啦,我把程式碼貼出來,這個js是封裝了百度的js的api的,有人如果要問為什麼封裝,直接使用不可以麼?那我的回答是:封裝可以將具體業務和地圖結合,使程式碼更清晰,並且可以持久化當前地圖的狀態,利於實現對地圖的操作。
var ZMap = function (id, center, level) { this.initCenter = new ZPoint(116.404, 39.915);//初始化的中心点,同时为了定义网格的中心点 this.id = id;//p的id this.level = level ? level : 13;//地图级别 this.center = center ? center : this.initCenter;//中心点 this.map = null;//百度地图实例 this.xgrids = [];//经线 this.ygrids = [];//纬线 this.beSelectBounds = {}; this.bounds = null;//当前地图的四个顶点 this.span = null;//当前网格的跨度 this.init(); } ZMap.prototype = { init: function () {//全局初始化 var zMap = this; this.map = new BMap.Map(this.id); this.map.centerAndZoom(this.center.point, this.level); this.map.enableScrollWheelZoom(); this.map.disableInertialDragging(); this.map.addControl(new BMap.NavigationControl({ anchor: BMAP_ANCHOR_BOTTOM_RIGHT, type: BMAP_NAVIGATION_CONTROL_ZOOM })); //缩放按钮 this.map.addControl(new BMap.ScaleControl({anchor: BMAP_ANCHOR_BOTTOM_LEFT, offset: new BMap.Size(80, 25)})); //比例尺 this.map.disableDoubleClickZoom(); this.map.setMapStyle({style: 'googlelite'}); this.initProperty(); this.initGrid(); //添加移动后的点击事件 this.map.addEventListener("dragend", function () { zMap.initProperty(); zMap.initGrid(); }); //添加放大或缩小时的事件 this.map.addEventListener("zoomend", function () { zMap.initProperty(); zMap.initGrid(); }); //设置点击事件 this.map.addEventListener("click", function (e) { var point = e.point; //获取当前点是在哪个区块内,获取正方形的四个顶点 var points = zMap.getGrid(point); //判断当前区域是否已经被选中过,如果被选中过则取消选中 var key = '' + points[0].lng + points[0].lat + points[2].lng + points[2].lat;//使用两个点的坐标作为key if (zMap.beSelectBounds[key]) { zMap.map.removeOverlay(zMap.beSelectBounds[key]); delete zMap.beSelectBounds[key]; return; } var polygon = new BMap.Polygon(points, {strokeColor: "red", strokeWeight: 2, strokeOpacity: 0.5}); zMap.map.addOverlay(polygon); zMap.beSelectBounds[key] = polygon; }); }, initProperty: function () {//初始化当前地图的状态 this.level = this.map.getZoom(); this.bounds = { x1: this.map.getBounds().getSouthWest().lng, y1: this.map.getBounds().getSouthWest().lat, x2: this.map.getBounds().getNorthEast().lng, y2: this.map.getBounds().getNorthEast().lat }; this.span = this.getSpan();//需要使用level属性 }, initGrid: function () {//初始化网格 var zMap = this; //将原来的网格线先去掉 for (var i in zMap.xgrids) { this.map.removeOverlay(zMap.xgrids[i]); } zMap.xgrids = []; for (var i in zMap.ygrids) { this.map.removeOverlay(zMap.ygrids[i]); } zMap.ygrids = []; //获取当前网格跨度 var span = zMap.span; //初始化地图上的网格 for (var i = zMap.bounds.x1 + (zMap.initCenter.point.lng - zMap.bounds.x1) % span.x - span.x; i <p style="text-align: left;"><span style="color: #ff0000"></span></p><p>相信看了本文案例你已經掌握了方法,更多精彩請關注php中文網其它相關文章! </p><p>推薦閱讀:</p><p style="text-align: left;"><a href="http://www.php.cn/js-tutorial-389857.html" target="_blank">Express與Koa2的使用詳解</a><br></p><p style="text-align: left;"><a href="http://www.php.cn/js-tutorial-389855.html" target="_blank">Singleton封裝增刪改查</a><br></p><p style="text-align: left;"><a href="http://www.php.cn/js-tutorial-389852.html" target="_blank"></a><br></p>########################################################### #########React開發時的eslint配置#########
以上是圖網格的實現的詳細內容。更多資訊請關注PHP中文網其他相關文章!