E
E
exdude2020-03-03 10:05:45
JavaScript
exdude, 2020-03-03 10:05:45

How to fake yandex API location?

There is a code that determines the location of the site visitor, and output it to the created block:

ymaps.ready(init);
  function init(){
    ymaps.geolocation.get({
      provider: 'yandex',
      autoReverseGeocode: true
    }).then(function (result) {
      $('#urcity').html('Ваш город: <b style="margin-top: -50px;">'+result.geoObjects.get(0).properties.get('metaDataProperty.GeocoderMetaData.AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Locality.LocalityName')+'</b>');
    });
  }


As well as a map that also determines the user's location:

function init() {
    var geolocation = ymaps.geolocation,
        myMap = new ymaps.Map('map', {
            center: [55, 34],
            zoom: 10
        }, {
            searchControlProvider: 'yandex#search'
        });
    geolocation.get({
        provider: 'yandex',
        mapStateAutoApply: true
    }).then(function (result) {
        result.geoObjects.options.set('preset', 'islands#redCircleIcon');
        result.geoObjects.get(0).properties.set({
            balloonContentBody: 'Мое местоположение'
        });
        myMap.geoObjects.add(result.geoObjects);
    });

    geolocation.get({
        provider: 'browser',
        mapStateAutoApply: true
    }).then(function (result) {
        result.geoObjects.options.set('preset', 'islands#blueCircleIcon');
        myMap.geoObjects.add(result.geoObjects);
    });
}


Is it possible using a script to change the variable that defines the city, so that later both the map and the city would change to the one chosen by the visitor?
This functionality has already been implemented with crutches, by changing the city in the block itself, and creating a new map. Since there are many cities, and the code for 160,000 lines is not ice at all.
Therefore, I ask for help or hints on implementation.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
sinneren, 2020-03-03
@exdude

I may not understand the problem, but...
when initializing:
center: [55.74954, 37.621587],
or
ymaps.util.bounds.getCenterAndZoom(
,
[$mapElement.width (), $mapElement.height()]
)
or
center: res.geoObjects.get(0).geometry.getCoordinates(),
or after the fact:
setCenter(center[, zoom[, options]])
https://tech. yandex.ru/maps/archive/doc/jsapi/2.0/...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question