getCurrentPosition in JS does not work on iOS

后端 未结 4 865
孤街浪徒
孤街浪徒 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:11

    You need to check three things

    Here's what we need to check:

    1. The user's OS has location services disabled.
    2. The user's browser has location services disabled.
    3. The user's browser doesn't support checking for location services at all.

    Combining the other answers on the page, we can handle all of these scenarios.

    This should get you up and running to handle all three scenarios:

        /* Set up getCurrentPosition options with a timeout */
        const navigatorLocationOptions = {
          enableHighAccuracy: true,
          timeout: 5000,
          maximumAge: 0
        };
    
        /* Determine browser permissions status */
        navigator.permissions.query({name:'geolocation'})
          .then((result) => {
            /* result.state will be 'granted', 'denied', or 'error' */
            if (result.state === 'granted') {
              navigator.geolocation.getCurrentPosition(pos => {
                 console.log('Retrieved user location', pos);
                 /* Got the location! Write your successful code here. */            
    
              }, (error) => {
                /* System/OS location services disabled */
                console.log('System/OS services disabled', navigator);
                noLocationFound();
              }, navigatorLocationOptions);
    
            } else {
              /* Browser location services disabled or error */
              console.log('Browser location services disabled', navigator);
              noLocationFound();
            }
          }, (error) => {
            /* Browser doesn't support querying for permissions */
            console.log('Browser permissions services unavailable', navigator);
            noLocationFound()
          });
    
          function noLocationFound() {
            /* Write code here for user location information is unavailable */
          }
    

提交回复
热议问题