当前位置:网站首页>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>`)
})
})边栏推荐
- What does GPRS network mean
- tutorial/detailed_ workflow. Ipynb quantitative finance qlib Library
- Ruiji takeout project - development of business development function Day2
- PaddleNLP基于ERNIR3.0文本分类以CAIL2018-SMALL数据集罪名预测任务为例【多标签】
- Less than a year after its establishment! MIT derivative quantum computing company completed financing of US $9million
- 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
- Establishment of Ruiji takeout development environment
- [CS231N]Lecture_2:Image Classification pipelin
- SQL注入 Less34(POST型宽字节注入+布尔盲注)
- 网易云信 2022Q2 产品补给站,快来获取你的产品补给计划吧!
猜你喜欢

flask之蓝图 补充openpyxl

What are the main functions and uses of LCR tester

When can I sign up for the 2022 class I constructor examination?

Att & CK preliminary understanding
How do we do full link grayscale on the database?

Hcip experiment (15)

Hcip experiment (14)

CMD common commands

Less than a year after its establishment! MIT derivative quantum computing company completed financing of US $9million

成立不到一年!MIT衍生量子计算公司完成900万美元融资
随机推荐
HCIP(8)
imx6q gpio复用
Hcip experiment (15)
Ngrok intranet penetration
静态路由和缺省路由实验
想要快速成长,先要经历重大打击!
Sword finger offer II 056. Sum of two nodes in a binary search tree (simple binary search tree DFS hash table double pointer iterator)
【转载】token令牌在登录场景使用
mysql8.0无法给用户授权或提示You are not allowed to create a user with GRANT的问题
Idea generate class diagram plug-in UML (super detailed)
There will be a black line on the border when the button in the wechat applet is clicked
Sword finger offer II 064. magic Dictionary (medium dictionary tree string design)
How do we do full link grayscale on the database?
AWK空行过滤
示波器发展史中的变化
Alibaba cloud CDN practice
网易云信 2022Q2 产品补给站,快来获取你的产品补给计划吧!
HCIP(11)
Container configuration starts redis cluster single machine 6 nodes 3 Master 3 slave
Sword finger offer II 054. Sum of all values greater than or equal to nodes (medium binary search tree DFS)