小程序获取定位位置

男娘i 2022-10-25 15:25 377阅读 0赞

wxml:

  1. <view class="location">推荐</view>
  2. <button class="getLocation" bindtap="getLocation" size='mini'>定位</button>
  3. <view class="hotCity">热门城市</view>
  4. <button class="btn" size="mini" wx:for="{ {hotCitys}}"
  5. wx:key="index" >{ { item}}</button>

js:

  1. data: {
  2. hotCitys:["北京", "上海", "深圳", "广州", "武汉", "荆州", "荆门"]
  3. },
  4. getLocation:function(){
  5. wx.getLocation({
  6. success:res=>{
  7. console.log(res);
  8. wx.request({
  9. url: 'http://iwenwiki.com:3002/api/lbs/location',
  10. data:{
  11. latitude:res.latitude,
  12. longitude:res.longitude
  13. },
  14. success:result=>{
  15. console.log(result);
  16. var cityName=result.data.result.ad_info.city.slice(0,2);
  17. console.log(cityName);
  18. //跳转----食疗坊界面 --把数据传递过去。
  19. wx.navigateTo({
  20. url: '../food/food',
  21. })
  22. }
  23. })
  24. }
  25. })
  26. },

wxss:

  1. /* pages/citys/citys.wxss */
  2. .location,.hotCity{
  3. padding:20rpx;
  4. font-weight: bold;
  5. font-size: 36rpx;
  6. }
  7. .getLocation{
  8. color: green;
  9. margin-top:20rpx;
  10. background: #eee;
  11. }
  12. .btn{
  13. margin: 10rpx;
  14. font-weight: normal;
  15. border-radius: 10rpx;
  16. background: #fff;
  17. border: 1px solid #999;
  18. font-size: 30rpx !important;
  19. padding: 4rpx 16rpx !important;
  20. }

app.json 需要配置:

在这里插入图片描述
效果显示:

在这里插入图片描述
在这里插入图片描述

发表评论

表情:
评论列表 (有 0 条评论,377人围观)

还没有评论,来说两句吧...

相关阅读