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

JavaScript microtasks and macrotasks explained

Preface: js is a single-threaded language, so it ...

Input file custom button beautification (demo)

I have written such an article before, but I used...

How to use CocosCreator object pool

Table of contents Preface: Specific operations St...

Detailed discussion of the character order of mysql order by in (recommended)

//MySQL statement SELECT * FROM `MyTable` WHERE `...

Graphical explanation of the solutions for front-end processing of small icons

Preface Before starting this article, let’s do a ...

CentOS installation mysql5.7 detailed tutorial

This article shares the detailed steps of install...

In-depth understanding of Worker threads in Node.js

Table of contents Overview The history of CPU-bou...

Specific use of lazy loading and preloading in js

Delayed loading (lazy loading) and preloading are...

Solve the problem of Mac Docker x509 certificate

question Recently I needed to log in to a private...

Detailed explanation of the functions of each port of Tomcat

From the tomcat configuration file, we can see th...

Example of how to create a local user in mysql and grant database permissions

Preface When you install MySQL, you usually creat...

JavaScript to implement image preloading and lazy loading

This article shares the specific code for impleme...

How to show or hide common icons on the desktop in Windows Server 2012

Windows Server 2012 and Windows Server 2008 diffe...