微信获取位置

5 篇文章 0 订阅
2 篇文章 0 订阅

var config = {

            debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。

            appId: ‘’, // 必填,公众号的唯一标识

            timestamp: ‘’,

            nonceStr: ‘’,

            signature: ‘’,

            jsApiList: [‘getLocation'] // 必填,需要使用的JS接口列表

        }

        wx.config(config);

 

例如:高德地图:

https://webapi.amap.com/maps?v=1.4.5&key=12f5b67ecff50a15cd3a51c65d2ef4c3

 

 

通过GET获取高德地图接口的返回值

function search(key, keyword) {

    $.ajax({

        type: "get",

        data: {

            key: key,

            keywords: keyword

        },

        dataType: "json",

        url: "http://restapi.amap.com/v3/assistant/inputtips",

        success: function (result) {

            if (result.status === "1" && result.tips.length > 0) {

                done(result.tips);

            }

        },

        error: function (err) {

            console.log(err);

        }

    });

}

 

设置坐标 返回坐标经纬度

function setMarker() {

    const p = arguments[0], m = arguments[1];

    coordinate = p;

    m.setCenter(p);

    m.setZoom(19);

    if (!marker) {

        marker = new AMap.Marker({

            content: '<div class="marker active"></div>',

            position: p

        });

    } else {

        marker.setPosition(p);

    }

    coordinate = p;

    marker.setMap(m);

}

 

赋值唯一公众号的唯一标识

const key = "";

const map = new AMap.Map('map');

var marker = null, coordinate = [];

 

获取定位并返回

$(document).ready(function () {

    $("#button ").on("click", function () {

        event.preventDefault();

        const keyword = $("#keyword").val().trim();

        if (keyword) {

            search(key, keyword);

        } else {

            console.log("没有关键字");

        }

    });

    $("#search ").on("click", ".item", function () {

        // 选择的那个位置坐标

        const loc = $(this).attr("data-location"), pos = loc.split(",");

        // 设置坐标 返回坐标经纬度

        setMarker(pos, map);

        clear();

    });

});

wx.ready(function () {

    wx.checkJsApi({

        jsApiList: ['getLocation', 'scanQRCode'],// 需要检测的JS接口列表,所有JS接口列表见附录2,

        success: function (res) {

            // 以键值对的形式返回,可用的api值true,不可用为false

            // 如:{"checkResult":{"chooseImage":true},"errMsg":"checkJsApi:ok"}

        }

    });

 

    wx.getLocation({

        type: 'gcj02'// 高德地图类型             

        success: function (res) {

            var latitude = res.latitude; // 纬度,浮点数,范围为90 ~ -90

            var longitude = res.longitude; // 经度,浮点数,范围为180 ~ -180。

            var speed = res.speed; // 速度,以米/每秒计

            var accuracy = res.accuracy; // 位置精度

            setMarker([longitude, latitude], map);

        },

        cancel: function (res) {

            weui.alert('用户拒绝授权获取地理位置');

        }

    });

});

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值