百度地图一套JS API,非常实用

import mapStyleJson from "./mapStyleJson";
import $ from "jquery"; class BaiduMapGl {
constructor(el, centerPoint, zoom) {
this.el = el;
this.centerPoint = centerPoint;
this.zoom = zoom;
this.init();
this.sq = [];
this.sh = [];
this.dz = [];
this.gb = [];
this.jk = [];
}
// 初始化地图
init() {
this.map = new BMapGL.Map(this.el, {
minZoom: 12,
});
this.setCenter(this.centerPoint, 12);
this.map.enableScrollWheelZoom(true);
this.map.setTilt(60);
this.map.setMapStyleV2({
styleJson: mapStyleJson,
});
return this;
}
// 设置地图中心点
setCenter(centerPoint, zoom) {
this.map.centerAndZoom(
new BMapGL.Point(centerPoint.lng, centerPoint.lat),
zoom
);
}
// 设置地图中心
setCenterPoint(lng, lat) {
this.centerPoint = {
lng,
lat,
};
}
// 绘制边界
drawPrismOverlay(data) {
let list = data.map((item) => {
return new BMapGL.Point(Number(item.lng), Number(item.lat));
});
let prism = new BMapGL.Prism(list, 200, {
topFillColor: "#00B0F0",
topFillOpacity: 0.3,
sideFillColor: "#1B9995",
sideFillOpacity: 1,
});
// strokeColor: '#1B9995',
// strokeWeight: 1,
// strokeOpacity: 0.5,
// fillColor:this.colorArr[idx],
this.map.addOverlay(prism);
return this;
} // 绘制边界
// drawPrismOverlay(data, border, borderOpacity, color, opacity, borderStyle) {
// let list = data.map((item) => {
// return new BMapGL.Point(Number(item.lng), Number(item.lat))
// })
// let polygon = new BMapGL.Polygon(list, {
// strokeColor: border || "#9200ff",//边线颜色
// strokeOpacity: borderOpacity || 0.5,
// fillColor: color || "#95d3dd",//填充颜色
// fillOpacity: opacity || 0.5,
// strokeStyle: borderStyle || "dashed",
// strokeWeight: 2
// })
// this.map.addOverlay(polygon)
// return this
// } // 批量绘制markers
batchMarkers(list, type) {
list.forEach((item) => {
this.drawMarker(item, type);
});
return this;
}
// 单点绘制 带label
drawMarker({ title, point, icon, size, offset, callback }, type) {
if (type === "default") {
// 点
let pt = new BMapGL.Point(point.lng, point.lat);
// icon
let ic = new BMapGL.Icon(icon, new BMapGL.Size(size.width, size.height));
// 创建标注
let marker = new BMapGL.Marker(pt, {
icon: ic,
});
// 标注点击事件
marker.addEventListener("click", function() {
callback();
});
this.map.addOverlay(marker);
// label
let label = new BMapGL.Label(title, {
position: pt, // 指定文本标注所在的地理位置
offset: new BMapGL.Size(offset.x, offset.y), //设置文本偏移量
});
// label样式
label.setStyle({
color: "#fff",
fontSize: "12px",
height: "20px",
lineHeight: "20px",
fontFamily: "微软雅黑",
background: "transparent",
border: "none",
});
this.map.addOverlay(label);
} else if (type === "sq") {
// 点
let pt = new BMapGL.Point(point.lng, point.lat);
// icon
let ic = new BMapGL.Icon(icon, new BMapGL.Size(size.width, size.height));
// label
// 创建标注
let marker = new BMapGL.Marker(pt, {
icon: ic,
});
this.map.addOverlay(marker); // 标注点击事件
marker.addEventListener("click", function() {
callback();
});
marker.addEventListener("mouseover", function() {
// 为标注设置标签
let label = new BMapGL.Label(title, {
position: pt, // 指定文本标注所在的地理位置
offset: new BMapGL.Size(offset.x, offset.y), //设置文本偏移量
});
// label样式
label.setStyle({
color: "#fff",
fontSize: "12px",
height: "20px",
lineHeight: "20px",
fontFamily: "微软雅黑",
background: "transparent",
border: "none",
});
marker.setLabel(label);
});
marker.addEventListener("mouseout", function() {
var label = this.getLabel();
//设置标签内容为空
label.setContent("");
label.setStyle({ display: "none" });
});
this.sq.push(marker);
// console.log(this.sq);
} else if (type === "dz") {
// 点
let pt = new BMapGL.Point(point.lng, point.lat);
// icon
let ic = new BMapGL.Icon(icon, new BMapGL.Size(size.width, size.height));
// label
// 创建标注
let marker = new BMapGL.Marker(pt, {
icon: ic,
});
this.map.addOverlay(marker);
let label = new BMapGL.Label(title, {
position: pt, // 指定文本标注所在的地理位置
offset: new BMapGL.Size(offset.x, offset.y), //设置文本偏移量
}); // 标注点击事件
marker.addEventListener("click", function() {
callback();
});
marker.addEventListener("mouseover", function() {
// 为标注设置标签
let label = new BMapGL.Label(title, {
position: pt, // 指定文本标注所在的地理位置
offset: new BMapGL.Size(offset.x, offset.y), //设置文本偏移量
});
// label样式
label.setStyle({
color: "#fff",
fontSize: "12px",
height: "20px",
lineHeight: "20px",
fontFamily: "微软雅黑",
background: "transparent",
border: "none",
});
marker.setLabel(label);
});
marker.addEventListener("mouseout", function() {
var label = this.getLabel();
//设置标签内容为空
label.setContent("");
label.setStyle({ display: "none" });
});
this.dz.push(marker);
} else if (type === "gb") {
// 点
let pt = new BMapGL.Point(point.lng, point.lat);
// icon
let ic = new BMapGL.Icon(icon, new BMapGL.Size(size.width, size.height));
// label
// 创建标注
let marker = new BMapGL.Marker(pt, {
icon: ic,
});
this.map.addOverlay(marker); // 标注点击事件
marker.addEventListener("click", function() {
callback();
});
marker.addEventListener("mouseover", function() {
// 为标注设置标签
let label = new BMapGL.Label(title, {
position: pt, // 指定文本标注所在的地理位置
offset: new BMapGL.Size(offset.x, offset.y), //设置文本偏移量
});
// label样式
label.setStyle({
color: "#fff",
fontSize: "12px",
height: "20px",
lineHeight: "20px",
fontFamily: "微软雅黑",
background: "transparent",
border: "none",
});
marker.setLabel(label);
});
marker.addEventListener("mouseout", function() {
var label = this.getLabel();
//设置标签内容为空
label.setContent("");
label.setStyle({ display: "none" });
});
this.gb.push(marker);
} else if (type === "jk") {
// 点
let pt = new BMapGL.Point(point.lng, point.lat);
// icon
let ic = new BMapGL.Icon(icon, new BMapGL.Size(size.width, size.height));
// label
// 创建标注
let marker = new BMapGL.Marker(pt, {
icon: ic,
});
this.map.addOverlay(marker); // 标注点击事件
marker.addEventListener("click", function() {
callback();
});
marker.addEventListener("mouseover", function() {
// 为标注设置标签
let label = new BMapGL.Label(title, {
position: pt, // 指定文本标注所在的地理位置
offset: new BMapGL.Size(offset.x, offset.y), //设置文本偏移量
});
// label样式
label.setStyle({
color: "#fff",
fontSize: "12px",
height: "20px",
lineHeight: "20px",
fontFamily: "微软雅黑",
background: "transparent",
border: "none",
});
marker.setLabel(label);
});
marker.addEventListener("mouseout", function() {
var label = this.getLabel();
//设置标签内容为空
label.setContent("");
label.setStyle({ display: "none" });
});
this.jk.push(marker);
}
} // 地图旋转动画
enableAnimation() {
const keyFrames = [
{
center: new BMapGL.Point(this.centerPoint.lng, this.centerPoint.lat),
zoom: 12,
tilt: 60,
heading: 0,
percentage: 0,
},
{
center: new BMapGL.Point(this.centerPoint.lng, this.centerPoint.lat),
zoom: this.zoom,
tilt: 50,
heading: 360,
percentage: 1,
},
];
const opts = {
duration: 1000, // 设置每次迭代动画持续时间
delay: 3000, // 设置动画延迟开始时间
interation: 1, // 设置动画迭代次数
};
const animation = new BMapGL.ViewAnimation(keyFrames, opts); // 初始化动画实例
this.map.startViewAnimation(animation);
return this;
}
// 清除所有markers及labels
clearAllMarkers() {
// 获取所有覆盖物
let length = this.map.getOverlays().length;
for (let i = length; i > 0; i--) {
let marker = this.map.getOverlays()[i - 1];
// 去除marker及label
if (marker.toString() == "Marker" || marker.toString() == "Label") {
this.map.removeOverlay(marker);
}
}
}
// 清除所有覆盖物
clearOverlays() {
this.map.clearOverlays();
}
// 获取小区uid
getLocalUid(XQ) {
const local = new BMapGL.LocalSearch(this.map, {
renderOptions: { map: this.map },
});
local.setMarkersSetCallback((pois) => {
console.log(pois);
pois.map((v) => {
this.map.removeOverlay(v.marker);
});
const uid = pois[0].uid;
this.drowBoundary(uid);
});
local.search(XQ);
}
//小区边界
drowBoundary(uid) {
$.ajax({
async: false,
url:
"http://map.baidu.com/?pcevaname=pc4.1&qt=ext&ext_ver=new&l=12&uid=" +
uid,
dataType: "jsonp",
jsonp: "callback",
success: (result) => {
console.log(result);
const content = result.content;
if (content.geo != null && content.geo != undefined) {
const geo = content.geo;
let points = this.coordinateToPoints(geo);
//point分组,得到多边形的每一个点,画多边形
if (points && points.indexOf(";") >= 0) {
points = points.split(";");
}
var arr = [];
for (let i = 0; i < points.length - 1; i++) {
var temp = points[i].split(",");
arr.push(
new BMapGL.Point(parseFloat(temp[0]), parseFloat(temp[1]))
);
}
//创建多边形
let polygon = new BMapGL.Prism(arr, 50, {
topFillColor: "#00B0F0",
topFillOpacity: 0.3,
sideFillColor: "#1B9995",
sideFillOpacity: 0.8,
});
this.map.addOverlay(polygon); //增加多边形
this.map.setViewport(polygon.getPath());
} else {
console.log("暂无小区边界信息");
}
},
});
}
//百度米制坐标转换为经纬度
coordinateToPoints(coordinate) {
let points = "";
if (coordinate) {
const projection = BMAP_NORMAL_MAP.getProjection();
if (coordinate && coordinate.indexOf("-") >= 0) {
coordinate = coordinate.split("-");
}
//取点集合
let tempco = coordinate[1];
if (tempco && tempco.indexOf(",") >= 0) {
tempco = tempco.replace(";", "").split(",");
}
//分割点,两个一组,组成百度米制坐标
let temppoints = [];
for (let i = 0, len = tempco.length; i < len; i++) {
temppoints.push({
lng: tempco[i],
lat: tempco[i + 1],
});
i++;
}
for (let i = 0, len = temppoints.length; i < len; i++) {
let pos = temppoints[i];
let point = projection.pointToLngLat(
new BMapGL.Pixel(pos.lng, pos.lat)
);
points += [point.lng, point.lat].toString() + ";";
}
}
return points;
}
} export default BaiduMapGl;

