zhangrl
zhangrl
  • 发布:2021-01-15 17:56
  • 更新:2021-01-19 22:18
  • 阅读:1708

h5 uni.getlocation 获取不到坐标

分类:uni-app

h5 uni.getlocation 获取不到坐标 提示 {“errMsg”:"getLocation:ok","verticalAccuracy":0,"horizontalAccuracy":30}```javascript

  uni.getLocation({  
            // #ifdef APP-PLUS  
            type: 'gcj02',  
            geocode: true,  
            // #endif  
            success: function(r) {  
                uni.showModal({  
                    title: 'success提示',  
                    content: JSON.stringify(r),  
                    success: function (res) {  
                        if (res.confirm) {  
                            console.log('用户点击确定');  
                        } else if (res.cancel) {  
                            console.log('用户点击取消');  
                        }  
                    }  
                })  
                self._locationRefresh = false  
                ab_.debug && console.log('获取定位成功')  
                console.log(r)  
                // 定位缓存  
                self._location = r  
                res = r  

                if (!self._locationing) {  
                    // 10分钟刷新一次  
                    self._locationing = setInterval(function() {  
                        self.locationRefresh()  
                    }, 600000)  
                }  
            },  
            fail: function(e) {  
                uni.showModal({  
                    title: 'fail提示',  
                    content: JSON.stringify(e),  
                    success: function (res) {  
                        if (res.confirm) {  
                            console.log('用户点击确定');  
                        } else if (res.cancel) {  
                            console.log('用户点击取消');  
                        }  
                    }  
                })  
                ab_.debug && console.log('获取定位失败')  
                err = e  
            },  
            complete: complete,  
        })
2021-01-15 17:56 负责人:无 分享
已邀请:
gweii

gweii

贴下你的代码,说明运行环境吧,不然没法分析

  • zhangrl (作者)

    已贴出,运行到浏览器

    2021-01-16 23:04

gweii

gweii

如果是运行到浏览器,用Chrome的话需要翻墙才能拿到坐标(官方文档里有讲的),是不是这个原因?

下面是我的代码,执行后能在控制台打印经纬度

    /**  
     * 获取当前位置  
     */  
    getLocation: () => {  
        return new Promise((resolve, reject) => {  
            uni.getLocation({  
                type: 'gcj02',  
                success: (res) => {  
                    const loc = {  
                        // 给返回对象的各属性赋值  
                        longitude: res.longitude,  
                        latitude: res.latitude,  
                    }  
                    console.log("Current location: longitude =" + loc.longitude + ", latitude =" + loc.latitude);  
                    resolve(loc);  
                },  
                fail: (res) => {  
                    reject(res);  
                }  
            })  
        })  
    },
MonikaChen

MonikaChen

https还是http?

该问题目前已经被锁定, 无法添加新回复