H
H
hollanditkzn2017-10-17 10:45:43
Yii
hollanditkzn, 2017-10-17 10:45:43

How to display not the longitude and latitude, but the address?

How to make it so that to display in the gridview not the longitude and latitude, but the address, as I understand it, or to save the street and the house from the beginning, or is it possible somehow from the longitude and latitude to display the address?
How do I understand that it is better to look as I understand it on the rest api?
How I implemented
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']);
        $this->view->registerJsFile('@web/js/yandexMap.js');
        if($this->data != null){
            $script = <<<JS
let myGeocode;
yamps.ready(
    myGeocode = new yamps.geocode([$this->data]);
    myGeocode.then(
        function(res){
            return res.geoObjects.get(0).properties.get('name');
        },
        function(err){
            alert(err);
        }
    )
)
JS;

        }
    }
}

In gridview in attribute
[
                'attribute' => 'to',
                'format' => 'raw',
                'value' => function($courier){
                    return '<span class="shipping">Откуда: </span>'.\frontend\components\YandexMap::widget(['data' => $courier->to]) ;
                },
                'contentOptions' => ['class' => 'textTr tr202'],
            ],

If, of course, you need to look at the yandex.Map.js file itself, then here
let searchControl,
    suggestView;

ymaps.ready(init);

function init(){
    suggestView = new ymaps.SuggestView('toMap');
    suggestView = new ymaps.SuggestView('fromMap');
    searchControl = new ymaps.control.SearchControl({
        options: {
            float: 'left',
            floatIndex: 100,
            noPlacemark: true
        }
    });
    geoObject('#toMap', '#toInput');
    geoObject('#fromMap', '#fromInput');

    function geoObject(idInput, toInput){
        $(idInput).on('change', function () {
            let value = $(this).val();
            searchControl.search(value).then(function () {
                let geoOjectsArray = searchControl.getResultsArray();
                if(geoOjectsArray.length){
                    let coordinat = geoOjectsArray[0].geometry.getCoordinates();
                    $(toInput).val(coordinat);
                    console.log(coordinat);
                }
            });
        });
    }
}

And another question, if you do this, then it’s reasonable, because every request will need to be sent to api, which is better to get data from the database, and store the coordinates and the street in the database

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Micro Null, 2017-10-17
@hollanditkzn

There are several ways.
1. When creating a record, immediately save the address data, having previously received them from the Yandex API or another service.
2. If there is no address data by coordinates, then periodically fill them in when executing a certain script for the cron task. The script itself again accesses the Yandex API, receives the address and saves it to the database.
3. Have some kind of cartographic database that contains addresses and coordinates. With the help of a not tricky SQL query, you can get the address. The OSM database (OpenStreetMap) is suitable here.
In your case, it would be more correct to compare methods 1 and 2 together. Those. at the time of creating a record (for example, on an html form), request coordinates from the user and fill in the address field using them. If an error occurs, then we save only the coordinates, then fill them in by the crown.
The third method is fraught with the fact that the OSM base is very large and not all data are correct. But it's free. With a large number of requests, the Yandex API is blocked and requires money.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question