|
天下雪:今天刚好有个同学在问这个问题,就是如何在有了经纬度之后,如何获取城市信息,刚好看到两个相关文章,就贴出来,给大家参考一下; 作者;tammy1151,来自原文地址 首先需要申请百度地图Geocoding API Geocoding API包括地址解析和逆地址解析功能: 地理编码:即地址解析,由详细到街道的结构化地址得到百度经纬度信息,例如:“北京市海淀区中关村南大街27号”地址解析的结果是“lng:116.31985,lat:39.959836”。同时,地理编码也支持名胜古迹、标志性建筑名称直接解析返回百度经纬度,例如:“百度大厦”地址解析的结果是“lng:116.30815,lat:40.056885” ,通用的POI检索需求,建议使用Place API。 逆地理编码:即逆地址解析,由百度经纬度信息得到结构化地址信息,例如:“lat:31.325152,lng:120.558957”逆地址解析的结果是“江苏省苏州市虎丘区塔园路318号”。 代码: Page({ data:{ city:'' }, onLoad:function(options){ this.loadInfo(); }, loadInfo:function(){ var page=this wx.getLocation({ type: 'wgs84', // 默认为 wgs84 返回 gps 坐标,gcj02 返回可用于 wx.openLocation 的坐标 success: function(res){ // success var longitude=res.longitude var latitude=res.latitude page.loadCity(longitude,latitude) }, fail: function() { // fail }, complete: function() { // complete } }) }, loadCity:function(longitude,latitude){ var page =this wx.request({ url: '您的ak &location='+latitude+','+longitude+'&output=json', data: {}, header:{ 'Content-Type':'application/json' }, success: function(res){ // success console.log(res); var city=res.data.result.addressComponent.city; page.setData({city:city}); }, fail: function() { // fail }, complete: function() { // complete } }) } })
<!--index.wxml--> <view class="container"> {{city}} </view>
二:获取当前位置信息 小程序的api getLocation只能获得经纬度,所以需要将经纬度转换成地区信息发送http请求 ' + res.latitude + ',' + res.longitude + '&output=json&pois=1 可以将经纬度转换成地区信息,完整代码: wx.getLocation({ type: 'wgs84', success: function (res) { wx.request({ url: '' + res.latitude + ',' + res.longitude + '&output=json&pois=1', data: {}, success: function (ops) { console.log(ops) } }) } })
三:两种为对象属性赋值的方式对应config.wxml <view> 阶段一<switch id="config1" checked bindchange="switchChange"/> </view>
对应config.js data:{ //定义对象configs:{}}
//方式一 switchChange:function(e){ //为对象的某一属性赋值 configs.config1={ }; console.log(e);}
//方式二 switchChange:function(e){ //为对象的某一属性赋值 configs["config1"]={ } console.log(e);}
两种效果等同 但是 方式一的 congfig1取至function(e),是个不定值,因为若数组个数发生改变,相应的congfig1指的便不是同一个 方式二config1取至 congfig.wxml中事件switchChange,id为config1的属性 |