uniapp中小程序获取坐标

142 阅读1分钟

startLocationUpdate 与 onLocationChange 配合使用

startLocationUpdateBackground 与 onLocationChange 配合使用

stopLocationUpdate关闭监听实时位置变化,前后台都停止消息接收

配置文件:manifest.json

"mp-weixin": {
  "requiredPrivateInfos" : [
    "chooseAddress",
    "chooseLocation",
    "choosePoi",
    "getLocation",
    "onLocationChange",
    "startLocationUpdateBackground",
    "startLocationUpdate"
  ],
  "requiredBackgroundModes" : ["location"]
}

小程序后台:开通对应接口

<script>
  export default {
    onHide() {
      console.log("onHide ");
      uni.stopLocationUpdate();
    },
    onShow() {
      this.getUserLocation();
    },
    onLoad() {
      uni.onLocationChange(()=>{
        console.log(1234 + ":" + JSON.stringify(res));
      });
    },

    methods: {
      getUserLocation() {
        var that = this;
        uni.startLocationUpdateBackground({
          type: "wgs84",
          success: () => {},
          fail: (err) => {
            console.log("startLocationUpdateBackground", JSON.stringify(err));
          },
        });
        // uni.startLocationUpdate({
        //   type: "wgs84",
        //   success: () => {
        //     console.log("·····················");
        //   },
        // });
      },
    },
  };
</script>