How to get user's geolocation?

后端 未结 7 451
忘了有多久
忘了有多久 2021-01-03 02:34

On many sites I saw printed out my current city where I am (eg \"Hello to Berlin.\"). How they do that? What everything is needed for that? I guess the main part is here jav

7条回答
  •  旧巷少年郎
    2021-01-03 03:09

    I've found getCurrentPosition() to often be inaccurate since it doesn't spend a lot of time waiting on the GPS to acquire a good accuracy. I wrote a small piece of JavaScript that mimics getCurrentPosition() but actually uses watch position and monitors the results coming back until they are better accuracy.

    Here's how it looks:

    navigator.geolocation.getAccurateCurrentPosition(onSuccess, onError, {desiredAccuracy:20, maxWait:15000});
    

    Code is here - https://github.com/gwilson/getAccurateCurrentPosition

提交回复
热议问题