Wechat applet calls native wechat api to realize address selection of provinces and cities
// page/address/edit.js Page({ data:{}, onLoad:function(options){ // The page initialization options are the parameters brought by the page jump }, bingAddressTap:function(){ var that = this; wx.chooseLocation({ success: function(res){ var regex = /^(Beijing | Tianjin | Chongqing | Shanghai | Hong Kong SAR | Macau SAR)/; var REGION_PROVINCE=[]; var addressBean = { REGION_PROVINCE:null, REGION_COUNTRY:null, REGION_CITY:null, ADDRESS:null}; function regexAddressBean(address, addressBean){ regex = /^(.*?[city state]|.*?region|.*?special administrative region)(.*?[city and county])(.*?)$/g; var addxress = regex.exec(address); addressBean.REGION_CITY=addxress[1]; addressBean.REGION_COUNTRY=addxress[2]; addressBean.ADDRESS=addxress[3]+"("+res.name+")"; console.log(addxress); } if(!(REGION_PROVINCE = regex.exec(res.address))){ regex = /^(.*?(province|autonomous region))(.*?)$/; REGION_PROVINCE = regex.exec(res.address); addressBean.REGION_PROVINCE= REGION_PROVINCE[1]; regexAddressBean(REGION_PROVINCE[3],addressBean); } else { addressBean.REGION_PROVINCE= REGION_PROVINCE[1]; regexAddressBean(res.address, addressBean); } that.setData({ADDRESS_1_STR: addressBean.REGION_PROVINCE+" " +addressBean.REGION_CITY+"" +addressBean.REGION_COUNTRY }); that.setData(addressBean); } }) }, onReady:function(){ // page rendering is complete }, onShow:function(){ // page display }, onHide:function(){ // page hide }, onUnload:function(){ // page close } })
The above is the js code
Web section (wxml)
<view class="info"> <text>Shipping address</text> <input bindfocus="bingAddressTap" value="{{REGION_PROVINCE}}{{REGION_CITY}}{{REGION_COUNTRY}}" placeholder="小区/大厦/学校"/> </view> <view class="info"> <text>Detailed address</text> <textarea auto-height="true" placeholder="Detailed address (such as house number, etc.)" value="{{ADDRESS}}"/> </view>
Click the text box next to the shipping address to select it
Preview effect