使用百度地图实现地图网格的示例


Posted in Javascript onFebruary 06, 2018

前言:最近要使用百度地图实现楼盘可视化的功能,因此最基础的功能就是将地图网格化以后实现不同地域的楼盘划分;

1,自行去百度地图的开放平台申请秘钥哈,这里我就把自己的秘钥贴出来了;ak=A3CklGvnFOjkAzKzay2dySgfdig0GKz4

2,新建一个简单页面,下面我把自己的页面贴出来

<!DOCTYPE html>
<html>
<head>
 <meta name="viewport" content="initial-scale=1.0, user-scalable=no"/>
 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
 <style type="text/css">
  html {
   height: 100%
  }
  body {
   height: 100%;
   margin: 0px;
   padding: 0px
  }
  #container {
   height: 100%
  }
 </style>
 <script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=A3CklGvnFOjkAzKzay2dySgfdig0GKz4"></script> 
<script type="text/javascript" src="ziroom-map.js"></script>
 </head>
 <body> 
<div id="container"></div>
 <script> 
var myMap = new ZMap("container"); </script>
 </body>
 </html>

3,其中引入了ziroom-map.js,这是我们公司的名字啦,我把代码贴出来,这个js是封装了百度的js的api的,有人如果要问为什么封装,直接使用不可以么?那我的回答是:封装可以将具体业务和地图相结合,使代码更清晰,并且可以持久化当前地图的状态,利于实现对地图的操作。

var ZMap = function (id, center, level) {
 this.initCenter = new ZPoint(116.404, 39.915);//初始化的中心点,同时为了定义网格的中心点
 this.id = id;//div的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 < zMap.bounds.x2 + span.x; i += span.x) {
   var polyline = new BMap.Polyline([
    new BMap.Point(i.toFixed(6), zMap.bounds.y1),
    new BMap.Point(i.toFixed(6), zMap.bounds.y2)
   ], {strokeColor: "black", strokeWeight: 1, strokeOpacity: 0.5});
   zMap.xgrids.push(polyline);
   zMap.map.addOverlay(polyline);
  }
  for (var i = zMap.bounds.y1 + (zMap.initCenter.point.lat - zMap.bounds.y1) % span.y - span.y; i < zMap.bounds.y2 + span.y; i += span.y) {
   var polyline = new BMap.Polyline([
    new BMap.Point(zMap.bounds.x1, i.toFixed(6)),
    new BMap.Point(zMap.bounds.x2, i.toFixed(6))
   ], {strokeColor: "black", strokeWeight: 1, strokeOpacity: 0.5});
   zMap.ygrids.push(polyline);
   zMap.map.addOverlay(polyline);
  }
 },
 getSpan: function () {//获取网格的跨度
  var scale = 0.75;
  var x = 0.00064;
  for (var i = this.level; i < 19; i++) {
   x *= 2;
  }
  var y = parseFloat((scale * x).toFixed(5));
  return {x: x, y: y};
 },
 getGrid: function (point) {//返回当前点在所在区块的四个顶点
  var zMap = this;
  //先找出两条纵线坐标
  var xpoints = this.xgrids.map(function (polyline) {
   return polyline.getPath()[0].lng;
  }).filter(function (lng) {
   return Math.abs(lng - point.lng) <= zMap.span.x;
  }).sort(function (a, b) {
   return a - b;
  }).slice(0, 2);
  //再找出两条横线的坐标
  var ypoints = this.ygrids.map(function (polyline) {
   return polyline.getPath()[0].lat;
  }).filter(function (lat) {
   return Math.abs(lat - point.lat) <= zMap.span.y;
  }).sort(function (a, b) {
   return a - b;
  }).slice(0, 2);
  return [
   new BMap.Point(xpoints[0], ypoints[0]),
   new BMap.Point(xpoints[0], ypoints[1]),
   new BMap.Point(xpoints[1], ypoints[1]),
   new BMap.Point(xpoints[1], ypoints[0])
  ];
 },
 reset: function () {//重置
  this.map.reset();
 }
}
var ZPoint = function (x, y, code) {
 this.code = code;
 this.point = new BMap.Point(x, y);
}

总结:好了这篇随笔就这么多了,欢迎大家指正。

