Answer the question
In order to leave comments, you need to log in
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']) ?>
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,
]);
}
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');
}
}
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());
}
});
});
}
Answer the question
In order to leave comments, you need to log in
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 questionAsk a Question
731 491 924 answers to any question