web開發(fā)如何使用百度地圖實(shí)現(xiàn)地圖網(wǎng)格

這篇文章主要介紹了web開發(fā)如何使用百度地圖實(shí)現(xiàn)地圖網(wǎng)格,具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。

紅旗網(wǎng)站建設(shè)公司創(chuàng)新互聯(lián)建站,紅旗網(wǎng)站設(shè)計(jì)制作,有大型網(wǎng)站制作公司豐富經(jīng)驗(yàn)。已為紅旗1000多家提供企業(yè)網(wǎng)站建設(shè)服務(wù)。企業(yè)網(wǎng)站搭建\成都外貿(mào)網(wǎng)站建設(shè)公司要多少錢,請(qǐng)找那個(gè)售后服務(wù)好的紅旗做網(wǎng)站的公司定做!

前言:最近要使用百度地圖實(shí)現(xiàn)樓盤可視化的功能,因此最基礎(chǔ)的功能就是將地圖網(wǎng)格化以后實(shí)現(xiàn)不同地域的樓盤劃分;

1,自行去百度地圖的開放平臺(tái)申請(qǐng)秘鑰哈,這里我就把自己的秘鑰貼出來了;ak=A3CklGvnFOjkAzKzay2dySgfdig0GKz4

2,新建一個(gè)簡(jiǎn)單頁面,下面我把自己的頁面貼出來

<!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,這是我們公司的名字啦,我把代碼貼出來,這個(gè)js是封裝了百度的js的api的,有人如果要問為什么封裝,直接使用不可以么?那我的回答是:封裝可以將具體業(yè)務(wù)和地圖相結(jié)合,使代碼更清晰,并且可以持久化當(dāng)前地圖的狀態(tài),利于實(shí)現(xiàn)對(duì)地圖的操作。

var ZMap = function (id, center, level) {
 this.initCenter = new ZPoint(116.404, 39.915);//初始化的中心點(diǎn),同時(shí)為了定義網(wǎng)格的中心點(diǎn)
 this.id = id;//div的id
 this.level = level ? level : 13;//地圖級(jí)別
 this.center = center ? center : this.initCenter;//中心點(diǎn)
 this.map = null;//百度地圖實(shí)例
 this.xgrids = [];//經(jīng)線
 this.ygrids = [];//緯線
 this.beSelectBounds = {};
 this.bounds = null;//當(dāng)前地圖的四個(gè)頂點(diǎn)
 this.span = null;//當(dāng)前網(wǎng)格的跨度
 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();
  //添加移動(dòng)后的點(diǎn)擊事件
  this.map.addEventListener("dragend", function () {
   zMap.initProperty();
   zMap.initGrid();
  });
  //添加放大或縮小時(shí)的事件
  this.map.addEventListener("zoomend", function () {
   zMap.initProperty();
   zMap.initGrid();
  });
  //設(shè)置點(diǎn)擊事件
  this.map.addEventListener("click", function (e) {
   var point = e.point;
   //獲取當(dāng)前點(diǎn)是在哪個(gè)區(qū)塊內(nèi),獲取正方形的四個(gè)頂點(diǎn)
   var points = zMap.getGrid(point);
   //判斷當(dāng)前區(qū)域是否已經(jīng)被選中過,如果被選中過則取消選中
   var key = '' + points[0].lng + points[0].lat + points[2].lng + points[2].lat;//使用兩個(gè)點(diǎn)的坐標(biāo)作為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 () {//初始化當(dāng)前地圖的狀態(tài)
  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 () {//初始化網(wǎng)格
  var zMap = this;
  //將原來的網(wǎng)格線先去掉
  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 = [];
  //獲取當(dāng)前網(wǎng)格跨度
  var span = zMap.span;
  //初始化地圖上的網(wǎng)格
  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 () {//獲取網(wǎng)格的跨度
  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) {//返回當(dāng)前點(diǎn)在所在區(qū)塊的四個(gè)頂點(diǎn)
  var zMap = this;
  //先找出兩條縱線坐標(biāo)
  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);
  //再找出兩條橫線的坐標(biāo)
  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);
}

感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“web開發(fā)如何使用百度地圖實(shí)現(xiàn)地圖網(wǎng)格”這篇文章對(duì)大家有幫助,同時(shí)也希望大家多多支持創(chuàng)新互聯(lián),關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,更多相關(guān)知識(shí)等著你來學(xué)習(xí)!

網(wǎng)頁題目:web開發(fā)如何使用百度地圖實(shí)現(xiàn)地圖網(wǎng)格
當(dāng)前網(wǎng)址:http://bm7419.com/article24/gihjje.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供動(dòng)態(tài)網(wǎng)站、自適應(yīng)網(wǎng)站、網(wǎng)站建設(shè)、云服務(wù)器、App開發(fā)、網(wǎng)站改版

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

外貿(mào)網(wǎng)站制作