当前位置:网站首页>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>`)
})
})边栏推荐
- Less than a year after its establishment! MIT derivative quantum computing company completed financing of US $9million
- Sword finger offer II 057. the difference between the value and the subscript is within the given range (medium array bucket sort sliding window TreeSet)
- 2021 mathematical modeling group B exercise
- MySQL command (add, delete, check and modify)
- 【转载】token令牌在登录场景使用
- Summary of common error types in JS
- Leetcode question brushing series - sum of majority type
- Openresty request authentication
- 微信小程序使用canvas绘图,圆形头像,网络背景图,文字,虚线,直线
- PaddleNLP基于ERNIR3.0文本分类以CAIL2018-SMALL数据集罪名预测任务为例【多标签】
猜你喜欢

【CVPR 2021】Cylinder3D:用于LiDAR点云分割的圆柱体非对称3D卷积网络

微信小程序剪切图片的功能

6K6w5LiA5qyh5pS75Ye75YiG5p6Q

Ngrok intranet penetration

XXX port is already in use

Att & CK Threat Intelligence

79. Word search (medium string array matrix backtracking)

flask之蓝图 补充openpyxl
![[CVPR 2021] cylinder3d: cylindrical asymmetric 3D convolution network for LIDAR point cloud segmentation](/img/3d/3def78ec88419712e14cf437e21447.png)
[CVPR 2021] cylinder3d: cylindrical asymmetric 3D convolution network for LIDAR point cloud segmentation

2022年一级建造师考试什么时候才能报名?
随机推荐
Leetcode question brushing series - sum of majority type
105. Construct binary tree from preorder and inorder traversal sequence (medium binary tree DFS hash table binary tree)
mysql8.0无法给用户授权或提示You are not allowed to create a user with GRANT的问题
Hcip experiment (15)
SQL injection less38 (Stack Injection)
TensorFlow Serving 高性能的机器学习模型服务系统
[Ruiji takeout] day05 package management business development
DOM programming + events
HCIP(14)
Log4j vulnerability elk platform processing method (logstah5.5.1)
(翻译)图技术简明历史
Awk blank line filtering
75. Color classification (medium array double pointer sorting)
CDN working principle
Sword finger offer II 063. replacement word (medium prefix tree string)
Mysql8.0 cannot authorize users or prompt you are not allowed to create a user with grant
Hcip experiment (14)
vuejs中如何实现动态路由切换及路由的缓存
gprs网络指的是什么
近期bug总结