实现步骤

1. 获取当前经纬度

2. 调用腾讯(百度、高德)地图对应的请求地址,一般都会有独一的key, 譬如

腾讯地图调用地址:

https://apis.map.qq.com/ws/geocoder/v1/?location=${latitude},${longitude}&key=${keys}

百度地图调用地址:

https://apis.map.baidu.com/ws/geocoder/v2/?location=${latitude},${longitude}&key=${keys}

  

wxml

<view>{{district}}</view>

<picker mode="region" bindchange="bindRegionChange" value="{{region}}" custom-item="{{customItem}}">
<view class="picker">
当前选择:{{region[0]}} - {{region[1]}} - {{region[2]}}
</view>
</picker>

js

let keys = 'SGXBZ-6X3K6-NYLSF-MALZD-QC6PK-BABOS';
let _page, _this; Page({ /**
* 页面的初始数据
*/
data: {
customItem: '全部'
}, /**
* 生命周期函数--监听页面加载
*/
onLoad: function(options) {
_this = this;
wx.getLocation({
success: function(res) {
_this.getDistrict(res.latitude, res.longitude)
},
})
}, getDistrict(latitude, longitude) {
_page = this;
wx.request({
url: `https://apis.map.qq.com/ws/geocoder/v1/?location=${latitude},${longitude}&key=${keys}`,
header: {
'Content-Type': 'application/json'
},
success: function(res) {
console.log(res.data.result.address_component.district, res.data.result) // 省
let province = res.data.result.address_component.province;
// 市
let city = res.data.result.address_component.city;
// 区
let district = res.data.result.address_component.district; _page.setData({
district: res.data.result.address_component.district,
region: [province,city,district]
})
}
})
}, bindRegionChange (e) {
console.log('picker发送选择改变,携带值为', e.detail.value)
this.setData({
region: e.detail.value
})
}
})

  

微信小程序 - 获取所在位置(省、市、区)的更多相关文章

  1. 微信小程序-获取当前位置和城市名

    微信小程序-获取当前城市位置 1, 获取当前地理位置,首先要拿到用户的授权wx.openSetting: 2,微信的getLocation接口,获取当前用户的地理位置(微信返回的是经纬度,速度等参数) ...

  2. 关于微信小程序获取当前位置信息

    小程序开发---获取当前位置信息 一.获取用户地理位置信息 1.配置app.json文件 { "pages": ["pages/index/index"], & ...

  3. 微信小程序获取当前位置

    详细参数说明请看小程序api文档:https://developers.weixin.qq.com/miniprogram/dev/api/wx.openLocation.html wx.getLoc ...

  4. 微信小程序-获取当前位置

    在 app.json 里面增加 permission 属性配置(小游戏需在game.json中配置): "permission": { "scope.userLocati ...

  5. 微信小程序-获取当前城市位置及再次授权地理位置

    微信小程序-获取当前城市位置 1. 获取当前地理位置,可通过wx.getLocation接口,返回经纬度.速度等信息; 注意---它的默认工作机制: 首次进入页面,调用该api,返回用户授权结果,并保 ...

  6. 微信小程序-获取经纬度

    微信小程序-获取经纬度 最近公司新功能 要求在外的市场人员 发送位置信息回来. 用的还是微信小程序开发.... 微信小程序 提供一个接口 getLocation 这个接口反回来的位置 相对实际位置 相 ...

  7. [微信小程序] 微信小程序获取用户定位信息并加载对应城市信息,wx.getLocation,腾讯地图小程序api,微信小程序经纬度逆解析地理信息

    因为需要在小程序加个定位并加载对应城市信息 然而小程序自带api目前只能获取经纬度不能逆解析,虽然自己解析方式,但是同时也要调用地图,难道用户每次进小程序还要强行打开地图选择地址才定位吗?多麻烦也不利 ...

  8. 微信小程序获取地理位置授权

    微信小程序获取地理位置授权,首先需要在app.json中添加配置: "permission": { "scope.userLocation": { " ...

  9. (八)微信小程序---获取定位信息chooseLocation

    微信小程序---获取定位信息  chooseLocation wxml <view bindtap="getlocalPath">{{localPath}}</v ...

随机推荐

  1. UVALive 6257 Chemist's vows

    #include<iostream> #include<string.h> #include<stdio.h> #include<ctype.h> #i ...

  2. BZOJ 1022 [SHOI2008]小约翰的游戏John AntiNim游戏

    1022: [SHOI2008]小约翰的游戏John Time Limit: 1 Sec  Memory Limit: 162 MBSubmit: 1475  Solved: 932[Submit][ ...

  3. Codeforces Round #294 (Div. 2)D - A and B and Interesting Substrings 字符串

    D. A and B and Interesting Substrings time limit per test 2 seconds memory limit per test 256 megaby ...

  4. CodeForces 81D.Polycarp's Picture Gallery 乱搞

    D. Polycarp's Picture Gallery time limit per test 2 seconds memory limit per test 256 megabytes inpu ...

  5. HDU 3535 AreYouBusy 经典混合背包

    AreYouBusy Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total Su ...

  6. ASP.NET Web Api 实现数据的分页

    前言 这篇文章我们将使用不同的方式实现手动分页(关于高端大气上档次的OData本文暂不涉及,但有可能会在系列的后期介绍,还没确定...),对于分页的结果,我们将采用2种不同的方式响应给客户端(1.将分 ...

  7. easyui combobox开启搜索自动完成功能

    combo.json [{ "id":-1, "text":" ", "spell":"" },{ ...

  8. 淘宝--印风 专注于MySQL内核代码

    http://blog.csdn.net/zhaiwx1987/article/details/6113472?utm_source=jiancool

  9. MySQL运维开发相关的所有工具

    http://www.ruzuojun.com/topic/592.html   Percona Toolkit 安装使用 http://cenalulu.github.io/mysql/mysql- ...

  10. [Asp.net MVC]Html.AntiForgeryToken()

    CSRF(Cross-site request forgery)跨站请求伪造,也被称为“One Click Attack”或者Session Riding,通常缩写为CSRF或者XSRF,是一种对网站 ...