微信小程序 地圖拖拽,獲取地圖拖拽的位置

想要實(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)不到的

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容