K
K
kirillleogky2019-12-08 13:16:44
JavaScript
kirillleogky, 2019-12-08 13:16:44

How to add your coordinates to the function call (with ymaps.ready )?

There is a main js:

import getUserLocation from './getLocation';
import addMap from './addMap';

const userLocation = getUserLocation();
/* eslint-disable */
ymaps.ready(addMap(53.9000, 27.5667));
/* eslint-enable */

getLocation.js :
export default async function getUserLocation() {

  return fetch(`https://ipinfo.io/json?token=${токен}`).then((response) => response.json());
}

and actually addMap.js :
export default function initMap(latitude, longitude) {
  /* eslint-disable */
   new ymaps.Map('map', {
         center: [latitude, longitude],
         zoom: 15,
        });
  /* eslint-enable */
}

How can I take coordinates from another API and insert them into addMap.js when called?
In my case, an error appears:
5deccd85c32d7337513137.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton Neverov, 2019-12-08
@kirillleogky

1) Create a global maps object:

import getUserLocation from './getLocation';
import addMap from './addMap';

let maps;

2) When the maps are ready, create a map and get the user's geolocation, update the map position:
ymaps.ready(function() {
  maps = new ymaps.Map("map", {
    center: [55.76, 37.64],
    zoom: 2
  });
  getUserLocation().then((lat, lng) => maps.setCenter([55.76, 37.64]));
});

The position can be updated only after the map is initialized, not earlier, not later.
How to pack it all into modules, think for yourself, please.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question