shuishen
2022-02-21 e2ba68694f02dc1a6d35d5d0a8d6199e702179fc
leafletMapOur/loginMap/js/getPosition.js
@@ -1,180 +1,18 @@
class $getPositions {
    constructor(arg) {
        this.id = arg.id || 0 //任务id
        this.fn = arg.fn ? function (data) {
            arg.fn(data)
        } : function (data) {
            console.log("未设置输出回调")
            return data
        }
        this.ourPosition = [] //全部位置
        this.nowPosition = [] //当前位置
        this.onceNowPosition = [] //当前一次位置
        this.getOnceSPosition = null
        this.state = false
        this.isContinued = false
        this.isContinuedTimeInt = null
        this.isContinuedTimeOut = null
        return this
window.qq = window.qq || {}
qq.maps = qq.maps || {}
window.soso || (window.soso = qq)
soso.maps || (soso.maps = qq.maps);
(function () {
    function getScript (src) {
        var protocol = (window.location.protocol == "https:") ? "https://" : "http://"
        src = src && (src.indexOf("http://") === 0 || src.indexOf("https://") === 0) ? src : protocol + src
        document.write('<' + 'script src="' + src + '"' + ' type="text/javascript"><' + '/script>')
    }
    //开始获取
    init () {
        //判断权限
        return this
    qq.maps.__load = function (apiLoad) {
        delete qq.maps.__load
        apiLoad([["2.4.142", "T7RBZ-62U3X-RSQ4P-ZZVCB-WE7JT-HRBOG", 0], ["https://mapapi.qq.com/", "jsapi_v2/2/4/142/mods/", "https://mapapi.qq.com/jsapi_v2/2/4/142/theme/", true], [1, 18, 34.519469, 104.461761, 4], [1645433081331, "https://pr.map.qq.com/pingd", "https://pr.map.qq.com/pingd"], ["https://apis.map.qq.com/jsapi", "https://apikey.map.qq.com/mkey/index.php/mkey/check", "https://sv.map.qq.com/xf", "https://sv.map.qq.com/boundinfo", "https://sv.map.qq.com/rarp", "https://apis.map.qq.com/api/proxy/search", "https://apis.map.qq.com/api/proxy/routes/", "https://confinfo.map.qq.com/confinfo", "https://overseactrl.map.qq.com"], [[null, ["https://rt0.map.gtimg.com/tile", "https://rt1.map.gtimg.com/tile", "https://rt2.map.gtimg.com/tile", "https://rt3.map.gtimg.com/tile"], "png", [256, 256], 3, 19, "114", true, false], [null, ["https://m0.map.gtimg.com/hwap", "https://m1.map.gtimg.com/hwap", "https://m2.map.gtimg.com/hwap", "https://m3.map.gtimg.com/hwap"], "png", [128, 128], 3, 18, "110", false, false], [null, ["https://p0.map.gtimg.com/sateTiles", "https://p1.map.gtimg.com/sateTiles", "https://p2.map.gtimg.com/sateTiles", "https://p3.map.gtimg.com/sateTiles"], "jpg", [256, 256], 1, 19, "101", false, false], [null, ["https://rt0.map.gtimg.com/tile", "https://rt1.map.gtimg.com/tile", "https://rt2.map.gtimg.com/tile", "https://rt3.map.gtimg.com/tile"], "png", [256, 256], 1, 19, "", false, false], [null, ["https://sv0.map.qq.com/hlrender/", "https://sv1.map.qq.com/hlrender/", "https://sv2.map.qq.com/hlrender/", "https://sv3.map.qq.com/hlrender/"], "png", [256, 256], 1, 19, "", false, false], [null, ["https://rtt2.map.qq.com/rtt/", "https://rtt2a.map.qq.com/rtt/", "https://rtt2b.map.qq.com/rtt/", "https://rtt2c.map.qq.com/rtt/"], "png", [256, 256], 1, 19, "", false, false], null, [["https://rt0.map.gtimg.com/vector/", "https://rt1.map.gtimg.com/vector/", "https://rt2.map.gtimg.com/vector/", "https://rt3.map.gtimg.com/vector/"], [256, 256], 3, 18, "114", ["https://rt0.map.gtimg.com/icons/", "https://rt1.map.gtimg.com/icons/", "https://rt2.map.gtimg.com/icons/", "https://rt3.map.gtimg.com/icons/"], []], null], ["https://s.map.qq.com/TPano/v1.1.2/TPano.js", "map.qq.com/", ""], "{\"ver\":5,\"isup\":1,\"url\":\"https://mapstyle.qpic.cn/fileupdate/jsauto/style?id=30&version=5\"}"], loadScriptTime)
    }
    var loadScriptTime = (new Date).getTime()
    getScript("https://mapapi.qq.com/jsapi_v2/2/4/142/main.js")
    }
    continued () {
        //开始持续获取定位
        console.log("开始持续定位")
        let that = this
        this.isContinued = true
        if (this.isContinuedTimeInt) {
            console.log("重复开始持续定位")
            return
        } else {
            that.once(function (res) {
                if (res.state == false) {
                    console.log(res.intlat)
                }
            })
            this.isContinuedTimeInt = setInterval(() => {
                that.once(function (res) {
                    if (res.state == false) {
                        console.log(res.intlat)
                    }
                })
            }, 5000)
        }
    }
    continuedTime (val) {
        //开始持续获取定位
        let that = this
        if (val != 1) {
            // console.log(that.id,"开始持续定位")
            this.isContinued = true
            that.once(function (res) {
                if (res.state == false) {
                    // console.log(res.intlat)
                    that.outData()
                }
            })
            if (this.isContinuedTimeOut) {
                // console.log("重复开始持续定位")
                return
            }
        }
        this.isContinuedTimeOut = setTimeout(() => {
            that.once(function (res) {
                if (res.state == false) {
                    // console.log(res.intlat)
                    that.outData()
                    if (res.ok) {
                        //返回成功
                        that.continuedTime(1)
                    } else {
                        //返回失败
                        that.continuedTime(1)
                    }
                }
            })
        }, 3000)
    }
    overContinued () {
        // console.log("结束持续定位")
        let that = this
        if (this.isContinued) {
            this.isContinued = false
            if (this.isContinuedTimeInt) {
                clearInterval(that.isContinuedTimeInt)
                this.isContinuedTimeInt = null
            }
            if (this.isContinuedTimeOut) {
                clearTimeout(that.isContinuedTimeOut)
                this.isContinuedTimeOut = null
            }
        }
        this.isContinued = false
        that.outData('out')
    }
    once (fn) {
        // console.log("开始获取一次定位");
        this.state = true
        let ok = true
        let that = this,
            fns = fn || '',
            outit = function () {
                if (fns) {
                    fns({
                        state: that.state,
                        intlat: that.onceNowPosition,
                        ok: ok
                    })
                } else {
                    that.outData()
                }
            }
        outit()
        let success = function (position) {
            // console.log(position);
            var lat = position.coords.longitude//经度
            var lon = position.coords.latitude//纬度
            // console.log("成功返回经纬度信息");
            // console.log(lon, lat, "成功");
            that.onceNowPosition = [lat, lon]
            that.state = false
            ok = true
            outit()
        }
        let error = function (error) {
            // console.log(error, "失败");
            let msg = "none"
            switch (error.code) {
                case error.PERMISSION_DENIED:
                    msg = "User denied the request for Geolocation."
                    break
                case error.POSITION_UNAVAILABLE:
                    msg = "Location information is unavailable."
                    break
                case error.TIMEOUT:
                    msg = "The request to get user location timed out."
                    break
                case error.UNKNOWN_ERROR:
                    msg = "An unknown error occurred."
                    break
            }
            that.onceNowPosition = msg
            that.state = false
            ok = false
            outit()
        }
        let options = {
            enableHighAccuracy: true,
            // timeout: 5000,
            timeout: Infinity,
            maximumAge: 0,
        }
        this.getOnceSPosition = navigator.geolocation.getCurrentPosition(
            success,
            error,
            options
        )
    }
    //结束获取
    outInit () {
    }
    //抛出数据
    outData (val) {
        let d = {
            state: this.state,
            intlat: this.onceNowPosition
        }
        if (val) {
            d[val] = val
        }
        this.fn(d)
    }
}
// export default $getPositions;
})()