WeChat Mini Program Location Permission and uniapp uni.getLocation Implementation
Location Permission Setup for WeChat Mini Programs
Access the WeChat Public Platform portal.
-
Confirm that the mini-program category has been completed. Navigate to the left sidebar, select "Settings" > "Basic Settings" > "Edit".
-
Under Development Management > Interface Settings > Geolocation, locate the permission toggle: Click the enable button for the required interface (e.g., wx.getLocation). Note that this option will not appear unless the previous step is completed.
-
Fill out the application form: Submit the necessary documentation including screenshots from a production environment rather than a test setup. Include a brief description of usage, such as:
The application requires location verification for employee check-in functionality to ensure attendance falls within designated boundaries, supporting remote work operations.
After submission, approval typically takes less than five minutes. Refresh the page manually to verify status.
Using uniapp's uni.getLocation API
Invoke the location service during relevant user interactions, such as when a check-in button is pressed:
uni.getLocation({
type: 'wgs84',
success: function(res) {
console.log("Latitude and longitude retrieved successfully");
const lat = res.latitude;
const lng = res.longitude;
console.log(lat);
console.log(lng);
},
fail: function(res) {
console.log("Failed to retrieve location");
console.log(res);
}
});
Configure the manifest.json file within your uniapp project under the mp-weixin section:
{
"requiredPrivateInfos": [
"chooseLocation", "getLocation"
],
"permission": {
"scope.userLocation": {
"desc": "Your location is used to verify check-in validity"
}
}
}
Both requiredPrivateInfos and permission entries are essential for proper functionality.