1. Register an account on Baidu Map Open Platform and log inWebsite: 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:
|
<<: Reasons and solutions for MySQL failing to create foreign keys
>>: How to prevent website content from being included in search engines
Why did I use this? It all started with the makin...
We all know that we can use the mkdir command to ...
This article is a self-written imitation of the X...
1. Problem description The storage installed in t...
I recently watched Rich Harris's <Rethinki...
Table of contents 1. Import files 2. HTML page 3....
Table of contents Custom Vite plugins Using vite-...
I am currently developing a new app project. This...
Table of contents Preface 1. Introduction to Axio...
Table of contents Concurrent scenarios Write-Writ...
1. Use the df command to view the overall disk us...
Preface Excel is powerful and widely used. With t...
<br />Original text: http://www.mikkolee.com...
To connect Node-red to the database (mysql), you ...
The plugin is installed in the Firefox browser. T...