Skip to content

Commit

Permalink
Add transform for SetVisibleExtent & Add padding to SetZoomToExtent (#59
Browse files Browse the repository at this point in the history
) (#63)

* Fix transform for SetVisibleExtent. Add padding to SetZoomToExtent.

* fix null/nothing

---------

Co-authored-by: lolochristen <[email protected]>
  • Loading branch information
lolochristen and lolochristen authored Jul 6, 2024
1 parent de8ec05 commit 6463da1
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
<h3 class="card-title">OpenStreetMap</h3>
</div>
<div class="card-body">
<div class="btn-toolbar gap-1" role="toolbar">
<button class="btn btn-secondary" @onclick="SetVisibleExtent">Adelboden</button>
<button class="btn btn-secondary" @onclick="ZoomToMarkers">Zoom to Markers</button>
</div>
<p>Demo with a loaded KML Layer on first map, custom WMS source on second map, synced center and a Graticule Layer on second map.</p>
<pre>
Center: @_map?.Center
Expand All @@ -22,6 +26,10 @@ Zoom: @_map?.Zoom
FormatOptions="@_kmlOptions">
</Layer>
</Layers>
<Features>
<Marker Type="MarkerType.MarkerPin" Coordinate="new Coordinate(x, y)"></Marker>
<Marker Type="MarkerType.MarkerPin" Coordinate="new Coordinate(x+2, y+2)"></Marker>
</Features>
</OpenStreetMap>

<OpenStreetMap Style="height:480px" Class="card" @bind-Center="_center" ScaleLineUnit="ScaleLineUnit.US">
Expand Down Expand Up @@ -55,4 +63,14 @@ Zoom: @_map?.Zoom
//private OpenStreetMap _map;
private Map _map;
private dynamic _kmlOptions = new { ShowPointNames = true, ExtractStyles = true };

private async Task SetVisibleExtent()
{
await _map.SetVisibleExtent(new Extent(7.2797221255061775, 46.325069369932436, 7.852244417871202, 46.59670324642207));
}

private async Task ZoomToMarkers()
{
await _map.SetZoomToExtent(ExtentType.Markers);
}
}
6 changes: 6 additions & 0 deletions src/OpenLayers.Blazor.Demo.Components/Pages/ShapesDemo.razor
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<div class="card-body">
<button class="btn btn-primary" @onclick="AddShapes">Add some shapes</button>
<button class="btn btn-primary" @onclick="RemoveShape">Remove the last</button>
<button class="btn btn-primary" @onclick="ZoomToShapes">Zoom to Shapes</button>
<p>
<code>@_featureInfo</code>
</p>
Expand Down Expand Up @@ -61,4 +62,9 @@
{
_map.ShapesList.Remove(_map.ShapesList.Last());
}

private async Task ZoomToShapes()
{
await _map.SetZoomToExtent(ExtentType.Geometries);
}
}
5 changes: 3 additions & 2 deletions src/OpenLayers.Blazor/Map.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -552,10 +552,11 @@ public async Task SetZoom(double zoom)
/// Zooms to the given extent
/// </summary>
/// <param name="extent"></param>
/// <param name="padding">Padding (in pixels) to be cleared inside the view. Values in the array are top, right, bottom and left padding. defaults to [0,0,0,0]</param>
/// <returns></returns>
public ValueTask SetZoomToExtent(ExtentType extent)
public ValueTask SetZoomToExtent(ExtentType extent, decimal[]? padding = null)
{
return _module?.InvokeVoidAsync("MapOLZoomToExtent", _mapId, extent.ToString()) ?? ValueTask.CompletedTask;
return _module?.InvokeVoidAsync("MapOLZoomToExtent", _mapId, extent.ToString(), padding) ?? ValueTask.CompletedTask;
}

/// <summary>
Expand Down
18 changes: 11 additions & 7 deletions src/OpenLayers.Blazor/wwwroot/openlayers_interop.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ export function MapOLLoadGeoJson(mapId, json, dataProjection, raiseEvents) {
_MapOL[mapId].loadGeoJson(json, dataProjection, raiseEvents);
}

export function MapOLZoomToExtent(mapId, extent) {
_MapOL[mapId].setZoomToExtent(extent);
export function MapOLZoomToExtent(mapId, extentType, padding) {
_MapOL[mapId].setZoomToExtent(extentType, padding);
}

export function MapOLMarkers(mapId, markers) {
Expand Down Expand Up @@ -498,18 +498,20 @@ MapOL.prototype.setZoom = function (zoom) {
this.Map.getView().setZoom(zoom);
};

MapOL.prototype.setZoomToExtent = function (extent) {
switch (extent) {
MapOL.prototype.setZoomToExtent = function (extentType, padding) {
if (padding == null)
padding = undefined;
switch (extentType) {
case "Markers":
var extent = this.Markers.getSource().getExtent();
if (extent[0] === Infinity) return;
this.Map.getView().fit(extent, this.Map.getSize());
this.Map.getView().fit(extent, { size: this.Map.getSize(), padding:padding });
break;

case "Geometries":
var extent = this.Geometries.getSource().getExtent();
if (extent[0] === Infinity) return;
this.Map.getView().fit(extent, this.Map.getSize());
this.Map.getView().fit(extent, { size: this.Map.getSize(), padding:padding });
break;
}
};
Expand Down Expand Up @@ -678,7 +680,9 @@ MapOL.prototype.disableVisibleExtentChranged = false;

MapOL.prototype.setVisibleExtent = function (extent) {
this.disableVisibleExtentChanged = true;
this.Map.getView().fit(new Array(extent.x1, extent.y1, extent.x2, extent.y2), this.Map.getSize());
var viewProjection = this.Map.getView().getProjection();
const te = ol.proj.transformExtent(new Array(extent.x1, extent.y1, extent.x2, extent.y2), this.Options.coordinatesProjection, viewProjection);
this.Map.getView().fit(te, this.Map.getSize());
this.disableVisibleExtentChanged = false;
};

Expand Down

0 comments on commit 6463da1

Please sign in to comment.