• 关于微信小程序map地图的使用


    结构部分: 

    <map id='mymap' bindregionchange='bindregionchange' show-location longitude='{{longitude}}' latitude='{{latitude}}' markers='{{markers}}' class="maps" scale='18' >
    <cover-image class='position_img' bindtap='getlocation' src='../images/position.png'></cover-image>
    </map>
     
    逻辑部分: 
        
    // pages/index/index.js
    const app = getApp()
    Page({

    /**
    * 页面的初始数据
    */
    data: {
    latitude: 22.3434,
    longitude: 113.535,
    markers: [
    {
    id: 1,
    iconPath: '../images/core1.png',
    latitude: 22.3434,
    longitude: 113.535,
    40,
    height: 40
    }
    ],
     
    },

    /**
    * 生命周期函数--监听页面加载
    */
    onLoad: function (options) {
    this.getLocationInfo()
    console.log(app)
     
    },

    /**
    * 生命周期函数--监听页面初次渲染完成
    */
    onReady: function () {
    var that = this
    that.mapCtx = wx.createMapContext('mymap')
    },

    /**
    * 生命周期函数--监听页面显示
    */
    onShow: function () {

    },

    /**
    * 生命周期函数--监听页面隐藏
    */
    onHide: function () {

    },

    /**
    * 生命周期函数--监听页面卸载
    */
    onUnload: function () {

    },

    /**
    * 页面相关事件处理函数--监听用户下拉动作
    */
    onPullDownRefresh: function () {

    },

    /**
    * 页面上拉触底事件的处理函数
    */
    onReachBottom: function () {

    },

    /**
    * 用户点击右上角分享
    */
    onShareAppMessage: function () {

    },
    // 获取用户的位置
    getLocationInfo() {
    var that = this
    wx.getLocation({
    type: 'gcj02',
    success: function(res) {
    console.log(res)
    that.setData({
    longitude: res.longitude,
    latitude: res.latitude,
    markers: [
    {
    id: 1,
    iconPath: '../images/core1.png',
    latitude: res.latitude,
    longitude: res.longitude,
    40,
    height: 40
    }
    ]
    })
    },
    })
    },
    // d定位当前位置
    getlocation() {
    var that = this
    wx.getLocation({
    type: 'gcj02', //最好带上type
    success: function (res) {
    console.log(res)
    that.setData({
    longitude: res.longitude,
    latitude: res.latitude,
    markers: [
    {
    id: 1,
    iconPath: '../images/core1.png',
    latitude: res.latitude,
    longitude: res.longitude,
    40,
    height: 40
    }
    ]
    })
    },
    })
    },
    // 当视图发生变化时触发
    bindregionchange(e) {
    var that = this
    if(e.type=='end'){
    that.mapCtx.getCenterLocation({
    success: function(res) {
    // console.log(res)
    that.setData({
    markers: [
    {
    id: 1,
    iconPath: '../images/core1.png',
    latitude: res.latitude,
    longitude: res.longitude,
    40,
    height: 40
    }
    ]
    })
    }
    })
    }
    }
    })
     

  • 相关阅读:
    BOM与DOM的区别与联系
    HTTP与HTTPS的区别
    总结一下C++与C#之间的区别
    点标记(lambda表达式+linq查询标记符)与linq语句(查询表达式)
    java多线程:继承Thread和实现Runable接口的区别
    打印BroadcastReceiver的所有接受者
    修改apk里面的源码
    关于启动模式中的问题
    onSaveInstanceState和onRestoreInstanceState()
    Options Menu的android3.0以上和以下版本显示刷新原理,刷新适配
  • 原文地址:https://www.cnblogs.com/PinkYun/p/9700140.html
Copyright © 2020-2023  润新知