微信小程序 地图定位简单实例


Posted in Javascript onOctober 14, 2016

微信小程序开发地图定位。

微信小程序 刚刚公布没多久,自己学习一下内容,以便以后的开发,想落后别人,这里做了一个简单的小程序示例,大家可以参考下

微信小程序 地图定位简单实例

要求要完成的功能:

1.要完成的要点是城市定位。

2.就是切换城市。

首页我们先参照微信小程序开放的官方文档找到:

微信小程序 地图定位简单实例

在这里我们可以找到”当前位置经纬度“

getLocation: function ()
{
var that = this wx.getLocation(

{


success: function (res) {




 console.log(res)


 that.setData({



 hasLocation: true,



 location: formatLocation(res.longitude, res.latitude)//这里是获取经纬度


})


}

})
},

//将经纬度转换成城市名和街道地址,参见百度地图接口文档:http://developer.baidu.com/map/index.php?title=webapi/guide/webservice-geocoding

onLoad: function (options) {
console.log('onLoad')

var that = this;

wx.getLocation({


success: function (res) {



wx.request({




url: 'http://api.map.baidu.com/geocoder/v2/?ak=btsVVWf0TM1zUBEbzFz6QqWF&callback=renderReverse&location=' + res.latitude + ',' + res.longitude + '&output=json&pois=1', data: { },




header: { 'Content-Type': 'application/json' },




success: function(ops) {





console.log(ops.data)




}


})

// console.log(res)

// that.setData({

// hasLocation: true,

// location: formatLocation(res.longitude, res.latitude)

// })

}
})
}

这里用到微信小程序的文档里面有发起的是https请求

 微信小程序 地图定位简单实例

这里面有案例我就不多讲解:

上面的代码打印出来的数据是:

renderReverse&&renderReverse(
{"status":0,
"result":
{"location":{"lng":114.05786799999997,"lat":22.543098999645019},
"formatted_address":"广东省深圳市福田区福华一路138-5",
"business":"购物公园,新洲,香蜜湖",
"addressComponent":{"country":"中国","country_code":0,"province":"广东省","city":"深圳市","district":"福田区","adcode":"440304","street":"福华一路","street_number":"138-5","direction":"附近","distance":"18"},"pois":[{"addr":"深圳市福田区福华一路138号","cp":" ","direction":"北","distance":"51","name":"深圳国际商会大厦","poiType":"房地产","point":{"x":114.05776971981998,"y":22.54267931627966},"tag":"房地产;写字楼","tel":"","uid":"9ed8fd9034cebefaeb12e42c","zip":""},{"addr":"福华一路98号","cp":" ","direction":"南","distance":"60","name":"卓越大厦","poiType":"房地产","point":{"x":114.05777870287507,"y":22.543597255274773},"tag":"房地产;写字楼","tel":"","uid":"c7fb04bd8fb44d68fb0cad85","zip":""},{"addr":"深圳市福田区","cp":" ","direction":"西北","distance":"236","name":"购物公园","poiType":"购物","point":{"x":114.05972802583108,"y":22.54214523984097},"tag":"购物;购物中心","tel":"","uid":"0e082dd9db526730aecb66f9","zip":""},{"addr":"福华一路南侧","cp":" ","direction":"西北","distance":"123","name":"投行大厦","poiType":"房地产","point":{"x":114.05829972007068,"y":22.54214523984097},"tag":"房地产;写字楼","tel":"","uid":"b3a40a67bedbe7782fb17ea0","zip":""},{"addr":"深圳福田区国际商会大厦A座","cp":" ","direction":"东北","distance":"77","name":"深圳国际商会大厦A座","poiType":"房地产","point":{"x":114.05750022816707,"y":22.54255414230188},"tag":"房地产;写字楼","tel":"","uid":"108ed554a3f5358229fc4892","zip":""},{"addr":"福华一路88号","cp":" ","direction":"西","distance":"131","name":"中心商务大厦","poiType":"房地产","point":{"x":114.05899141531315,"y":22.54275442061121},"tag":"房地产;写字楼","tel":"","uid":"cac5fc76d0304d8e2db96d8b","zip":""},{"addr":"深圳市福田区福华一路88号中心商务大厦首层","cp":" ","direction":"西","distance":"134","name":"招商银行(中央商务支行)","poiType":"金融","point":{"x":114.05900039836824,"y":22.542704351061439},"tag":"金融;银行","tel":"","uid":"c7fb04bd3d531f6bfa0cadef","zip":""},{"addr":"深圳福田中心区福华一路28号(投资大厦旁)","cp":" ","direction":"西","distance":"229","name":"深圳马哥孛罗好日子酒店","poiType":"酒店","point":{"x":114.05991666998811,"y":22.54288793932078},"tag":"酒店;星级酒店","tel":"","uid":"0523a14106ceb804b23c8142","zip":""},{"addr":"福华一路208号购物公园B1层","cp":" ","direction":"西北","distance":"234","name":"永旺超市(购物公园店)","poiType":"购物","point":{"x":114.05971904277598,"y":22.542170274720726},"tag":"购物;超市","tel":"","uid":"9884a864bb2c032af8dc85d1","zip":""},{"addr":"深圳市福田区深南大道4103号兴业银行大厦17-18层","cp":" ","direction":"西南","distance":"158","name":"深圳市公证处(一号路)","poiType":"政府机构","point":{"x":114.05857819477869,"y":22.54424815372944},"tag":"政府机构;公检法机构","tel":"","uid":"765bf8daf4efd08cb45f1ec0","zip":""}],"poiRegions":[],"sematic_description":"深圳国际商会大厦北51米","cityCode":340}})

