H5获取位置信息的方法

1.根据IP获取

const getGeo = () => {
  fetch("http://ip-api.com/json")
    .then((response) => response.json())
    .then(async (data) => {
      console.log("data===", data);
      this.getAddress(data?.lon,data?.lat)
    })
    .catch((error) => {
      console.error("Error:", error);
    });
    
};
getGeo();

2.高德地图获取位置信息及天气

(1)高德地图官网添加天气应用获取key和密钥

(2)在vue项目public下的index.html中引入script

  <script type="text/javascript">
    window._AMapSecurityConfig = {
      securityJsCode: 'b085928b165e00483d23b263fafecb13',
    }
  </script>
  <script type="text/javascript" src="https://js.cdn.aliyun.dcloud.net.cn/dev/uni-app/uni.webview.1.5.1.js"></script>
  <script type="text/javascript"
    src="https://webapi.amap.com/maps?v=1.4.15&key=申请的key&plugin=(所需api例如AMap.CitySearch,AMap.Weather)">
  </script>

(3)调用获取当前位置api和天气api

  //获取位置
    getCity() {
      let that = this
      if (window) {
        window.AMap.plugin("AMap.CitySearch", function () {
          var citySearch = new window.AMap.CitySearch();
          citySearch.getLocalCity(function (status, result) {
            if (status === "complete" && result.info === "OK") {
              // 查询成功,result即为当前所在城市信息
              console.log("getCity```````", result);
              that.getWeather(result.city)
            }
          });
        });
      }
    },
    // 获取天气
    getWeather(city) {
      let that = this
      // 加载天气查询插件
      window.AMap.plugin("AMap.Weather", function () {
        // 创建天气查询实例
        let weather = new window.AMap.Weather()
        // 执行实时天气信息查询
        weather.getLive(city, function (err, data) {
          if (data.info == "OK") {
              console.log("getWeather```````", data);
            // that.weatherdata = data
          }
        });
      });
    },

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

爱写程序的小高

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值