以上这篇使用百度地图实现地图网格的示例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Javascript 相关文章推荐
背景音乐每次刷新都可以自动更换
Feb 01 Javascript
Mootools 1.2教程 排序类和方法简介
Sep 15 Javascript
jquery操作select大全
Apr 25 Javascript
js重写alert控件(适合学习js的新手朋友)
Aug 24 Javascript
javascript正则表达式中的replace方法详解
Apr 20 Javascript
DOM 高级编程
May 06 Javascript
Linux下为Node.js程序配置MySQL或Oracle数据库的方法
Mar 19 Javascript
Bootstrap基本插件学习笔记之按钮(21)
Dec 08 Javascript
JS使用正则表达式找出最长连续子串长度
Oct 26 Javascript
javascript定时器的简单应用示例【控制方块移动】
Jun 17 Javascript
原生javascript自定义input[type=radio]效果示例
Aug 27 Javascript
js防抖函数和节流函数使用场景和实现区别示例分析
Apr 11 Javascript
js中的闭包学习心得
Feb 06 #Javascript
JS基于设计模式中的单例模式(Singleton)实现封装对数据增删改查功能
Feb 06 #Javascript
Vue仿今日头条实例详解
Feb 06 #Javascript
electron demo项目npm install安装失败的解决方法
Feb 06 #Javascript
详解React开发必不可少的eslint配置
Feb 05 #Javascript
详解js的作用域、预解析机制
Feb 05 #Javascript
Vue使用枚举类型实现HTML下拉框步骤详解
Feb 05 #Javascript
You might like
PHP实现文件安全下载
2006/10/09 PHP
PHP获取当前页面URL函数实例
2014/10/22 PHP
关于ThinkPHP中的异常处理详解
2018/05/11 PHP
!DOCTYPE声明对JavaScript的影响分析
2010/04/12 Javascript
javascript是怎么继承的介绍
2012/01/05 Javascript
javascript之typeof、instanceof操作符使用探讨
2013/05/19 Javascript
js获取url中指定参数值的示例代码
2013/12/14 Javascript
你可能不知道的JavaScript的new Function()方法
2014/04/17 Javascript
jquery实现鼠标滑过小图时显示大图的方法
2015/01/14 Javascript
JS简单实现动画弹出层效果
2015/05/05 Javascript
深入探讨javascript函数式编程
2015/10/11 Javascript
不得不分享的JavaScript常用方法函数集(下)
2015/12/25 Javascript
基于javascript bootstrap实现生日日期联动选择
2016/04/07 Javascript
Bootstrap插件全集
2016/07/18 Javascript
JS实现图文并茂的tab选项卡效果示例【附demo源码下载】
2016/09/21 Javascript
Bootstrap CSS使用方法
2016/12/23 Javascript
vue router demo详解
2017/10/13 Javascript
jQuery实现新闻播报滚动及淡入淡出效果示例
2018/03/23 jQuery
python中pandas.DataFrame排除特定行方法示例
2017/03/12 Python
Python虚拟环境virtualenv的安装与使用详解
2017/05/28 Python
Python2.7编程中SQLite3基本操作方法示例
2017/08/09 Python
Python基础之getpass模块详细介绍
2017/08/10 Python
详解django三种文件下载方式
2018/04/06 Python
Python使用matplotlib和pandas实现的画图操作【经典示例】
2018/06/13 Python
python 中不同包 类 方法 之间的调用详解
2020/03/09 Python
HTML5中的进度条progress元素简介及兼容性处理
2016/06/02 HTML / CSS
德国机车企业:FC-Moto
2017/10/27 全球购物
CSMA/CD介质访问控制协议
2015/11/17 面试题
师范生自荐信范文
2013/10/06 职场文书
2015年实习单位评语
2015/03/25 职场文书
简单的辞职信范文(2016最新版)
2015/05/12 职场文书
微信搭讪开场白
2015/05/28 职场文书
2016年党员读书月活动总结
2016/04/06 职场文书
Go语言空白表示符_的实例用法
2021/07/04 Golang
Python Pandas数据分析之iloc和loc的用法详解
2021/11/11 Python
angular4实现带搜索的下拉框
2022/03/25 Javascript