你找到city传进去就可以了,

第二步切换城市:

在次找到我们的微信小程序的开发文档里面有

picker

滚动选择器,现支持三种选择器,通过mode来区分,分别是普通选择器,时间选择器,日期选择器,默认是普通选择器。

普通选择器:mode = selector

属性名 类型 默认值 说明
range Array [] mode为 selector 时,range 有效
value Number 0 mode为 selector 时,是数字,表示选择了 range 中的第几个,从0开始。
bindchange EventHandle   value改变时触发change事件,event.detail = {value: value}

时间选择器:mode = time

属性名 类型 默认值 说明
value String   表示选中的时间,格式为"hh:mm"
start String   表示有效时间范围的开始,字符串格式为"hh:mm"
end String   表示有效时间范围的结束,字符串格式为"hh:mm"
bindchange EventHandle   value改变时触发change事件,event.detail = {value: value}

日期选择器:mode = date

属性名 类型 默认值 说明
value String 0 表示选中的日期,格式为"yyyy-MM-dd"
start String   表示有效日期范围的开始,字符串格式为"yyyy-MM-dd"
end String   表示有效日期范围的结束,字符串格式为"yyyy-MM-dd"
fields String day 有效值year,month,day,表示选择器的粒度
bindchange EventHandle   value改变时触发change事件,event.detail = {value: value}

 

注意:开发工具暂时只支持mode = selector。

示例代码:

<view class="section">
 <view class="section__title">地区选择器</view>
 <picker bindchange="bindPickerChange" value="{{index}}" range="{{array}}">
  <view class="picker">
   当前选择:{{array[index]}}
  </view>
 </picker>
</view>
<view class="section">
 <view class="section__title">时间选择器</view>
 <picker mode="time" value="{{time}}" start="09:01" end="21:01" bindchange="bindTimeChange">
  <view class="picker">
   当前选择: {{time}}
  </view>
 </picker>
</view>

<view class="section">
 <view class="section__title">日期选择器</view>
 <picker mode="date" value="{{date}}" start="2015-09-01" end="2017-09-01" bindchange="bindDateChange">
  <view class="picker">
   当前选择: {{date}}
  </view>
 </picker>
</view>
Page({
 data: {
  array: ['美国', '中国', '巴西', '日本'],
  index: 0,
  date: '2016-09-01',
  time: '12:01'
 },
 bindPickerChange: function(e) {
  console.log('picker发送选择改变,携带值为', e.detail.value)
  this.setData({
   index: e.detail.value
  })
 },
 bindDateChange: function(e) {
  this.setData({
   date: e.detail.value
  })
 },
 bindTimeChange: function(e) {
  this.setData({
   time: e.detail.value
  })
 }
})

微信小程序 地图定位简单实例

<view class="fl">
<text wx:if="{{ifture}}">{{cityname}}</text>

<text wx:else> {{array[index]}} </text>

<!--<view class="add-address"></view>-->

