uniapp使用天地图,打包app,在不通过任何按钮操作的前提下,如何获取当前位置信息?

uniapp使用天地图,打包app,在不通过任何按钮操作的前提下,如何获取当前位置信息?

因为需要兼容app,所以用到了renderjs,发现只有在vue视图里面绑定renderjs里面的函数getCurrentPosition才能有机会获取定位信息,并且还是“割裂”的(希望能和 Promise 写法一样),如下:

vue试图里面:

<button ref="btn1" @tap="Trenderjs.getCurrentPosition">getCurrentPosition</button>
<script module="Trenderjs" lang="renderjs">
    export default {
        data() {
            return {
                isinit: false,
                map: null,
            }
        },
        mounted(e) {
            if (typeof window.T === 'function') {
                //this.initEcharts()
            } else {
                ////console.log('init00')
                if (this.option&&this.option.apikey) {
                    const script = document.createElement('script')
                    // view 层的页面运行在 www 根目录,其相对路径相对于 www 计算
                    script.src = 'http://api.tianditu.gov.cn/api?v=4.0&tk=' + this.option.apikey
                    
                    script.onload = this.initEcharts.bind(this)
                    
                    document.head.appendChild(script)
                }
            }
        },
        methods: {
            updateEcharts(newValue, oldValue, ownerInstance, instance) {
                // 监听 service 层数据变更
                if (this.option.apikey) {
                     const script = document.createElement('script')
                     script.src = 'http://api.tianditu.gov.cn/api?v=4.0&tk=' + this.option.apikey
                     script.onload = this.initEcharts.bind(this)
                     document.head.appendChild(script)
                }
            },
            
            initEcharts() {
                var that = this;

                that.map = new T.Map('mapDiv', {
                    projection: 'EPSG:4326'
                });

                that.map.centerAndZoom(new T.LngLat(that.option.lng, that.option.lat), 17);
                //that.map.removeEventListener("moveend");
                that.map.addEventListener("moveend", function(e) {
                    ////console.log(e.target.getCenter().getLng() + "," + e.target.getCenter().getLat())
                    that.$ownerInstance.callMethod('nijiexi', {
                        lng: e.target.getCenter().getLng(),
                        lat: e.target.getCenter().getLat(),
                        apikey: that.option.apikey
                    })
                });
            },
            
            getCurrentPosition(event, ownerInstance) {
                let that = this
                let lo = new T.Geolocation();
                
                let result = lo.getCurrentPosition((e) => {
                    let position = e
                    // {"accuracy":8177,"lnglat":{"lat":22.5272,"lng":113.9852}}
                    that.$ownerInstance.callMethod('getPosition', {
                        position: position.lnglat
                    })
                });
            },
            

        }
    }
</script>
阅读 1.4k
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题