zoukankan      html  css  js  c++  java
  • h5 百度获取地址

    vue+unipp(h5)

    获取省市区地址  baduMap.js  异步加载

    export default {
        init: function () {
            const AK = "***";
            const BMap_URL = 'https://api.map.baidu.com/api?v=2.0&ak=' + AK + '&s=1&callback=onBMapCallback';
            return new Promise((resolve, reject) => {
                // 如果已加载直接返回
                if (typeof BMap !== 'undefined') {
                    resolve(BMap);
                    return true;
                }
                // 百度地图异步加载回调处理
                window.onBMapCallback = function () {
                    resolve(BMap);
                };
                let getCurrentCityName = function () {
                    return new Promise(function (resolve, reject) {
                        let myCity = new BMap.LocalCity()
                        myCity.get(function (result) {
                            resolve(result.name)
                        })
                    })
                }
                // 插入script脚本
                let scriptNode = document.createElement("script");
                scriptNode.setAttribute("type", "text/javascript");
                scriptNode.setAttribute("src", BMap_URL);
                document.body.appendChild(scriptNode);
            });
        }
    }
    

      引用 

    import map from "../../utils/baduMap.js";
    
    
      mounted() {
        this.getCity();
      },
    
       // 百度获取地区
        getCity() {
          // if(!map){
          //   return;
          // }
          map.init().then((BMap) => {
            const locationCity = new BMap.Geolocation();
            var that = this;
            locationCity.getCurrentPosition(
              function getinfo(options) {
                let city = options.address.city; //此处拿到位置相关信息
                that.LocationCity = city;
                that.region = options.address.province + "," + options.address.city;
                if (that.region === ",") {
                  that.region = "";
                }
                
                console.log( that.region)
              },
              function (e) {
                that.LocationCity = "定位失败";
                uni.showToast({
                  title: "定位失败!请检查定位权限并关闭重开",
                  duration: 4000,
                  icon: "none",
                });
              },
              { provider: "baidu" }
            );
          });
        },
    

      

  • 相关阅读:
    BZOJ_2661_[BeiJing wc2012]连连看_费用流
    BZOJ_4867_[Ynoi2017]舌尖上的由乃_分块+dfs序
    BZOJ_3238_[Ahoi2013]差异_后缀自动机
    BZOJ_3207_花神的嘲讽计划Ⅰ_哈希+主席树
    [转载]快速幂与矩阵快速幂
    ACM的一点基础知识
    [转载]C++STL—vector的插入与删除
    C++STL—map的使用
    [转载]汇编语言assume伪指令的作用
    [转载]c++中的位运算符
  • 原文地址:https://www.cnblogs.com/FACESCORE/p/13930092.html
Copyright © 2011-2022 走看看