How does WeChat Mini Programs obtain the longitude and latitude of the current location through API
This article introduces the relevant knowledge of "how WeChat Mini Programs obtains the longitude and latitude of the current location through API". In the operation of actual cases, many people will encounter such a dilemma. Next, let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Effect picture:
1. Wxml file
2. Js file
Var coors;Page ({data: {polyline: [], markers: [],}, onReady: function () {this.mapContext = wx.createMapContext ("map", this);}, onLoad: function (options) {/ / get the current map, set latitude and longitude, the coordinates passed, and the coordinate address where the user placed the order. Console.log (options); wx.getLocation ({success: (res) = > {this.setData ({latitude: options.latitude, longitude: options.longitude}); this.getCenterLocation (res) Wx.request ({url: 'https://apis.map.qq.com/ws/direction/v1/driving/?from=' + this.data.markers [0] .latitude +','+ this.data.markers [0] .longitude +'& to=' + this.data.markers [1] .latitude +','+ this.data.markers [1] .longitude +'& output=json&callback=cb&key=PD5BZ-K2VRO-CPEWZ-SOBAC-4KCDT-KAFLF') Success: (res) = > {coors = res.data.result.routes [0] .Polyline for (var I = 2 I < coors.length; iTunes +) {coors [I] = coors [I-2] + coors [I] / 1000000} console.log (coors) / / dashed var b = []; for (var I = 0; I < coors.length I = I + 2) {b [I / 2] = {latitude: coors [I], longitude: coors [I + 1]} Console.log (b [I / 2])} this.setData ({polyline: [{points: B, color: "# 00ae20", width: 4, dottedLine: false}) })})) }, / / two coordinates: an order address is issued, an engineer receives the order address, and then the engineer's coordinate location is constantly updated. GetCenterLocation: function (res) {this.setData ({markers: [{iconPath: "/ resources/center.png", id: 0, latitude: res.latitude, longitude: res.longitude, width: 30, height: 30, alpha: 0.8, callout: {content: "my place" Color: "# ffffff", fontSize: 10, borderRadius: 10, bgColor: "# 6e707c", padding: 5, display: "ALWAYS"}}, {iconPath: "/ resources/user.png", id: 1, latitude: res.latitude + 0.1 Longitude: res.longitude + 0.1, width: 30, height: 30, alpha: 0.8, callout: {content: "engineer", color: "# ffffff", fontSize: 10, borderRadius: 10, bgColor: "# 6e707c", padding: 5 Display: "ALWAYS"}}],}) },)
3. Wxss file
.mapHeight {display: flex; flex-direction: column;} .map {flex: 1; height: 100vh; width: 100%;} "how WeChat Mini Programs obtains the longitude and latitude of his current location through API". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!