You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
feature #2162 [Map] Adding polygons to google and leaflet + info window (rrr63)
This PR was merged into the 2.x branch.
Discussion
----------
[Map] Adding polygons to google and leaflet + info window
| Q | A
| ------------- | ---
| Bug fix? | no
| New feature? | yes
| Issues |
| License | MIT
Hello,
In my job i use to work a lot with maps, so i would love to work with symfony ux in next projects.
But i have to work with polygons, so i tried to implement it to the ux-map.
I added polygons on the Map component, and made it work on both bridge : google and leaflet.
I added a window popup on click also, the current version of window popup worked only with markers.
I had to rename `createInfoWindow` to `createInfoWindowMarker`. With that i could add a `createInfoWindowPolygon`.
The only difference with the infoWindow from marker is that his anchor will be the event cursor position, because the area of the polygon could be very large and we want a window on the click position.
You can try by adding a simple polygon on your map :
```php
->addPolygon(new Polygon(
points: [
new Point(45.7402741, 3.191185),
new Point(45.7314078, 3.1903267),
new Point(45.7307488, 3.208952),
new Point(45.7402741, 3.2112694)
],
rawOptions:[
'fillColor' => 'green',
'color' => 'green',
],
infoWindow: new InfoWindow(
headerContent: '<b>Polygon</b>',
content: 'I am a polygon'
)
))
```

Here a full example that works with leaflet and google that show some of french regions via an api call
```php
$polygons = json_decode(file_get_contents('api_url'), true);
$polygons = $polygons['features'];
$googleOptions = (new GoogleOptions())
->mapId('mapId')
;
$map = (new Map('default', $googleOptions))
->center(new Point(45.8079318, 3))
->zoom(10)
;
foreach ($polygons as $polygon) {
$points = [];
$title = $polygon['properties']['nom'];
foreach ($polygon['geometry']['coordinates'][0] as $point) {
if (count($point) == 2) {
$points[] = new Point($point[1], $point[0]);
}
}
$map->addPolygon(new Polygon(
points: $points,
rawOptions:[
'fillColor' => 'red',
'color' => 'red',
],
infoWindow: new InfoWindow(
headerContent: '<b>'.$title.'</b>',
content: 'The French region of '.$title.'.'
)
));
}
```


I added tests for polygons and polygon info window
I hope I did it right, i listen for any suggestions or edits in the PR
Commits
-------
2f63db7 [Map] Add support for Polygons
0 commit comments