百度地图一套JS API,非常实用的更多相关文章

  1. 百度地图经纬度转换JS版

    //百度地图的坐标转换,由于百度地图在GCJ02协议的基础上又做了一次处理,变为 BD09协议的坐标,以下是坐标的转化方式,可以方便和其他平台转化 jQuery.MapConvert = { x_pi ...

  2. 利用百度地图WEB服务APIGeoCoding API批量地址解析

    Geocoding API包括地址解析和逆地址解析功能: 地理编码:即地址解析,由详细到街道的结构化地址得到百度经纬度信息,例如:“北京市海淀区中关村南大街27号”地址解析的结果是“lng:116.3 ...

  3. Java web与web gis学习笔记(二)——百度地图API调用

    系列链接: Java web与web gis学习笔记(一)--Tomcat环境搭建 Java web与web gis学习笔记(二)--百度地图API调用 JavaWeb和WebGIS学习笔记(三)-- ...

  4. Qt的QWebChannel和JS、HTML通信/交互驱动百度地图

    Qt的QWebChannel和JS.HTML通信/交互驱动百度地图 0 前言 我一个研究嵌入式的,不知道怎么就迷上了上位机,接了几个项目都是关于Qt,这个项目还是比较经典的,自己没事儿的时候也进行研究 ...

  5. 百度地图API开发的快速使用和大量坐标点操作【点聚合,海量点,mapv】

    快速上手 注意:本篇文章代码是基于 百度地图 JavaScript API v3.0 的条件下编写,GL版本可能稍有变化. 地图嘛,很重要的一部分就是坐标经纬度了: 经度: 英文 longitude ...

  6. 百度地图API地理位置和坐标转换

    1.由地名(省份.城市.街道等)得到其对应的百度地图坐标: http://api.map.baidu.com/geocoder/v2/?output=json&ak=你从百度申请到的Key&a ...

  7. 百度地图API使用方法详解

    最近做了个项目,其中项目中有个需求需要用到百度地图进行导航,通过查阅相关资料参考百度地图api完成了一个例子. API地址:http://developer.baidu.com/map/jsdemo. ...

  8. ***微信LBS地理位置开发+百度地图API(地理位置和坐标转换)

    微信公众平台开发 - 获取用户地理位置 本文介绍在微信公众平台上如何使用高级接口开发获取用户地理位置的功能. 一.获取用户地理位置接口 开通了上报地理位置接口的公众号,用户在关注后进入公众号会话时,会 ...

  9. 百度地图API简单使用

    百度地图API是由JavaScript语言编写的,在使用之前需要将API引用到页面中:  现在新版本的需要密钥,下面用的是旧版的 <script src="http://api.map ...

