百度地图一套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. Async Programming All in One

    Async Programming All in One Async & Await Frontend (async () => { const url = "https:// ...

  2. true && number !== boolean

    true && number !== boolean bug let result = ``; // section, name ? create text, compute cent ...

  3. C++算法代码——和为给定数

    题目来自:http://218.5.5.242:9018/JudgeOnline/problem.php?cid=1376&pid=0 题目描述 给出若干个整数,询问其中是否有一对数的和等于给 ...

  4. 1095 Cars on Campus——PAT甲级真题

    1095 Cars on Campus Zhejiang University has 6 campuses and a lot of gates. From each gate we can col ...

  5. 1.go语言入门----Helloworld与包引用

    HelloWorld与包引用 学习一门语言的惯例都是从helloworld开始,go语言也不例外 在gopath下的src中创建一个helloworld目录,创建main.go文件 package m ...

  6. SpringBoot源码解析

    1.@SpringBootApplication springboot采用注解方式开发的,当创建了一个springboot项目时,在启动类上会有一个注解@SpringBootApplication,这 ...

  7. Django登录使用的技术和组件

    登录 ''' 获取用户所有的数据 每条数据请求的验证 成功之后获取所有正确的信息 失败则显示错误信息 ''' #登陆页面管理 def login(request): if request.method ...

  8. Linux安装jdk(两种方式)

    最近在研究大数据方面的东西,业务场景是从设备采集数据经过处理然后存放DB. 建设上面的环境第一步肯定是安装jdk,所以和大家一起学一下基本知识centos7.5安装jdk1.8. 安装jdk有两种方法 ...

  9. 共享内存与存储映射(mmap)

    [前言]对这两个理解还是不够深刻,写一篇博客来记录一下. 首先关于共享内存的链接:共享内存.里面包含了创建共享内存区域的函数,以及两个进程怎么挂载共享内存通信,分离.释放共享内存. 共享内存的好处就是 ...

  10. SpringBoot(四): SpringBoot web开发 SpringBoot使用jsp

    1.在SpringBoot中使用jsp,需要在pom.xml文件中添加依赖 <!--引入Spring Boot内嵌的Tomcat对JSP的解析包--> <dependency> ...