Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ios getCurrentPosition 没有位置信息 #111

Open
hezhii opened this issue Dec 13, 2019 · 5 comments
Open

ios getCurrentPosition 没有位置信息 #111

hezhii opened this issue Dec 13, 2019 · 5 comments
Labels
bug Something isn't working ios Only for ios

Comments

@hezhii
Copy link

hezhii commented Dec 13, 2019

android 可以正常获取,ios 则只有经纬度信息

@hezhii hezhii added bug Something isn't working ios Only for ios labels Dec 13, 2019
@marlti7
Copy link

marlti7 commented Mar 30, 2020

你解决了吗

@tanjwGit
Copy link

tanjwGit commented Mar 30, 2020 via email

@hezhii
Copy link
Author

hezhii commented Mar 31, 2020

我目前是通过持续定位获取的

/**
 * 获取当前位置
 */
export async function getCurrentLocation() {
  try {
    // android 请求权限
    if (IS_ANDROID) {
      const granted = await PermissionsAndroid.request(PermissionsAndroid.PERMISSIONS.ACCESS_COARSE_LOCATION)
      if (granted !== PermissionsAndroid.RESULTS.GRANTED) {
        return
      }
    }

    // 初始化 sdk
    await init({
      ios: '',
      android: ''
    })

    // 监听定位变化,监听到城市位置信息之后,resolve 并停止定位
    const locationPromise = new Promise(resolve => {
      setLocatingWithReGeocode(true)
      setNeedAddress(true)
      addLocationListener(location => {
        if (location && location.adCode) {
          resolve(location)
          stop()
        }
      })
    })

    // 超时,20 秒之后直接 resolve
    const timeoutPromise = new Promise(resolve => {
      setTimeout(() => {
        stop()
        resolve()
      }, 20 * 1000)
    })

    start()
    return Promise.race([locationPromise, timeoutPromise])
  } catch (err) {
    console.warn(err)
  }
}

@QiangGGe
Copy link

  setLocatingWithReGeocode(true)

这句代码很关键

@liyuankunkun
Copy link

Geolocation.getCurrentPosition((data) => {
console.log('data----',data);
// resolve(coords);
})

打印结果:
{
"location": {
"adCode": "110105",
"street": "亮马桥东街",
"cityCode": "010",
"locationType": 4,
"speed": 0,
"altitude": 0,
"longitude": 116.00000,
"timestamp": 1701940618182,
"errorCode": 0,
"city": "北京市",
"description": "XXX",
"heading": 0,
"latitude": 39.00000,
"streetNumber": "43号",
"district": "朝阳区",
"country": "中国",
"poiName": "XXX(亮马桥店)",
"address": "北京市朝阳区亮马桥东街",
"errorInfo": "success",
"province": "北京市",
"trustedLevel": 1,
"gpsAccuracy": -1,
"coordinateType": "GCJ02",
"accuracy": 30,
"locationDetail": "#id:YbmJhYzZiZGplZjU1wam1tbW9vNjlhZGM5M2RiLFpYRm95bUJRcHlrREFIeUlKVkhtKzBLcg==#csid:951594e77d1f4e9b8dde3fff#pm111011"
},
"coords": {
"latitude": 39.000000,
"longitude": 116.000000,
"altitude": 0,
"accuracy": 30,
"altitudeAccuracy": 0,
"heading": 0,
"speed": 0
},
"timestamp": 1701940618182
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working ios Only for ios
Projects
None yet
Development

No branches or pull requests

5 participants