当前位置:网站首页>Baidu map usage
Baidu map usage
2022-07-28 22:34:00 【Crane sorrow_】
AK obtain
1. Register an account on Baidu map Developer Platform https://lbsyun.baidu.com/
2. Create a map app
3. obtain AK
Use Baidu map
1. introduce js
<script>
type="text/javascript"
src="https://api.map.baidu.com/api?v=1.0&&type=webgl&ak= Your key "
</script>
2. Create a container for the map ( You can define the width and height of the map )
<div id="container"></div>
<style type="text/css">
/* The width and height of the container map */
#container {
height: 600px;
width: 800px;
}
</style>
3. Initialize map
var map = new BmapGL.Map("container")
var point = new BmapGL.Point( longitude , latitude )
map.centerAndZoom(point,16)Add functionality
You can add many functions to the created map : Mouse scrolling and contracting 、 Add some 、 draw a circle 、 Add text annotations
1. Mouse scrolling and zooming
map.enableScrollWheelZoom(true);
2. Add some
var marker = new BMapGL.Marker(point);
map.addOverlay(marker);
You can also link the added points
var lineArr = [];
// Record one by one
var last = null;
// Add event
map.addEventListener("click",e=>{
// If there is a previous one, remove the previous one
last?map.removeOverlay(last):'';
// Get the location of the click point ( longitude , latitude )
var p = new BMapGL.Point(e.latlng.lng, e.latlng.lat);
// Add to array component
lineArr.push(p);
// Create a tag
var m = new BMapGL.Marker(p);
// Redefine the previous
last = m;
// show marks
map.addOverlay(m);
})
Then add an event to the mouse to make the previous point link when clicking the mouse
map.addEventListener("dblclick",function(){
tempMarker?map.removeOverlay(tempMarker):'';// If there is tempMarker Remove
list.push(list[0]);// The first 0 Copy one point and add it to the end
// Draw the line
var polyline = new BMapGL.Polygon(list, {strokeColor:"blue", strokeWeight:5, strokeOpacity:0.5,fillColor:"#f00",fillOpacity:0.5})
list = [];// Empty list
map.addOverlay(polyline);
})After completing these, you will get the following map

3. add controls
var scaleCtrl = new BMapGL.ScaleControl(); // Add scale bar control
map.addControl(scaleCtrl);
var zoomCtrl = new BMapGL.ZoomControl(); // Add a zoom control
map.addControl(zoomCtrl);
var cityCtrl = new BMapGL.CityListControl(); // Add city list control
map.addControl(cityCtrl);
4. draw a circle
var circle = new BMapGL.Circle(point,1000,{strokeColor:"blue", strokeWeight:2, strokeOpacity:0.5}); // Create circles
map.addOverlay(circle);
5. Add text annotations
var content = "<strong style='color:red'> China </strong>";
var label = new BMapGL.Label(content, { // Create text annotation
position: point, // Set the geographic location of the dimension
offset: new BMapGL.Size(10, 20) // Set the offset of the dimension
})
map.addOverlay(label);
6. Search for
var map = new BMapGL.Map('container');
var point = new BMapGL.Point(113.6648,34.7835);
map.centerAndZoom(point,17);
// Define a local map search
var local = new BMapGL.LocalSearch(map,{
renderOptions:{map:map}
})
$(function(){
// add to input event
$(".keyword").on("input",function(e){
// adopt ajax Request server address
$.ajax({
url:`https://api.map.baidu.com/place/v2/suggestion?query=${$(".keyword").val()}®ion= zhengzhou &city_limit=true&output=json&ak=3pu554R4LxktRbjImRnmwFqo7yIxI5yV`,
dataType:"jsonp" // type jsonp
})
.then(res=>{
// Output the requested content
console.log(res);
var str = "";
// Variable result Add up str
for(var i=0;i<res.result.length;i++){
str+=`<div class="item">${res.result[i].name}</div>`
}
// Set up thml
$(".tip").html(str);
})
})
// single click tip Get corresponding item Value , hide tip
$(".tip").on("click",".item",function(){
// Using the proxy method , obtain item Content
var str = $(this).text();
// Output
local.search(str); // Search for keywords
console.log(str,"this");
// Empty tip
$(".tip").html("");
})
$.ajax({
url:"https://api.map.baidu.com/location/ip?ak=3pu554R4LxktRbjImRnmwFqo7yIxI5yV&ip=&coor=bd09ll",
dataType:"jsonp"
})
.then(res=>{
console.log(res);
$("body").append(`<p>${res.content.address}</p>`)
})
})边栏推荐
- Sword finger offer II 056. Sum of two nodes in a binary search tree (simple binary search tree DFS hash table double pointer iterator)
- Analysis notes on let (const) temporary dead zone in JS
- Jmeter 安装第三方插件 Plugins Manager
- Is mov format a still image file format
- MySQL command (add, delete, check and modify)
- MySQL built-in functions
- 纪念一下第一次写的线段树了喽(对应洛谷3372)
- Att & CK preliminary understanding
- mysql8.0无法给用户授权或提示You are not allowed to create a user with GRANT的问题
- Closure, prototype and original link
猜你喜欢
![[LiteratureReview]Object Detection and Mapping with Bounding Box Constraints](/img/37/7cb5fa3a9078a5f5947485147c819d.png)
[LiteratureReview]Object Detection and Mapping with Bounding Box Constraints

CMD common commands

想要快速成长,先要经历重大打击!

Can the MySQL create statement be used to create a table structure and append new records

静态成员static详解
How do we do full link grayscale on the database?

PaddleNLP基于ERNIR3.0文本分类:WOS数据集为例(层次分类)

Idea generate class diagram plug-in UML (super detailed)

What are the main functions and uses of LCR tester

普源示波器实际的使用效果怎么样
随机推荐
[Ruiji takeout project] Day5 - Chapter 6 mobile verification code login
Att & CK Threat Intelligence
HCIP(9)
Leetcode question brushing series - sum of majority type
HCIP(15)
DOM programming + events
Solve the problem that TS node xxx.ts executes TS code and reports errors
gprs网络指的是什么
ssh 免密码登录
flask之蓝图 补充openpyxl
ATT&CK 威胁情报
Ngrok intranet penetration
The binary search boundary value processing based on leetcode35 is used to clarify the boundary value of the judgment condition using the idea of interval
PaddleNLP基于ERNIR3.0文本分类以CAIL2018-SMALL数据集罪名预测任务为例【多标签】
2021 mathematical modeling group B exercise
Wechat applet uses canvas drawing, round avatar, network background, text, dotted line, straight line
【CVPR 2021】Cylinder3D:用于LiDAR点云分割的圆柱体非对称3D卷积网络
网易云信 2022Q2 产品补给站,快来获取你的产品补给计划吧!
[CS231N]Lecture_ 2:Image Classification pipelin
Less than a year after its establishment! MIT derivative quantum computing company completed financing of US $9million