Reactjs: как установить navigator.geolocation.getCurrentPosition в cookie - PullRequest
0 голосов
/ 15 мая 2019

Я пытаюсь установить getcurrentLocation в cookie при первом запуске.Но не могу этого сделать.

Мой текущий cookie.js

export default{
    /**
   * @func persistent
   *
   * @returns {boolean} Returns true if cookies are persistent, false if not.
   */
  persistent() {
    if (navigator.cookieEnabled != null && navigator.cookieEnabled) {
      const cookieName = "dummy.cookie.data";
      const expires = new Date();
      expires.setYear(expires.getYear() + 1930); // We try to save a cookie for 30 years
      document.cookie = `${cookieName}; expires=${expires.toUTCString()}`;
  if (document.cookie.indexOf(cookieName) !== -1) {
    this.remove(cookieName);
    return true;
  }

  const userMarket = getLocation();
  document.cookie = `${cookieName}; expires=${userMarket}`;
  function getLocation() {
    if(navigator.geolocation) {
      navigator.geolocation.getCurrentPosition(geoSuccess, geoError);
    } else {
      console.log("Geolocation is not supported by this browser.");
      }
  }

  function geoSuccess(position) {
    var lat = position.coords.latitude;
    var lng = position.coords.longitude;
    console.log("lat:" + lat + " lng:" + lng);
  }
  this.remove(cookieName);
}
return false;
  }
};

Я хочу добавить navigator.geolocation.getCurrentPosition, чтобы получить его значение.

// пример

navigator.geolocation.getCurrentPosition(
   (position) => {
          this.setState({
            latitude: position.coords.latitude,
            longitude: position.coords.longitude,
            error: null,
          });
        },
        (error) => this.setState({ error: error.message }),
        { enableHighAccuracy: false, timeout: 20000, maximumAge: 1000 },
      );

здесь, как я могу создать функцию util для извлечения геолокации?

Любой вывод будет полезен.Спасибо

1 Ответ

1 голос
/ 15 мая 2019

В вашем примере вы пытаетесь настроить cookie с пустым именем, и этот cooki был настроен с пустым именем и значением dummy.cookie.data.

Вам также нужно установить значение.

Пример:

document.cookie = `${cookieName}=yourExampleValue; expires=${expires.toUTCString()}`;

Вот документы .

...