getCurrentPosition in JS does not work on iOS

后端 未结 4 867
孤街浪徒
孤街浪徒 2021-01-18 01:48

I have a page that contains a code that gets the current location from the device and load other stuff based on the location with this code:

if (navigator.ge         


        
4条回答
  •  太阳男子
    2021-01-18 02:19

    GeoLocation works only if a user provides his/her permissions.

    Make sure that is the case.

    This CodePen demo has been tested in iOS simulator with Safari and successfully shown the latitude and the longitude.

    var options = {
      enableHighAccuracy: true,
      timeout: 5000,
      maximumAge: 0
    };
    
    function log(data) {
      const tag = document.createElement('p');
      tag.textContent = data;
      document.body.appendChild(tag);
    }
    
    function success(pos) {
      var crd = pos.coords;
      console.log('Successfully determined a user position:', crd);
    
      log('Your current position is:');
      log(`Latitude : ${crd.latitude}`);
      log(`Longitude: ${crd.longitude}`);
      log(`More or less ${crd.accuracy} meters.`);
    }
    
    function error(err) {
      console.warn(`ERROR(${err.code}): ${err.message}`);
    }
    
    navigator.geolocation.getCurrentPosition(success, error, options);

    It can't be the case if your code doesn't hit either if nor else unless some other code has triggered an infinite loop earlier in your code.

提交回复
热议问题