How best to highlight several areas on the Yandex. Maps?



  • I'm writing a small macro with API Yandex. Mapping version 1.1.
    In mine. https://ru.stackoverflow.com/questions/474546/%D0%AF%D0%BD%D0%B4%D0%B5%D0%BA%D1%81-%D0%9A%D0%B0%D1%80%D1%82%D1%8B-%D0%BF%D0%BE%D0%B4%D1%81%D0%B2%D0%B5%D1%82%D0%BA%D0%B0-%D1%80%D0%B5%D0%B3%D0%B8%D0%BE%D0%BD%D0%BE%D0%B2 I've been shown how to refresh the area all the time.

    How do you put a few areas on the map? Of course, we can create variables for each area, but what if we need to identify 20 areas with certain names?

    var regionVba = response.filter(function (obj) {
        return obj.name == "Московская область";
    })[0];
    var shapes = regionVba.metaDataProperty.encodedShapes;
    var polygon;
    for (var ix = shapes.length; ix--; ) {
        polygon = YMaps.Polygon.fromEncodedPoints(
            shapes[ix].coords, 
            shapes[ix].levels
        );
        polygon.setStyle({
            polygonStyle: {
                fillColor: "b00c0c55",
                strokeColor: "b00c0c"
            }
        });
        map.addOverlay(polygon);
    }
    

    regionVba1 = response.filter(function (obj) {
    return obj.name == "Владимирская область";
    })[0];
    shapes = regionVba1.metaDataProperty.encodedShapes;
    polygon;
    for (var ix = shapes.length; ix--; ) {
    polygon = YMaps.Polygon.fromEncodedPoints(
    shapes[ix].coords,
    shapes[ix].levels
    );
    polygon.setStyle({
    polygonStyle: {
    fillColor: "bccc0c55",
    strokeColor: "bccc0c"
    }
    });
    map.addOverlay(polygon);
    }

    Then this code will be cumbersome and inoptimate. Is there any way to say goodbye? For example, one day in 20 names, single out the aggregate area and light it up?



  • You don't have to do the same job twice. For your task, it's enough to fix the regional filter.

    regionVba = response.filter(function (obj) {
        var names = [
            "Владимирская область",
            "Московская область" // добавить нужный список
        ];
        return (names.indexOf(obj.name) > -1);
    });
    

    Then form polygones in the cycle on the elements of the mass regionVba

    PS. Please note that method Array.indexOf() I don't support old browsers. It will be necessary to invent or invent a bicycle or to take advantage of the solutions that are available on this site for sure.




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2