美文网首页
微信小程序 地图拖拽,获取地图拖拽的位置

微信小程序 地图拖拽,获取地图拖拽的位置

作者: 莂问我丶 | 来源:发表于2019-02-28 15:58 被阅读0次

想要实现地图拖拽,中心点保持不变。

需要用到map:bindregionchange="regionchange"  这个方法

mapCtx.getCenterLocation  这个默认类型是gcj02坐标,但是腾讯地图是wgs84坐标系,就需要gcj02转wgs84坐标。

最终结果为js文件中

var coordinate = that.gcj02towgs84(res.longitude, res.latitude)

          console.log(coordinate, 2222)

重要的事情说说三遍:

console.log(coordinate, 2222)

console.log(coordinate, 2222)

console.log(coordinate, 2222)

这个才是给api转换用的经纬度

话不多说 上代码

wxml:

<view class="container">

  <map id="map4select" longitude="{{longitude}}" latitude="{{latitude}}" scale="14" controls="{{controls}}" markers="{{markers}}" show-location bindcontroltap="controltap" polyline="{{polyline}}" bindmarkertap="markertap" circles="{{circles}}" bindregionchange="regionchange"

    class='map'>

    <cover-image class="cover-image" bindtap="my_location" src="/images/mapicon.png" />

    <!-- <cover-image class="cover-image_confirm" bindtap="confirm_bag" src="/images/mapicon.png" /> -->

  </map>

</view>

js:

const app = getApp()

//定义一些常量

var x_PI = 3.14159265358979324 * 3000.0 / 180.0;

var PI = 3.1415926535897932384626;

var a = 6378245.0;

var ee = 0.00669342162296594323;

Page({

  data: {

    longitude: '108.947040',

    latitude: '34.259430',

  },

  onLoad() {

    var that = this;

    wx.getLocation({

      type: 'wgs84',

      success: function(res) {

        // console.log(res);

        var latitude = res.latitude

        var longitude = res.longitude

        that.setData({

          latitude: latitude,

          longitude: longitude

        })

        //弹框

        wx.showModal({

          title: '当前位置',

          content: "纬度:" + latitude + ",经度:" + longitude,

        })

      }

    })

  },

  regionchange(e) {

    console.log(e)

    // 地图发生变化的时候,获取中间点,也就是用户选择的位置toFixed

    if (e.type == 'end' && (e.causedBy == 'scale' || e.causedBy == 'drag')) {

      console.log(e)

      var that = this;

      this.mapCtx = wx.createMapContext("map4select");

      this.mapCtx.getCenterLocation({

        type: 'gcj02',

        success: function(res) {

          console.log(res, 11111)

          var coordinate = that.gcj02towgs84(res.longitude, res.latitude)

          console.log(coordinate, 2222)

          that.setData({

            latitude: res.latitude,

            longitude: res.longitude,

            circles: [{

              latitude: res.latitude,

              longitude: res.longitude,

              color: '#FF0000DD',

              fillColor: '#d1edff88',

              radius: 0, //定位点半径

              strokeWidth: 1

            }]

          })

        }

      })

    }

  },

  //定位到自己的位置事件

  my_location: function(e) {

    var that = this;

    that.onLoad();

  },

  gcj02towgs84(lng, lat) {

    var that  = this;

    if (that.out_of_china(lng, lat)) {

      return [lng, lat]

    } else {

      var dlat = that.transformlat(lng - 105.0, lat - 35.0);

      var dlng = that.transformlng(lng - 105.0, lat - 35.0);

      var radlat = lat / 180.0 * PI;

      var magic = Math.sin(radlat);

      magic = 1 - ee * magic * magic;

      var sqrtmagic = Math.sqrt(magic);

      dlat = (dlat * 180.0) / ((a * (1 - ee)) / (magic * sqrtmagic) * PI);

      dlng = (dlng * 180.0) / (a / sqrtmagic * Math.cos(radlat) * PI);

      var mglat = lat + dlat;

      var mglng = lng + dlng;

      return [lng * 2 - mglng, lat * 2 - mglat]

    }

  },

  transformlat(lng, lat) {

    var ret = -100.0 + 2.0 * lng + 3.0 * lat + 0.2 * lat * lat + 0.1 * lng * lat + 0.2 * Math.sqrt(Math.abs(lng));

    ret += (20.0 * Math.sin(6.0 * lng * PI) + 20.0 * Math.sin(2.0 * lng * PI)) * 2.0 / 3.0;

    ret += (20.0 * Math.sin(lat * PI) + 40.0 * Math.sin(lat / 3.0 * PI)) * 2.0 / 3.0;

    ret += (160.0 * Math.sin(lat / 12.0 * PI) + 320 * Math.sin(lat * PI / 30.0)) * 2.0 / 3.0;

    return ret

  },

  transformlng(lng, lat) {

    var ret = 300.0 + lng + 2.0 * lat + 0.1 * lng * lng + 0.1 * lng * lat + 0.1 * Math.sqrt(Math.abs(lng));

    ret += (20.0 * Math.sin(6.0 * lng * PI) + 20.0 * Math.sin(2.0 * lng * PI)) * 2.0 / 3.0;

    ret += (20.0 * Math.sin(lng * PI) + 40.0 * Math.sin(lng / 3.0 * PI)) * 2.0 / 3.0;

    ret += (150.0 * Math.sin(lng / 12.0 * PI) + 300.0 * Math.sin(lng / 30.0 * PI)) * 2.0 / 3.0;

    return ret

  },

  /**

  * 判断是否在国内,不在国内则不做偏移

  * @param lng

  * @param lat

  * @returns {boolean}

  */

  out_of_china(lng, lat) {

    return (lng < 72.004 || lng > 137.8347) || ((lat < 0.8293 || lat > 55.8271) || false);

  }

})

wxss:

.container{

  width: 100%;

  height: 100%;

}

.cover-image {

  position: absolute;

  top: 50%;

  left: 50%;

  margin-top: -40rpx;

  margin-left: -40rpx;

  width: 80rpx;

  height: 80rpx;

}

.cover-image_confirm {

  width: 50rpx;

  height: 50rpx;

}

.map {

  width: 100vw;

  height: 100vh;

}

PS:cover-image:没有找到好的方法居中

所以真实经纬度用户是感应不到的

相关文章

网友评论

      本文标题:微信小程序 地图拖拽,获取地图拖拽的位置

      本文链接:https://www.haomeiwen.com/subject/rzraeftx.html