随机推荐

  1. CSS3 & Flex Layout All In One

    CSS3 & Flex Layout All In One demos https://www.cnblogs.com/xgqfrms/p/10769302.html .flex-contai ...

  2. svg click event bug & css pointer-events

    svg click event bug & css pointer-events svg click event not working Error OK ??? css class /* d ...

  3. js 位掩码

    原文 定义掩码 const mask0 = parseInt("00000001", 2); const mask1 = parseInt("00000010" ...

  4. dart2native 使用Dart 在macOS,Windows或Linux上创建命令行工具

    下载dart2.6以上 >dart2native --help 编写源文件 // bin\main.dart main(List<String> args) { print('hel ...

  5. NGK项目好不好?

    在谈NGK项目之前,我们不得不提到NGK背后的研发团队,硅谷顶尖技术团队灵石团队.硅谷作为全世界最顶尖的高新技术和科技创新产业区,NGK.IO区块链系统正是在此处诞生. 灵石部门核心成员曾负责过多个P ...

  6. 利用 Java 操作 Jenkins API 实现对 Jenkins 的控制详解

    本文转载自利用 Java 操作 Jenkins API 实现对 Jenkins 的控制详解 导语 由于最近工作需要利用 Jenkins 远程 API 操作 Jenkins 来完成一些列操作,就抽空研究 ...

  7. EF多个表映射

    public class Media // One entity table { public int Id { get; set; } public string Name { get; set; ...

  8. smart-adminx项目导入依赖时,点击reinport时没反应且依赖全部报红的解决办法

    依赖报红的解决办法 报红效果如下: 原因分析:下载jar包时,出现大量以.lastUpdated结尾的无效文件. 解决办法:使用bat批处理文件批量删除无效文件 set REPOSITORY_PATH ...

  9. shell编程基础二

    一.流程控制 while循环:只要条件满足一直循环 read -p "请输入一个数字:" white_data while [ ${white_data} -lt 20 ] do ...

  10. 剑指 Offer 68 - I. 二叉搜索树的最近公共祖先 + 二叉排序树 + 最近公共祖先

    剑指 Offer 68 - I. 二叉搜索树的最近公共祖先 Offer_68_1 题目描述 方法一:迭代法 由于该题的二叉树属于排序二叉树,所以相对较简单. 只需要判断两个结点是否在根节点的左右子树中 ...