微信小程序如何设置地图导航

微信小程序如何设置地图导航,第1张

微信小程序如何设置地图导航

登入到速成应用平台账号,进入到“小程序模板”页面,选择“新建空白小程序”或选中某个模板后点击“使用”,随后设置下项目封面和标题,进入到小程序编辑页面;可以使用文本、图片等组件,将选中的组件(如文本)点击/拖拽到右边“编辑框页面中”,如下截图

二 、如何设置州迹笑地图导航

点击放置在模板中的组件(如文本)然后右侧面板点击“事件-功能-调用功能-地图导航”进行设置。

三 、如何绑定位置信息

右侧面板设置好地图导航,点击“地图管理-功能数据管理-地图管理”添加地址,填写公司名称、位置信息,保存;返回编辑页面并点击保存,然后刷新页面,选择文本并点击“事件-功能-绑定地址”,会出现我们刚设置好的“公司名称”,选中他即绑州基定成功。说明:可显示路线并调册含用手机上“地图app”。

好了,今天的学习就到了,小姐姐希望阔爱的你可以鼓起勇气,用上地图导航,让它引导你走到心上那个人儿所在的地方。恋人也罢,朋友、亲人也罢,相信你将不虚此行~

标记点markers只能在初始化的时候设置,不支持动态更告樱裂新。

示例:

<!-- map.wxml --> <map longitude="23.099994" latitude="113.324520" markers="{{markers}}" covers="{{covers}}" style="width: 375pxheight: 200px"></map> // map.js Page({ data: {markers: [{ latitude: 23.099994, longitude: 113.324520, name: 'T.I.T 创意园'袜闭, desc: '我现在的位置'}],covers: [{ latitude: 23.099794, longitude: 113.324520, icaonPath: '../images/car.png', rotate: 10}, { latitude: 23.099298, longitude: 113.324129, iconPath: '../颂滚images/car.png', rotate: 90}] } })

首先分析制作的思路:

1.在app.json文件的pages数组里加上main文件夹和template(模板)文件夹的路径。

2.在main.js文件中,在onLoad()函数中调用loadInfo()函数。

3. 自定义获取位置的函数loadInfo(),调用wx.getLocation,用于获取当前的纬度(latitude)和经度(longitude)。在loadInfo()函数中接着调用loadCity()函数。

4. 自定义获取城市的函数loadCity(),调用wx.request,在“百度地图开放平台”网站中注册自己的AK,通过获取城市信息的网址(http://api.map.baidu.com/geocoder/v2/?ak=自己的ak&location=纬度值,经度值&output=json)实现当前城市名称的获取。

在loadCity()函数中接着调用loadWeather()函数。

5.自定义获取天气的函数loadWeather(),根据已有的城市名称,通过获取天气信息的网址(http://wthrcdn.etouch.cn/weather_mini?city=城市名)实现天气信息的数据获取。

6.在main.wxml文件中,销扰未来天气部分通过import调用了自定义模亏旦旦板文件itemtpl.wxml。

然后分析项目中文件的源码。

app.json文件的代码如下:

{

"pages":[

"pages/main/main",

"pages/template/itemtpl",

"pages/index/index",

"pages/logs/logs"

],

"window":{

"backgroundTextStyle":"迟肆light",

"navigationBarBackgroundColor": "#fff",

"navigationBarTitleText": "天气",

"navigationBarTextStyle":"black"

}

}

main.wxml的代码如下:

<view class='cont'>

<!-- 今日天气-->

<view class='today'>

<view class='info'>

<view class='tempe'>{{today.wendu}}°C</view>

<view class='weather'>{{today.todayInfo.type}}{{today.todayInfo.fengxiang}}</view>

<view>温馨提示:{{today.ganmao}}</view>

<view class='city'>{{city}}</view>

</view>

</view>

<!-- 未来天气-->

<import src="../template/itemtpl"/>

<view class='future'>

<block wx:for="{{future}}">

<template is="future-item" data="{{item}}"/>

</block>

</view>

</view>

main.wxss文件的代码如下:

/**/

.cont{

font-size:30rpx

width:100%

height:100%

}

.today{

padding:50rpx 0 0 0

height:50%

}

.info{

padding:20rpx

background:rgba(0,0,0,.8)

line-height: 1.5em

color:#eee

}

.tempe{

font-size:90rpx

text-align: center

margin:30rpx 0

}

.weather{

text-align: center

}

.city{

font-size:40rpx

color:#f60

text-align: right

margin: 30rpx 10rpx 0 0

}

.future{

display:flex

flex-direction: row

height:100%

padding:20rpx 0 20rpx 10rpx

background:rgba(0,0,0,.8)

text-align: center

margin:50rpx 0 70rpx 0

color:#eee

}

.future-item{

min-height: 100%

width:160rpx

margin: 0 10rpx

border:solid 1px #f90

padding:10rpx 0 0 0

line-height:2em

}

main.js文件的代码如下:

//

Page({

data: {

// weatherData:''

city:"" ,

today:{},

future:{},

},

onLoad: function () {

this. loadInfo()

},

//自定义获取位置

loadInfo:function(){

var page=this

wx.getLocation({

type: 'gcj02', //返回可以用于wx.openLocation的经纬度

success: function (res) {

var latitude = res.latitude

var longitude = res.longitude

console.log(latitude, longitude)

page.loadCity(latitude, longitude)

}

})

} ,

//自定义获取城市

loadCity: function (latitude, longitude){

var page = this

wx.request({

url: 'http://api.map.baidu.com/geocoder/v2/?ak=自己的AK &location=' + latitude + ',' + longitude + '&output=json',

header: {

'content-type': 'application/json'

},

success: function (res) {

console.log(res)

var city=res.data.result.addressComponent.city

city=city.replace("市","")

page.setData({

city:city

})

page.loadWeather(city)

}

})

},

//自定义获取天气

loadWeather: function (city) {

var page = this

wx.request({

url: 'http://wthrcdn.etouch.cn/weather_mini?city=' + city,

header: {

'content-type': 'application/json'

},

success: function (res) {

console.log(res)

var future=res.data.data.forecast

var todayInfo=future.shift()

var today=res.data.data

today.todayInfo=todayInfo

page.setData({

today:today,

future:future,

})

}

})

}

})

itemtpl.wxml的代码如下:

<!-- 模板文件-->

<template name="future-item">

<view class="future-item">

<view>{{item.date}}</view>

<view>{{item.type}}</view>

<view>{{item.fengxiang}}</view>

<view>{{item.low}}</view>

<view>{{item.high}}</view>

</view>

</template>

至此,案例制作完成。


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/yw/12436551.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-23
下一篇2023-05-23

发表评论

登录后才能评论

评论列表(0条)

    保存