:
想要實(shí)現(xiàn)地圖拖拽嫂拴,中心點(diǎn)保持不變束凑。
需要用到map:bindregionchange="regionchange"? 這個(gè)方法
mapCtx.getCenterLocation? 這個(gè)默認(rèn)類型是gcj02坐標(biāo),但是騰訊地圖是wgs84坐標(biāo)系富稻,就需要gcj02轉(zhuǎn)wgs84坐標(biāo)掷邦。
最終結(jié)果為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)
這個(gè)才是給api轉(zhuǎn)換用的經(jīng)緯度
話不多說 上代碼
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: '當(dāng)前位置',
? ? ? ? ? content: "緯度:" + latitude + ",經(jīng)度:" + longitude,
? ? ? ? })
? ? ? }
? ? })
? },
? regionchange(e) {
? ? console.log(e)
? ? // 地圖發(fā)生變化的時(shí)候,獲取中間點(diǎn)椭赋,也就是用戶選擇的位置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, //定位點(diǎn)半徑
? ? ? ? ? ? ? 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
? },
? /**
? * 判斷是否在國內(nèi)抚岗,不在國內(nèi)則不做偏移
? * @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:沒有找到好的方法居中
所以真實(shí)經(jīng)緯度用戶是感應(yīng)不到的