web123456

uni-app (WeChat mini program) get the current location

  • uni.getSetting({
  • success: (res) => {
  • if (!res.authSetting['']) {
  • console.log("permission denied");
  • //Pull the authorization window
  • uni.authorize({
  • scope: "",
  • success: () => {
  • uni.getLocation({
  • type: 'gcj02',
  • success: (res) => {
  • console.log("Get the interface at the current location", res);
  • console.log('The longitude of the current location:' + res.longitude);
  • console.log('The latitude of the current position:' + res.latitude);
  • qqmapsdk.reverseGeocoder({
  • location: {
  • latitude: res.latitude,
  • longitude: res.longitude
  • },
  • success: (res) => {
  • console.log("Location:", res)
  • // Call the punch-in interface
  • let obj = {
  • address: res.result
  • .formatted_addresses
  • .standard_address,
  • longitude_x: res
  • .result
  • .location.lng,
  • latitude_y: res
  • .result
  • .location.lat
  • }
  • sign_in(obj, 'POST').then(
  • res => {
  • if (res.code ==
  • 200) {
  • uni.showToast({
  • title: "Check in successfully!",
  • icon: "success"
  • })
  • }
  • })
  • }
  • })
  • }
  • });
  • },
  • fail: (err) => {
  • uni.showModal({
  • title: 'hint',
  • content: 'If you click without authorization, the location function will not be used',
  • cancelText: 'No authorization',
  • cancelColor: '#999',
  • confirmText: 'Authorization',
  • confirmColor: '#f94218',
  • success: (res) => {
  • if (res.confirm) {
  • uni.openSetting({
  • success: (res) => {
  • console.log(res
  • .authSetting);
  • }
  • })
  • } else if (res.cancel) {
  • console.log("User clicking without authorization");
  • }
  • }
  • })
  • }
  • })
  • } else {
  • //Get directly if you have permission
  • uni.getLocation({
  • type: 'wgs84',
  • success: (res) => {
  • console.log("Get the interface at the current location", res);
  • console.log('The longitude of the current location:' + res.longitude);
  • console.log('The latitude of the current position:' + res.latitude);
  • qqmapsdk.reverseGeocoder({
  • location: {
  • latitude: res.latitude,
  • longitude: res.longitude
  • },
  • success: (res) => {
  • console.log("Location:", res)
  • let obj = {
  • address: res.result.formatted_addresses
  • .standard_address,
  • longitude_x: res.result.location.lng,
  • latitude_y: res.result.location.lat
  • }
  • sign_in(obj, 'POST').then(res => {
  • if (res.code == 200) {
  • uni.showToast({
  • title: "Check in successfully!",
  • icon: "success"
  • })
  • }
  • })
  • }
  • })
  • }
  • });
  • }
  • }
  • })