How to use Baidu Map API in vue project

How to use Baidu Map API in vue project

1. Register an account on Baidu Map Open Platform and log in

Website: http://lbsyun.baidu.com/index.php?title=jspopularGL

2. Select the map version you need:

Personally, 2.0 is enough for me. In fact, I am just lazy and have always used 2.0 without looking at 3.0. But 3.0 should be used in a similar way.

3. Import it into the index.html under the public folder of our vue project and remember to replace your ak (this ak is not the same as other aks)

<script type="text/javascript" src="//api.map.baidu.com/api?v=2.0&ak=你的ak"></script>

4. After that, you can use our Baidu Maps anywhere:

Add a map container to the page component we require:

<div id="map" class="map"></div>

The class here is used to define style operations such as size and layout; the important thing is the id value map.

Generally, we will load our map when the component is loaded, that is, when the page is rendered, so we can add our core code in the mounted stage:

// Baidu Map API function var map = new BMap.Map("map"); // Create a Map instance map.centerAndZoom(new BMap.Point(104.07258, 30.550701), 20); // Initialize the map, set the center point coordinates and map level // Add a map type control map.addControl(
   new BMap.MapTypeControl({
     mapTypes: [BMAP_NORMAL_MAP, BMAP_HYBRID_MAP],
   })
);
map.setCurrentCity("Chengdu"); //Set the city displayed on the map. This item must be set.map.enableScrollWheelZoom(true);

This will display our Baidu map on the page. Remember to add width and height in CSS! !

5. Adding marker points and having a pop-up window effect when clicking on them:

let point = new BMap.Point(104.07258, 30.550501);
// Create a point marker var marker = new BMap.Marker(point);
// Add a point marker to the map map.addOverlay(marker);
// Create information window var opts = {
  width: 200,
  height: 100,
  title: "Popup title",
};
var infoWindow = new BMap.InfoWindow(
  "Popup content",
  opts
);
// Add click event to the marker marker.addEventListener("click", function () {
  map.openInfoWindow(infoWindow, point); // Open the information window });

6. Add text marks to coordinate points:

//Set text mark var opts2 = {
  position: point, // Specify the geographic location of the text annotation offset: new BMap.Size(30, -30), // Set the text offset };
// Create a text annotation object var label = new BMap.Label("Chengdu Shulun Technology Co., Ltd.", opts2);
// Custom text annotation style label.setStyle({
  color: "blue",
  borderRadius: "5px",
  borderColor: "#ccc",
  padding: "10px",
  fontSize: "16px",
  height: "50px",
  lineHeight: "30px",
  fontFamily: "Microsoft YaHei",
});
map.addOverlay(label);

The above is the details of how to use Baidu Map API in Vue project. For more information about using Baidu Map API in Vue project, please pay attention to other related articles on 123WORDPRESS.COM!

You may also be interested in:
  • Detailed explanation of how to call Baidu Map API in Vue project
  • Example of using Baidu Map API in vue-cli scaffolding of vue.js
  • Detailed explanation of two methods of introducing Baidu Map jsApi in vue.js
  • The vue project realizes convenient access to Baidu Map API

<<:  Reasons and solutions for MySQL failing to create foreign keys

>>:  How to prevent website content from being included in search engines

Recommend

Detailed explanation of how to introduce custom fonts (font-face) in CSS

Why did I use this? It all started with the makin...

Briefly understand the two common methods of creating files in Linux terminal

We all know that we can use the mkdir command to ...

JavaScript imitates Xiaomi carousel effect

This article is a self-written imitation of the X...

How to implement Svelte's Defer Transition in Vue

I recently watched Rich Harris's <Rethinki...

js realizes packaging multiple pictures into zip

Table of contents 1. Import files 2. HTML page 3....

Practice of using Vite2+Vue3 to render Markdown documents

Table of contents Custom Vite plugins Using vite-...

Use Vue3+Vant component to implement App search history function (sample code)

I am currently developing a new app project. This...

Axios project with 77.9K GitHub repository: What are the things worth learning?

Table of contents Preface 1. Introduction to Axio...

How is MySQL transaction isolation achieved?

Table of contents Concurrent scenarios Write-Writ...

How to check disk usage in Linux

1. Use the df command to view the overall disk us...

CSS Standard: vertical-align property

<br />Original text: http://www.mikkolee.com...

How to connect to MySQL database using Node-Red

To connect Node-red to the database (mysql), you ...