M
M
mr.bob2017-08-21 13:11:00
JavaScript
mr.bob, 2017-08-21 13:11:00

Determining the entry of a point into the polygon of a Yandex map. Can you help?

I draw a map on page load, also a polygon.
Polygon border of the city.
When I click, I want to determine whether the point is in the city or not.
I racked my brain and could not find a solution, please help.

ymaps.ready( init );

    function init(){

        myMap = new ymaps.Map( "checkout-map", {
            center: [ 43.24, 76.91 ],
            controls: ['zoomControl'],
            zoom: 15
        },
        {suppressMapOpenBlock: true});

        var myPolygon = new ymaps.geometry.Polygon(
                [
                    [76.780685,43.333234],
                    [76.80077,43.331605],
                    [76.804632,43.356099],
                    [76.849135,43.355566],
                    [76.855937,43.336508],
                    [76.866205,43.350532],
                    [76.883698,43.341759],
                    [76.879742,43.330605],
                    [76.937845,43.382899],
                    [76.96312,43.379737],
                    [76.983998,43.405447],
                    [77.004736,43.401277],
                    [77.020942,43.385172],
                    [77.018058,43.36259],
                    [77.035087,43.361046],
                    [77.067703,43.375198],
                    [77.086071,43.373257],
                    [77.037577,43.342726],
                    [77.043713,43.334973],
                    [76.995605,43.290113],
                    [77.01569,43.286248],
                    [77.019552,43.261213],
                    [77.068175,43.218318],
                    [77.106906,43.177515],
                    [77.123182,43.188262],
                    [77.151232,43.184591],
                    [77.157705,43.173207],
                    [77.149611,43.15897],
                    [77.104366,43.148331],
                    [77.072473,43.157841],
                    [77.07232,43.145001],
                    [77.115845,43.116199],
                    [77.110828,43.110346],
                    [77.091497,43.116977],
                    [77.083205,43.111741],
                    [77.085213,43.078324],
                    [77.091392,43.050127],
                    [77.037147,43.03552],
                    [76.982216,43.041815],
                    [76.939987,43.036148],
                    [76.938785,43.049179],
                    [76.968397,43.070546],
                    [76.953334,43.07871],
                    [76.901685,43.07801],
                    [76.888907,43.130238],
                    [76.866554,43.126039],
                    [76.859153,43.142044],
                    [76.793388,43.143465],
                    [76.776308,43.159263],
                    [76.789451,43.170096],
                    [76.786752,43.189079],
                    [76.779934,43.194242],
                    [76.768454,43.215141],
                    [76.768991,43.236032],
                    [76.769527,43.248886],
                    [76.782659,43.254197],
                    [76.789912,43.279935],
                    [76.754056,43.29305],
                    [76.737502,43.315903],
                    [76.753791,43.318858],
                    [76.75978,43.314793],
                    [76.780704,43.321508]
                ]

        );

        myPolygon.options.setParent(myMap.options);
        myPolygon.setMap(myMap);

        // Слушаем клик на карте.
        myMap.events.add( $clickAction , function (e) {
            var coords = e.get('coords');

            // Если метка уже создана – просто передвигаем ее.
            if (myPlacemark) {
                myPlacemark.geometry.setCoordinates(coords);
                myPlacemark.options.set('visible', true);
            }
            // Если нет – создаем.
            else {
                myPlacemark = createPlacemark(coords);
                myMap.geoObjects.add( myPlacemark );
                // Слушаем событие окончания перетаскивания на метке.
                myPlacemark.events.add('dragend', function () {
                    getAddress(myPlacemark.geometry.getCoordinates());
                });
            }
            getAddress(coords);

            alert(myPolygon.geometry.contains( coords ) ? 'город' : 'загород');

        });

    }

Answer the question

In order to leave comments, you need to log in

3 answer(s)
E
Egor Kazantsev, 2017-08-21
@saintbyte

Postgres + Postgis and no Yandex.

A
Alexander Pushkarev, 2017-08-21
@AXP-dev

Did you watch it? https://tech.yandex.ru/maps/jsbox/2.1/placemarks_i...

M
mr.bob, 2017-08-21
@kzk8888

found the solution myself. My polygon coordinates were incorrect. The script itself is working.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question