H
H
hollanditkzn2017-10-16 16:55:11
Yii
hollanditkzn, 2017-10-16 16:55:11

How to get yandex map longitude and latitude data?

I don’t understand a little how to realize that the result obtained is to save only the longitude and latitude from the yandex map. My problem is that I can get data in js, but it’s difficult for me to transfer it to php and I don’t understand how to do it more conveniently.
How I implemented in the
view

<?= $form->field($model, 'to')->textInput(['maxlength' => true, 'id' => 'inputYandexMap']) ?>

In the controller
public function actionCreate()
    {
        $model = new Courier();
        if ($model->load(Yii::$app->request->post()) && $model->validate()) {
            $model->to = YandexMap::widget(['data' => $model->to]);
            $model->save()
            Yii::$app->session->addFlash('update', 'Доставка была назначена');
            return $this->redirect('shipping')
        }
        return $this->render('create', [
            'model' => $model,
        ]);
    }

In the YandexMap widget
class YandexMap extends Widget
{
    public $data = null;
    public function init()
    {
        parent::init();
    }

    public function run()
    {
        $this->view->registerJsFile('https://api-maps.yandex.ru/2.1/?lang=ru_RU', ['type' => 'text/javascript']);
        if ($this->data != null){
            $script = <<<JS
            let searchControl,
                suggestView;
            ymaps.ready(init);
            function init(){
    suggestView = new ymaps.SuggestView('inputYandexMap');
    searchControl = new ymaps.control.SearchControl({
        options: {
            float: 'left',
            floatIndex: 100,
            noPlacemark: true
        }
    });
        searchControl.search('.$this->data.').then(function () {
            let geoOjectsArray = searchControl.getResultsArray();
            if(geoOjectsArray.length){
                return geoOjectsArray[0].geometry.getCoordinates();
            }
        });
    }
JS;
        $this->view->registerJs($script);
        }
        $this->view->registerJsFile('@web/js/yandexMap.js');
    }
}

And in js file
let searchControl,
    suggestView;

ymaps.ready(init);

function init(){
    suggestView = new ymaps.SuggestView('inputYandexMap');
    searchControl = new ymaps.control.SearchControl({
        options: {
            float: 'left',
            floatIndex: 100,
            noPlacemark: true
        }
    });
    $('#inputYandexMap').on('change', function () {
        let value = $(this).val();
        console.log(value);
        searchControl.search(value).then(function () {
            let geoOjectsArray = searchControl.getResultsArray();
            if(geoOjectsArray.length){
                console.log(geoOjectsArray[0].geometry.getCoordinates());
            }
        });
    });
}

I just can’t understand how to implement this, and rather the code will seem like nonsense, but I would like a hint where to look and, of course, an example so that I don’t ask unnecessary questions.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Grechushnikov, 2017-10-16
@hollanditkzn

1. You have a hidden field and a form on your page. paste it there.
2. transfer via rest

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question