A
A
Andrey Okhotnikov2019-05-30 19:53:22
Yandex maps
Andrey Okhotnikov, 2019-05-30 19:53:22

How to get the user's city in react-yandex-map?

I use the react component from Yandex . But I can't get the user's location. Code from link or below

import React from "react";
import ReactDOM from "react-dom";
import { YMaps } from "react-yandex-maps";

import "./styles.css";
const getGeoLocation = ymaps => {
  return ymaps.geolocation
    .get({ provider: "yandex", mapStateAutoApply: true })
    .then(result =>
      ymaps.geocode(result.geoObjects.position).then(res => {
        let firstGeoObject = res.geoObjects.get(0);
        console.log(
          firstGeoObject.getLocalities().length
            ? firstGeoObject.getLocalities()
            : firstGeoObject.getAdministrativeAreas()
        );
      })
    );
};

const handleApiAvaliable = ymaps => {
  const geolocation = getGeoLocation(ymaps);

  console.log(geolocation)
};

function App() {
  return (
    <div className="App">
      <YMaps onApiAvaliable={ymaps => handleApiAvaliable(ymaps)} />
    </div>
  );
}

const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question