百度地图一套JS API,非常实用
百度地图一套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,非常实用的更多相关文章
- 百度地图经纬度转换JS版
//百度地图的坐标转换,由于百度地图在GCJ02协议的基础上又做了一次处理,变为 BD09协议的坐标,以下是坐标的转化方式,可以方便和其他平台转化 jQuery.MapConvert = { x_pi ...
- 利用百度地图WEB服务APIGeoCoding API批量地址解析
Geocoding API包括地址解析和逆地址解析功能: 地理编码:即地址解析,由详细到街道的结构化地址得到百度经纬度信息,例如:“北京市海淀区中关村南大街27号”地址解析的结果是“lng:116.3 ...
- Java web与web gis学习笔记(二)——百度地图API调用
系列链接: Java web与web gis学习笔记(一)--Tomcat环境搭建 Java web与web gis学习笔记(二)--百度地图API调用 JavaWeb和WebGIS学习笔记(三)-- ...
- Qt的QWebChannel和JS、HTML通信/交互驱动百度地图
Qt的QWebChannel和JS.HTML通信/交互驱动百度地图 0 前言 我一个研究嵌入式的,不知道怎么就迷上了上位机,接了几个项目都是关于Qt,这个项目还是比较经典的,自己没事儿的时候也进行研究 ...
- 百度地图API开发的快速使用和大量坐标点操作【点聚合,海量点,mapv】
快速上手 注意:本篇文章代码是基于 百度地图 JavaScript API v3.0 的条件下编写,GL版本可能稍有变化. 地图嘛,很重要的一部分就是坐标经纬度了: 经度: 英文 longitude ...
- 百度地图API地理位置和坐标转换
1.由地名(省份.城市.街道等)得到其对应的百度地图坐标: http://api.map.baidu.com/geocoder/v2/?output=json&ak=你从百度申请到的Key&a ...
- 百度地图API使用方法详解
最近做了个项目,其中项目中有个需求需要用到百度地图进行导航,通过查阅相关资料参考百度地图api完成了一个例子. API地址:http://developer.baidu.com/map/jsdemo. ...
- ***微信LBS地理位置开发+百度地图API(地理位置和坐标转换)
微信公众平台开发 - 获取用户地理位置 本文介绍在微信公众平台上如何使用高级接口开发获取用户地理位置的功能. 一.获取用户地理位置接口 开通了上报地理位置接口的公众号,用户在关注后进入公众号会话时,会 ...
- 百度地图API简单使用
百度地图API是由JavaScript语言编写的,在使用之前需要将API引用到页面中: 现在新版本的需要密钥,下面用的是旧版的 <script src="http://api.map ...
随机推荐
- .pyc & Python
.pyc & Python Python bytecode / 字节码 Python compiles the .py files and saves it as .pyc files , s ...
- web & js & touch & gesture
web & js & touch & gesture 触摸 & 手势 https://caniuse.com/#feat=touch js https://develo ...
- IM & RTC
IM & RTC 即时通信(IM) & 实时通信(RTC) 场景 即时通信(可靠性高,延时高) 场景包括文字聊天.语音消息发送.文件传输.音视频播放等; 发短信 实时通信(可靠性低,延 ...
- 华盛顿邮报专访:SPC能否再掀起币圈新浪潮?
近日,美国知名媒体华盛顿邮报对话NGK灵石团队技术副总裁Daphne Patel女士,对话主题为"SPC能否再掀起币圈新浪潮".此次对话以问答的形式展开,将SPC的最新情况呈现在你 ...
- SCSS引入通用SCSS
新建通用common.scss .empty { margin-top: 100rpx; text-align: center; .empty-img { width: 220rpx; height: ...
- 1071 Speech Patterns——PAT甲级真题
1071 Speech Patterns People often have a preference among synonyms of the same word. For example, so ...
- 开源OA办公系统的“应用市场”,能够为协同办公开拓什么样的“前路”?
在我们的日常生活中,应用市场这个词,总是与智能手机划上等号,不管使用的是iPhone还是安卓,总会接触到手机上的APP应用市场,我们可以在应用市场中,选择自己所需要的APP应用软件,下载使用后,可以让 ...
- 翻译:《实用的Python编程》02_06_List_comprehension
目录 | 上一节 (2.5 collections模块) | 下一节 (2.7 对象模型) 2.6 列表推导式 一个常见的任务是处理列表中的项(译注:元素).本节介绍列表推导式,完成此任务的强大工具. ...
- Error Code: 1366. Incorrect DECIMAL value: '0' for column '' at row -1 0.266 sec;
Reference: https://stackoverflow.com/questions/35037288/incorrect-decimal-integer-value-mysql Er ...
- 完全基于node的web应用
完全基于node的web应用 node js web fs fs文件路径 事实上通常"正确的方式"一般都不简单. 用例 模块 基本http服务器 基于事件驱动回调 模块化serve ...