Comments (2)
Thanks a lot for providing a sample!
I could reproduce problematic behavior on Windows desktop. To circumvent caching I rewrote your sample to use a WritableLayer (derived from IModifyFeatureLayer) and to use a GeometryFeature to call IFeature.RenderedGeometry.Clear();
In v5 (not released yet) this was already changed. There you do not need to use a specific layer. You do need to call IFeature.Modified();
Note, that we have samples for editing. They need to be simplified some more though. Using your own code like this is not a bad idea (perhaps better) but the samples can help you find the difference.
using Mapsui;
using Mapsui.ArcGIS;
using Mapsui.ArcGIS.ImageServiceProvider;
using Mapsui.Cache;
using Mapsui.Extensions;
using Mapsui.Layers;
using Mapsui.Nts;
using Mapsui.Nts.Extensions;
using Mapsui.Providers;
using Mapsui.Styles;
using Mapsui.Tiling;
using Mapsui.UI.Maui;
using NetTopologySuite.Geometries;
using System.Diagnostics;
using Brush = Mapsui.Styles.Brush;
using Color = Mapsui.Styles.Color;
using Polygon = NetTopologySuite.Geometries.Polygon;
namespace MauiApp1
{
public partial class MainPage : ContentPage
{
GeometryFeature _draggedPolygon;
List<Coordinate> _draggedOrigCoords;
MPoint _touchStartedScreenPoint { get; set; }
ILayer _touchStartedLayer { get; set; }
GeometryFeature _polygonFeature;
MapControl _mapControl;
double x = -9188151.36056;
double y = 3235144.74039;
public MainPage()
{
InitializeComponent();
_mapControl = new Mapsui.UI.Maui.MapControl()
{
Map = new Mapsui.Map()
{
CRS = "EPSG:3857",
}
};
_mapControl.Map.Layers.Add(OpenStreetMap.CreateTileLayer());
_mapControl.Loaded += MapControl_Loaded;
_mapControl.TouchStarted += MapControl_TouchStarted;
_mapControl.TouchMove += MapControl_TouchMove;
_mapControl.TouchEnded += MapControl_TouchEnded;
var point = new MPoint(x, y);
_mapControl.Map.Home = (n) => n.CenterOnAndZoomTo(point, resolution: 1000, 500, Mapsui.Animations.Easing.CubicOut);
Content = _mapControl;
}
private void MapControl_TouchEnded(object sender, Mapsui.UI.TouchedEventArgs e)
{
_mapControl.Map.Navigator.PanLock = false;
_touchStartedScreenPoint = null;
ClearPolygonFields();
}
private void MapControl_TouchMove(object sender, Mapsui.UI.TouchedEventArgs e)
{
var point = e.ScreenPoints.First();
if (_touchStartedLayer?.Name == "Polygons")
{
MovePolygon(point, _polygonFeature);
}
}
private void MapControl_TouchStarted(object sender, Mapsui.UI.TouchedEventArgs e)
{
var mapInfo = _mapControl.GetMapInfo(e.ScreenPoints.FirstOrDefault());
_touchStartedLayer = mapInfo.Layer;
_touchStartedScreenPoint = e.ScreenPoints.First();
}
private void MapControl_Loaded(object sender, EventArgs e)
{
var baseCoordinate = new Coordinate(x, y);
_polygonFeature = CreatePolygon(baseCoordinate);
var polygonLayer = CreatePolygonLayer(_polygonFeature);
_mapControl.Map.Layers.Add(polygonLayer);
_mapControl.Refresh();
}
private void ClearPolygonFields()
{
Debug.WriteLine("Ended");
_draggedPolygon = null;
_draggedOrigCoords = null;
}
private void MovePolygon(MPoint screenPoint, GeometryFeature geometryFeature)
{
Debug.WriteLine($"Polygon Coord 0 X: {geometryFeature.Geometry.Coordinates.First().X}");
Debug.WriteLine($"Polygon Coord 0 Y: {geometryFeature.Geometry.Coordinates.First().Y}");
if (_draggedPolygon == null)
{
_mapControl.Map.Navigator.PanLock = true;
// set the dragged polygon
_draggedPolygon = geometryFeature;
_draggedOrigCoords = geometryFeature.Geometry.Coordinates.Select(r => new Coordinate(r.X, r.Y)).ToList();
}
else if (_touchStartedScreenPoint != null)
{
// move polygon with screen point
for (var i = 0; i < geometryFeature.Geometry.Coordinates.Length; i++)
{
var coordinate = geometryFeature.Geometry.Coordinates[i];
var originalCoordinate = _draggedOrigCoords[i];
var firstTouchedWorldPoint = _mapControl.Map.Navigator.Viewport.ScreenToWorld(_touchStartedScreenPoint);
var currentWorldPoint = _mapControl.Map.Navigator.Viewport.ScreenToWorld(screenPoint);
coordinate.X = Math.Round(currentWorldPoint.X - firstTouchedWorldPoint.X + originalCoordinate.X, 5, MidpointRounding.AwayFromZero);
coordinate.Y = Math.Round(currentWorldPoint.Y - firstTouchedWorldPoint.Y + originalCoordinate.Y, 5, MidpointRounding.AwayFromZero);
}
}
_draggedPolygon.RenderedGeometry.Clear();
_mapControl.Refresh();
}
private ILayer CreatePolygonLayer(GeometryFeature polygon)
{
var polygonList = new List<GeometryFeature> { { polygon } };
var layer = new WritableLayer()
{
Name = "Polygons",
Style = new VectorStyle
{
Fill = new Brush(Color.Orange),
Outline = new Pen
{
Color = Color.Orange,
Width = 1,
PenStyle = PenStyle.DashDotDot,
PenStrokeCap = PenStrokeCap.Round
}
},
Opacity = .3,
IsMapInfoLayer = true,
};
layer.AddRange(polygonList);
return layer;
}
private GeometryFeature CreatePolygon(Coordinate baseCoordinate)
{
var width = 250000;
var height = 250000;
var point = new MPoint(baseCoordinate.X, baseCoordinate.Y);
var topLeft = new Coordinate(point.X - (width / 2), point.Y - (height / 2));
var topRight = new Coordinate(point.X + (width / 2), point.Y - (height / 2));
var bottomRight = new Coordinate(point.X + (width / 2), point.Y + (height / 2));
var bottomLeft = new Coordinate(point.X - (width / 2), point.Y + (height / 2));
var ring = new LinearRing(new[] { topLeft, topRight, bottomRight, bottomLeft, topLeft });
var polygon = new Polygon(ring);
return new GeometryFeature(polygon);
}
}
}
from mapsui.
Great! Thank you for the resolution and you are quite welcome for the sample.
I've tested this code out and it is working as expected.
Looking forward to v5 :)
from mapsui.
Related Issues (20)
- How to modify ZOrder of IFeature ? HOT 4
- Animations freeze in WPF under heavy load. HOT 2
- How to use EditingWidget HOT 1
- Can use RasterizingLayer and open IsMapInfoLayer=true,performance degradation HOT 1
- Replacement of map.home function in 5.0 Beta HOT 3
- Maui performance issues under Android HOT 20
- Wrong attribution link after switching map layer HOT 2
- Performance collapse with many Refresh calls and zooming out HOT 2
- Layer is not updated after fetches because the Delayer hangs HOT 1
- Viewport.Rotation could get to 360 ° or more HOT 9
- Some of the Mapsui.Tests.Fetcher are not working HOT 10
- Improve RasterStyle HOT 4
- [Uno] Use SKSwapChainPanel whenever possible HOT 2
- In Map.DataChanged event, DataChangedEventArgs Layer name is empty. It should contains the Layer Name. HOT 3
- [UNO] MapInfo tapable area is off from symbol HOT 8
- Density Coordination HOT 2
- IDisposable guidelines HOT 1
- Map.Info eventargs return null for args.MapInfo.Feature when clicking on feature. HOT 2
- Removing items in ObservableCollection doesn't affect _shadowCollection HOT 1
- Dont cache BaseOnSourceLayers which use WritableLayers as their SourceLayer HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from mapsui.