<picker bindchange="bindPickerChange" value="{{index}}" range="{{array}}">

<view class="add-address">

切换城市

</view>

</picker>
</view >

 感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

Javascript 相关文章推荐
jquery阻止冒泡事件使用模拟事件
Sep 06 Javascript
JS滚轮事件onmousewheel使用介绍
Nov 01 Javascript
jQuery点击自身以外地方关闭弹出层的简单实例
Dec 24 Javascript
express的中间件basicAuth详解
Dec 04 Javascript
jQuery对象与DOM对象转换方法详解
May 10 Javascript
JS批量替换内容中关键词为超链接
Feb 20 Javascript
jQuery中绑定事件bind() on() live() one()的异同
Feb 23 Javascript
javascript 中关于array的常用方法详解
May 05 Javascript
快速搭建React的环境步骤详解
Nov 06 Javascript
微信小程序引入模块中wxml、wxss、js的方法示例
Aug 09 Javascript
JS实现简单省市二级联动
Nov 27 Javascript
axios解决高并发的方法:axios.all()与axios.spread()的操作
Nov 09 Javascript
Bootstrap作品展示站点实战项目2
Oct 14 #Javascript
JS控制页面跳转时未请求要跳转的地址怎么回事
Oct 14 #Javascript
js中json处理总结之JSON.parse
Oct 14 #Javascript
Bootstrap优化站点资源、响应式图片、传送带使用详解3
Oct 14 #Javascript
D3.js封装文本实现自动换行和旋转平移等功能
Oct 14 #Javascript
D3.js实现文本的换行详解
Oct 14 #Javascript
Bootstrap企业网站实战项目4
Oct 14 #Javascript
You might like
让这部DC动画新作刷新你的认知
2020/03/03 欧美动漫
用在PHP里的JS打印函数
2006/10/09 PHP
用PHP实现文件上传二法
2006/10/09 PHP
ajax php 实现写入数据库
2009/09/02 PHP
ThinkPHP3.1新特性之对Ajax的支持更加完善
2014/06/19 PHP
PHP向socket服务器收发数据的方法
2015/01/24 PHP
浅谈htmlentities 、htmlspecialchars、addslashes的使用方法
2016/12/09 PHP
几个比较经典常用的jQuery小技巧
2010/03/01 Javascript
JS获取select的value和text值的简单实例
2014/02/26 Javascript
JavaScript获得指定对象大小的方法
2015/07/01 Javascript
jquery实现简单文字提示效果
2015/12/02 Javascript
Javascript中prototype的使用详解
2016/06/18 Javascript
ES6中Class类的静态方法实例小结
2017/10/28 Javascript
用jquery获取select标签中选中的option值及文本的示例
2018/01/25 jQuery
学习Vue组件实例
2018/04/28 Javascript
Angular2 自定义表单验证器的实现方法
2018/12/14 Javascript
vue中axios请求的封装实例代码
2019/03/23 Javascript
jQuery实现提交表单时不提交隐藏div中input的方法
2019/10/08 jQuery
JQuery常用选择器功能与用法实例分析
2019/12/23 jQuery
详解datagrid使用方法(重要)
2020/11/06 Javascript
使用Python的Twisted框架构建非阻塞下载程序的实例教程
2016/05/25 Python
Python自定义类的数组排序实现代码
2016/08/28 Python
python中装饰器级连的使用方法示例
2017/09/29 Python
Python爬取个人微信朋友信息操作示例
2018/08/03 Python
python 通过文件夹导入包的操作
2020/06/01 Python
python opencv角点检测连线功能的实现代码
2020/11/24 Python
HTML5新增元素如何兼容旧浏览器有哪些方法
2014/05/09 HTML / CSS
中国酒类在线零售网站:酒仙网
2016/08/20 全球购物
实习期自我鉴定
2013/10/11 职场文书
五好党支部事迹材料
2014/02/06 职场文书
北京奥运会主题口号
2014/06/13 职场文书
委托书的样本
2015/01/28 职场文书
律政俏佳人观后感
2015/06/09 职场文书
观后感格式
2015/06/19 职场文书
Mysql实现简易版搜索引擎的示例代码
2021/08/30 MySQL
Nginx使用ngx_http_upstream_module实现负载均衡功能示例
2022/08/05 Servers