I recently encountered a requirement to obtain the current user’s location in the public account’s Web project. After using SDK to find the current latitude and longitude, I could not see the address. So we use Tencent map API reverse address resolution can complete the function we want

Remember to check WebServiceAPI in Tencent API

As a learning record

The code is as follows:

wx.getLocation({
    type: 'wgs84'.// The default GPS coordinates are wGS84. If you want to return the Mars coordinates directly for openLocation, pass 'gcj02'.
    success: function (res) {
        var lng = res.longitude;
        var lat = res.latitude;
        $('#Longitude').val(lng);
        $('#Latitude').val(lat);
        console.log(lat + "," + lng);
        $.ajax({
            type: 'get'.url: 'http://apis.map.qq.com/ws/geocoder/v1'.dataType: 'jsonp'.data: {
                key: "Your key value".// Develop the key
                location: lat + "," + lng,// Position coordinates
                get_poi: "1".// Whether to return the list of surrounding POIS: 1. 0 does not return (default)
                coord_type: "1".// Enter the locations coordinates type,1 GPS coordinates
                parameter: { "scene_type": "tohome"."poi_num": 20 },// Add control function
                output: "jsonp"
            },
            success: function (data, textStatus) {
                if (data.status == 0) {
                    var address = data.result.formatted_addresses.recommend;
                    alert(data.result.address + address);
                } else {
                    alert("Location error, please contact administrator!")}},error: function () {
                alert("Location error, please contact administrator!")}}); }});Copy the code

Welcome to qq group communication: 704028989