Vue uses Baidu Maps to realize city positioning

Vue uses Baidu Maps to realize city positioning

This article shares the specific code of Vue using Baidu Maps to realize city positioning for your reference. The specific content is as follows

Vue project running environment: Vue 2.0, Vue Cli 3.0

Step 1: Log in to Baidu Map Open Platform and create an application under Console ----> Application Management ----> My Application.

The purpose is to obtain ak

Step 2: In the index.html file in the public folder

Import Baidu Maps and add your ak

Step 3: Create a vue.config.js file in the root directory of the project

If there is a vue.config.js file, just add the following code.

The vue.config.js file is no longer created by default after VueCli 3.0 . When this file is used, we need to create it manually.

module.exports = {
  configureWebpack: {
    externals: {
      'BMap': 'BMap' // Baidu map configuration}
  }
}

Step 4: The next step is to implement the specific code.

Next, two implementation solutions are introduced.

Method 1 (recommended): separately encapsulate the js tool file to obtain the address.

1. Create a new util folder under the src folder, and then create a new getUserLocation.js file in the util folder.

Note: This util folder can store all your own encapsulated tool js files, not just the location-related file getUserLocation.js .

The code and screenshots are as follows:

// Get the current city const getCurrentCityName = function() {
  return new Promise((resolve, reject) => {
    let myCity = new BMap.LocalCity();
    myCity.get((result) => {
      let geoc = new BMap.Geocoder();
      geoc.getLocation(result.center, (rs) => {
        // rs carries all the location information, here we only get the city and the district
        let addComp = rs.addressComponents;
        let result = addComp.city + addComp.district;
        resolve(result);
      });
    }, {enableHighAccuracy: true});
  });
}
export default getCurrentCityName;

Introduce and call the methods encapsulated in the above files in the component

Method 1: Import tool files

The page can display location information through the locationMsg attribute, for example: Fengtai District, Beijing

Method 2: Position directly in the component.

You can use the following code directly in the component to successfully locate it. This method takes longer than method 1.

The code and screenshots are as follows:

mounted() {
    // Get location information this.getCity();
  },
  methods: {
    getCity() {
      const getLocation = new BMap.Geolocation();
      var _this = this;
      getLocation.getCurrentPosition((position) => {
        // position stores all positioning data console.log(position);
        // Here we get the city and province let city = position.address.city;
        let province = position.address.province;
        _this.locationMsg = province + city;
      }, () => {
        _this.locationMsg = 'Positioning failed!';
      }, {provider: 'baidu'});
    }
  }

Code Diagram

Note: The _this here is actually unnecessary, you can use this directly. Before using arrow functions, _this was used. Later, after changing to arrow functions, the code related to _this was not modified. Of course, there is no problem writing according to the above code.

If nothing unexpected happens, you can get the location information through the locationMsg attribute in the component.

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Vue uses Amap to realize city positioning
  • vue+Amap realizes map search and click positioning operation
  • Example code of the positioning and keyword search function of the vue project using Amap (pitfall experience)
  • Vue uses Gaode map to locate points according to coordinates
  • Vue Baidu Map + Detailed Explanation of Positioning

<<:  MySQL select, insert, update batch operation statement code examples

>>:  How to monitor Linux server status

Recommend

MySQL database operations (create, select, delete)

MySQL Create Database After logging into the MySQ...

JavaScript to achieve simple tab bar switching case

This article shares the specific code for JavaScr...

What are Web Slices?

IE8 new feature Web Slices (Web Slices) Microsoft...

How to install and configure WSL on Windows

What is WSL Quoting a passage from Baidu Encyclop...

SSM implements the mysql database account password ciphertext login function

introduction Our company is engaged in the resear...

Troubleshooting MySQL high CPU load issues

High CPU load caused by MySQL This afternoon, I d...

The easiest way to make a program run automatically at startup in Linux

I collected a lot of them, but all ended in failu...

Vue two-choice tab bar switching new approach

Problem Description When we are working on a proj...

The difference between z-index: 0 and z-index: auto in CSS

I've been learning about stacking contexts re...

Detailed explanation of dynamically generated tables using javascript

*Create a page: two input boxes and a button *Cod...

HTML Tutorial: DOCTYPE Abbreviation

When writing HTML code, the first line should be ...

uni-app implements NFC reading function

This article shares the specific code of uni-app ...

A brief discussion on the role of the docker --privileged=true parameter

Around version 0.6, privileged was introduced to ...

How to view the status of remote server files in Linux

As shown below: The test command determines wheth...

JavaScript microtasks and macrotasks explained

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