Home >Web Front-end >JS Tutorial >Detailed explanation of vue-baidu-map's automatic positioning method after entering the page
This time I will bring you a detailed explanation of the automatic positioning method of vue-baidu-map after entering the page. What are the precautions for vue-baidu-map to realize automatic positioning after entering the page. The following is a practical case. , let’s take a look.
Written before: I am just a front-end novice, and what is mentioned in the article may have shortcomings, so I only provide a reference. If there are any imperfections, you are welcome to point them out! ,Hope it helps you!
Okay, let’s get to the point. In fact, I have been troubled by this problem before, and I checked online but couldn't find a solution. It wasn't until today that I took the liberty to raise an issue with the boss on GitHub, and it woke me up. In fact, it was because I was too eager for quick success and did not carefully read the reference documentation provided by vue-baidu-map. Or maybe I read it and then forgot about it!
First of all, let’s be clear (original words from the document): Since the Baidu Map JS API only has one loading method: JSONP, the rendering of the BaiduMap component and all its sub-components can only be asynchronous. Therefore, please use the component's ready event to execute code that can only be executed after the map API is loaded. Do not try to call the BMap class in vue's own lifecycle, let alone modify the model layer at these times.
Incorrect usage
I have tried it, and the above method seems to be feasible and the effect can be seen, but we'd better use the correct method provided by the author!
Correct usage
This method is recommended! The following method to solve the problem of automatic positioning when entering the page is also here.
The following is my writing method, for reference only. Please point out if there are any shortcomings, I am just a novice, haha!
Template:
d477f9ce7bf77f53fbcf36bec1b69b7a 2671d65c4ea3ef15465f6e65e94ab9db 82115b0f4d86e34650abf6bc49088284 54ab1b5c2eb4cb7591b51df9d46be330 26c0e5f30144c9d88ee0b242b91c55a4 0f99f12c2ed8dc35b86bb3dc59b7b907 9904e77b51de944eae2ebf1cdd4ebf4b 974eebcdcfab19e720ba2224c076d1a3 21c97d3a051048b8e55e3c8f199a54b2
JS implementation:
<script> export default { data () { return { // 省略一部分 autoLocationPoint: {lng: 0, lat: 0}, initLocation: false, } }, methods: { handler ({BMap, map}) { let _this = this; // 设置一个临时变量指向vue实例,因为在百度地图回调里使用this,指向的不是vue实例; var geolocation = new BMap.Geolocation(); geolocation.getCurrentPosition(function(r){ console.log(r); _this.center = {lng: r.longitude, lat: r.latitude}; // 设置center属性值 _this.autoLocationPoint = {lng: r.longitude, lat: r.latitude}; // 自定义覆盖物 _this.initLocation = true; console.log('center:', _this.center) // 如果这里直接使用this是不行的 },{enableHighAccuracy: true}) // 下面注释是百度地图API官方实现方法,因为我使用自定义图标覆盖物,所以没有使用这种方法! // 如使用以下这种方法,那么我Template里所写的自定义定位图标代码是不需要的 // var geolocation = new BMap.Geolocation(); // geolocation.getCurrentPosition(function(r){ // if(this.getStatus() == BMAP_STATUS_SUCCESS){ // var mk = new BMap.Marker(r.point); // map.addOverlay(mk); // map.panTo(r.point); // alert('您的位置:'+r.point.lng+','+r.point.lat); // } // else { // alert('failed'+this.getStatus()); // } // },{enableHighAccuracy: true}) } } } </script>
If you copy the code directly, please note that you must copy selectively because I have not posted all the code. , copying directly to your project will cause problems! However, this code is relatively simple and can be understood with a little effort, haha!
I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!
Recommended reading:
Detailed explanation of the steps to use the vue pop-up message component
Detailed explanation of the implementation steps of angular routing highlighting
The above is the detailed content of Detailed explanation of vue-baidu-map's automatic positioning method after entering the page. For more information, please follow other related articles on the PHP Chinese website!