M
M
Maxim Timofeev2016-07-21 14:00:47
JavaScript
Maxim Timofeev, 2016-07-21 14:00:47

How to get user coordinates in yandex maps?

There is a code:

ymaps.ready(init);

function init() {
    var myMap = new ymaps.Map(\"map\", {
            center: [".$this->coords."],
            zoom: ".$this->zoom."
        });
    var geolocation = ymaps.geolocation,
        coords = [geolocation.latitude, geolocation.longitude];
        
        //кнопка
    var myButton = new ymaps.control.Button('Проложить маршрут');
myButton.events
    .add('click', function () {
            ymaps.route([
        [".$this->coords."],
        coords,
    ]).then(function (route) {
        myMap.geoObjects.add(route);
        var points = route.getWayPoints(),
            lastPoint = points.getLength() - 1;
        points.options.set('preset', 'twirl#redStretchyIcon');
        points.get(0).properties.set('iconContent', 'Точка отправления');
        points.get(lastPoint).properties.set('iconContent', 'Точка прибытия');

    }, function (error) {
        alert('Возникла ошибка: ' + error.message);
    });
    });
myMap.controls.add(myButton);

}

gives an Internal Server Error when you click on the "lay route" button.
Everything works if you do not determine the position of the user, but insert any coordinates. Accordingly, I'm stupid here:
var geolocation = ymaps.geolocation,
        coords = [geolocation.latitude, geolocation.longitude];

What's wrong here? How to get the user's coordinates into the coords variable.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
freeExec, 2016-07-22
@freeExec

Вы хотя бы пример из документации смотрели? Найдите пять отличий. Функция не мгновенная, нужен callback.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question