最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - navigator.geolocation.getCurrentPosition not working - Stack Overflow

programmeradmin4浏览0评论

I can't seem to get navigator.geolocation to work. It goes into the error function with error code 3, and error message "Location request timed out" What is my code below doing wrong?

import { AppRegistry } from 'react-native'
import { App } from './src'

var options = {
    enableHighAccuracy: true,
    timeout: 5000,
    maximumAge: 0
  };

  function success(pos) {
    var crd = pos.coords;

    console.log('Your current position is:');
    console.log(`Latitude : ${crd.latitude}`);
    console.log(`Longitude: ${crd.longitude}`);
    console.log(`More or less ${crd.accuracy} meters.`);
  }

  function error(err) {
    console.warn(`ERROR(${err.code}): ${err.message}`);
  }

  navigator.geolocation.getCurrentPosition(success, error, options);

GLOBAL.XMLHttpRequest = GLOBAL.originalXMLHttpRequest || GLOBAL.XMLHttpRequest
console.error = (error) => error.apply
AppRegistry.registerComponent('vepo', () => App)

I can't seem to get navigator.geolocation to work. It goes into the error function with error code 3, and error message "Location request timed out" What is my code below doing wrong?

import { AppRegistry } from 'react-native'
import { App } from './src'

var options = {
    enableHighAccuracy: true,
    timeout: 5000,
    maximumAge: 0
  };

  function success(pos) {
    var crd = pos.coords;

    console.log('Your current position is:');
    console.log(`Latitude : ${crd.latitude}`);
    console.log(`Longitude: ${crd.longitude}`);
    console.log(`More or less ${crd.accuracy} meters.`);
  }

  function error(err) {
    console.warn(`ERROR(${err.code}): ${err.message}`);
  }

  navigator.geolocation.getCurrentPosition(success, error, options);

GLOBAL.XMLHttpRequest = GLOBAL.originalXMLHttpRequest || GLOBAL.XMLHttpRequest
console.error = (error) => error.apply
AppRegistry.registerComponent('vepo', () => App)
Share Improve this question asked Apr 18, 2018 at 18:18 BeniaminoBagginsBeniaminoBaggins 12.5k46 gold badges174 silver badges334 bronze badges 1
  • 1 Try removing maximumAge: 0 – Pritish Vaidya Commented Apr 18, 2018 at 18:51
Add a ment  | 

4 Answers 4

Reset to default 3

This is a known location bug in react native and specially Android. Do not send the third parameter (the options one) and see if that works.

If has to do with the highAccuracy and maximumAge

An alternative would be to use the HTML and JS code provided by Mozilla: https://developer.mozilla/en-US/docs/Web/API/Geolocation/getCurrentPosition#Specifications Worked the first time I tried it ... the call is in HTML, and the code is in JS.

Here's the HTML code:

      <p><button onclick="geoFindMe()">Show my location</button></p>
<div id="out"></div>

Here's the JS code:

    function geoFindMe() {
                 var output = document.getElementById("out");

             if (!navigator.geolocation){
               output.innerHTML = "<p>Geolocation is not supported by your browser</p>";
               return;
             }

             function success(position) {
               var latitude  = position.coords.latitude;
               var longitude = position.coords.longitude;

               output.innerHTML = '<p>Latitude is ' + latitude + '° <br>Longitude is ' + longitude + '°</p>';

               var img = new Image();
               img.src = "https://maps.googleapis./maps/api/staticmap?center=" + latitude + "," + longitude + "&zoom=13&size=300x300&sensor=false";

               output.appendChild(img);
             }

             function error() {
               output.innerHTML = "Unable to retrieve your location";
             }

             output.innerHTML = "<p>Locating…</p>";

                 navigator.geolocation.getCurrentPosition(success, error);
     } 

Note: this code is straight out of Mozilla (link below - see "Geolocation Live Example":

https://developer.mozilla/en-US/docs/Web/API/Geolocation_API

You should request the navigator.geolocation.getCurrentPosition inside ponentDidMount or in a function call:

ponentDidMount() {
   navigator.geolocation.getCurrentPosition(success, error, options);
}
ponentDidMount(){
navigator.geolocation.getCurrentPosition(
                    (position) => {
                               console.log(position)
                                  })
}

this work for me, remember add the permission in Android

发布评论

评论列表(0)

  1. 暂无评论