In the continuation of the question " How on the Yandex map to show areas of the city? " Not all regions are displayed this way. For example, the region is not displayed on the map:

<script> ymaps.ready(function() { // 0. Π‘ΠΎΠ·Π΄Π°Π΅ΠΌ ΠΊΠ°Ρ€Ρ‚Ρƒ, Π½Π°ΠΏΡ€ΠΈΠΌΠ΅Ρ€ Ρ‚Π°ΠΊ: var map, regionName = "Bryansk Oblast", center = [52.8873315,33.415853], zoom = 5; map = new ymaps.Map('reviewsmap', { center: center, zoom: zoom, controls: [] }); // 1. Π—Π°ΠΏΡ€Π°ΡˆΠΈΠ²Π°Π΅ΠΌ Ρ‡Π΅Ρ€Π΅Π· Π³Π΅ΠΎΠΊΠΎΠ΄Π΅Ρ€ Ρ€Π°ΠΉΠΎΠ½ (Ρƒ ЯндСкса этой возмоТности ΠΏΠΎΠΊΠ° Π½Π΅Ρ‚, придСтся ΠΏΠΎΠ»ΡŒΠ·ΠΎΠ²Π°Ρ‚ΡŒΡΡ OSM) var url = "http://nominatim.openstreetmap.org/search"; $.getJSON(url, {q: regionName, format: "json", polygon_geojson: 1}) .then(function (data) { $.each(data, function(ix, place) { if ("relation" == place.osm_type) { // 2. Π‘ΠΎΠ·Π΄Π°Π΅ΠΌ ΠΏΠΎΠ»ΠΈΠ³ΠΎΠ½ с Π½ΡƒΠΆΠ½Ρ‹ΠΌΠΈ ΠΊΠΎΠΎΡ€Π΄ΠΈΠ½Π°Ρ‚Π°ΠΌΠΈ var p = new ymaps.Polygon(place.geojson.coordinates); p.options.set('fillColor', 'rgba(255, 0, 0, 0.4)'); p.options.set('strokeColor', '#F8DA19'); // 3. ДобавляСм ΠΏΠΎΠ»ΠΈΠ³ΠΎΠ½ Π½Π° ΠΊΠ°Ρ€Ρ‚Ρƒ map.geoObjects.add(p); } }); }, function (err) { console.log(err); }); }); </script> 

And I can cite several such examples. What it depends on is not clear.

  • Have you watched what data you get from an openstreetmap search? - Ivan Pshenitsyn

1 answer 1

I took the Volgograd Oblast and Bryansk Oblast areas for example and looked at the data in response from http://nominatim.openstreetmap.org/search .

To draw a region, you give the map an array of points taken from geojson.coordinates .

The first region has geojson.type == "Polygon" , and inside the geojson.coordinates one array. The region is normally drawn on the map.

Bryansk Oblast has geojson.type == "MultiPolygon" , and inside geojson.coordinates two arrays. The region is not drawn on the map.

I am not familiar with api openstreetmap, but here we see that the search can return as the coordinates of a single drawing region, or several. And the map needs to give the coordinates of only one of them (or all, but in turn).

Therefore, I suggest you edit that will check type and if it is MultiPolygon - draw only the first array of points:

 var coordinates = place.geojson.type == "MultiPolygon" ? place.geojson.coordinates[0] : place.geojson.coordinates; var p = new ymaps.Polygon(coordinates); 

Once again: I am not familiar with api and do not know what the second region is in the case of Bryansk. I drew them both on the map and did not see any difference, so I suggest simply drawing the first one. In any case, if you study the api docks and understand that you need to draw all the regions, this is easy to do. Now you know where to dig.