After getting position from Google map Api and looking to create polyline between two points but problem is Polyline->GeoPath is readonly.
How to assign Positions to GeoPath or bind position directly to map?
I am using following this sample
Map map = new Map
{
// ...
};
// instantiate a polyline
Polyline polyline = new Polyline
{
StrokeColor = Color.Blue,
StrokeWidth = 12,
Geopath =
{
new Position(47.6381401, -122.1317367),
new Position(47.6381473, -122.1350841),
new Position(47.6382847, -122.1353094),
new Position(47.6384582, -122.1354703),
new Position(47.6401136, -122.1360819),
new Position(47.6403883, -122.1364681),
new Position(47.6407426, -122.1377019),
new Position(47.6412558, -122.1404056),
new Position(47.6414148, -122.1418647),
new Position(47.6414654, -122.1432702)
}
};
// add the polyline to the map's MapElements collection
map.MapElements.Add(polyline);
Polyline -> Geopath -> Add will solve the issue
Polyline polyline = new Polyline
{
StrokeColor = Color.Blue,
StrokeWidth = 12
}
polyline.GeoPath.Add(new Position)
Related
I created a WPF Bing map and added polyline I would like set the center and zoom level, which fit polyline. Like map.fitBounds(bounds).
MapPolyline polyline = new MapPolyline();
polyline.Stroke = new System.Windows.Media.SolidColorBrush(System.Windows.Media.Colors.Blue);
polyline.Locations = new LocationCollection() {
new Location(47.6424, ,-122.3219),
new Location(47.8424,-122.1747),
new Location(47.67856,-122.130994)};
myMap.Children.Add(polyline);
You can get an IEnumerable<Location> from LocationCollection of your polyline and then use an overload of SetView to zoom to the locations. This overload allows you to set a margin as well.
myMap.SetView(polyline.Locations.Cast<Location>(),
new System.Windows.Thickness(0), 0);
Or you can create a LocationRect from LocationCollection of your polyline and then use another overload of SetView to zoom to the rectangle.
myMap.SetView(new LocationRect(polyline.Locations));
Exampel 1 - IEnumerable<Location>
MapPolyline polyline = new MapPolyline();
polyline.Stroke = new SolidColorBrush(Colors.Blue);
polyline.Locations = new LocationCollection() {
new Location(47.6424, -122.3219),
new Location(47.8424,-122.1747),
new Location(47.67856,-122.130994)};
myMap.Children.Add(polyline);
myMap.SetView(polyline.Locations.Cast<Location>(),
new System.Windows.Thickness(0), 0);
Example 2 - LocationRect
MapPolyline polyline = new MapPolyline();
polyline.Stroke = new SolidColorBrush(Colors.Blue);
polyline.Locations = new LocationCollection() {
new Location(47.6424, -122.3219),
new Location(47.8424,-122.1747),
new Location(47.67856,-122.130994)};
myMap.Children.Add(polyline);
myMap.SetView(new LocationRect(polyline.Locations));
I have a project that I need to make an image follow a spline.
I build the spline using Graphics.DrawCurve through an array of Points.
I'm trying to use PointAnimationUsingPath but I can't seem to get it to work. Apparently it doesn't work in C# with Windows form.
Can someone give me a light on how to do this?
Thank you All.
-----EDIT-----
Change to a WPF UserControl as recommend in comments.
Still need some help as the shape does not move exactly following the dots, below my code:
public partial class SplineBox : UserControl
{
Point[] finalPoint;
public SplineBox()
{
InitializeComponent();
}
public void MoveShape(Point[] _path)
{
// Create a NameScope for the page so that
// we can use Storyboards.
NameScope.SetNameScope(this, new NameScope());
// Create the EllipseGeometry to animate.
EllipseGeometry animatedEllipseGeometry =
new EllipseGeometry(new Point(10, 100), 15, 15);
// Register the EllipseGeometry's name with
// the page so that it can be targeted by a
// storyboard.
this.RegisterName("AnimatedEllipseGeometry", animatedEllipseGeometry);
// Create a Path element to display the geometry.
Path ellipsePath = new Path();
ellipsePath.Data = animatedEllipseGeometry;
ellipsePath.Fill = Brushes.Blue;
ellipsePath.Margin = new Thickness(15);
SplineCanvas.Children.Add(ellipsePath);
this.Content = SplineCanvas;
// Create the animation path.
PathGeometry animationPath = new PathGeometry();
PathFigure pFigure = new PathFigure();
pFigure.StartPoint = _path[0];
PolyBezierSegment pBezierSegment = new PolyBezierSegment();
for (int p = 1; p < _path.Length; p++)
{
pBezierSegment.Points.Add(_path[p]);
}
pFigure.Segments.Add(pBezierSegment);
animationPath.Figures.Add(pFigure);
// Freeze the PathGeometry for performance benefits.
animationPath.Freeze();
// Create a PointAnimationgUsingPath to move
// the EllipseGeometry along the animation path.
PointAnimationUsingPath centerPointAnimation = new PointAnimationUsingPath();
centerPointAnimation.PathGeometry = animationPath;
centerPointAnimation.Duration = TimeSpan.FromSeconds(5);
centerPointAnimation.RepeatBehavior = RepeatBehavior.Forever;
// Set the animation to target the Center property
// of the EllipseGeometry named "AnimatedEllipseGeometry".
Storyboard.SetTargetName(centerPointAnimation, "AnimatedEllipseGeometry");
Storyboard.SetTargetProperty(centerPointAnimation,
new PropertyPath(EllipseGeometry.CenterProperty));
// Create a Storyboard to contain and apply the animation.
Storyboard pathAnimationStoryboard = new Storyboard();
pathAnimationStoryboard.RepeatBehavior = RepeatBehavior.Forever;
pathAnimationStoryboard.AutoReverse = true;
pathAnimationStoryboard.Children.Add(centerPointAnimation);
// Start the Storyboard when ellipsePath is loaded.
ellipsePath.Loaded += delegate (object sender, RoutedEventArgs e)
{
// Start the storyboard.
pathAnimationStoryboard.Begin(this);
};
}
public void Paint(ScreenObject _spline)
{
List<Point> points = new List<Point>();
if (true)
{
var spline = _spline;
foreach (System.Windows.Point point in spline.SplineAnchors)
{
Point tempP = new Point((int)point.X, (int)point.Y);
points.Add(tempP);
}
finalPoint = points.ToArray();
//Pen pen = new Pen(Color.FromArgb(255, 0, 0, 255), 1);
//e.Graphics.DrawCurve(pen, finalPoint);
foreach (Point p in finalPoint)
{
// Create a red Ellipse.
Ellipse myEllipse = new Ellipse();
// Create a SolidColorBrush with a red color to fill the
// Ellipse with.
SolidColorBrush mySolidColorBrush = new SolidColorBrush();
// Describes the brush's color using RGB values.
// Each value has a range of 0-255.
mySolidColorBrush.Color = Color.FromArgb(255, 100, 255, 0);
myEllipse.Fill = mySolidColorBrush;
myEllipse.StrokeThickness = 2;
myEllipse.Stroke = Brushes.Black;
// Set the width and height of the Ellipse.
myEllipse.Width = 10;
myEllipse.Height = 10;
myEllipse.Margin = new Thickness(p.X - 5, p.Y - 5, 0, 0);
//e.Graphics.DrawRectangle(pen, new Rectangle(p.X - 5, p.Y - 5, 10, 10));
//e.Graphics.FillRectangle(Brushes.Red, new Rectangle(p.X - 5, p.Y - 5, 10, 10));
SplineCanvas.Children.Add(myEllipse);
}
}
}
}
I am trying to add a polygon to my map by using the following code example (retrieved from https://learn.microsoft.com/en-us/windows/uwp/maps-and-location/display-poi#add-a-shape):
public void HighlightArea() {
double centerLatitude = myMap.Center.Position.Latitude;
double centerLongitude = myMap.Center.Position.Longitude;
var mapPolygon = new MapPolygon
{
Path = new Geopath(new List<BasicGeoposition> {
new BasicGeoposition() {Latitude=centerLatitude+0.0005, Longitude=centerLongitude-0.001 },
new BasicGeoposition() {Latitude=centerLatitude-0.0005, Longitude=centerLongitude-0.001 },
new BasicGeoposition() {Latitude=centerLatitude-0.0005, Longitude=centerLongitude+0.001 },
new BasicGeoposition() {Latitude=centerLatitude+0.0005, Longitude=centerLongitude+0.001 },
}),
ZIndex = 1,
FillColor = Colors.Red,
StrokeColor = Colors.Blue,
StrokeThickness = 3,
StrokeDashed = false,
};
// Add MapPolygon to a layer on the map control.
var MyHighlights = new List<MapElement>();
MyHighlights.Add(mapPolygon);
var HighlightsLayer = new MapElementsLayer
{
ZIndex = 1,
MapElements = MyHighlights
};
myMap.Layers.Add(HighlightsLayer);
}
The polygon is shown on the map initially, but it disappears within 2 seconds from when it appeared (it fades away). I have successfully added a MapPolyline that sticks to the map by using an example I retrieved from the same link, which is why I cannot understand why the MapPolygon will not stick.
What may cause the polygon to fade away?
I'm playing with this library because it seems quite useful for app developing that involves locations.
Q1: I was able to implement this in code-behind but I'm not able to transfer this to MVVM architecture. How can I do it?
Q2: if you see my code you can notice that I'm trying to use markers but for some reason I am only able to use marker.Shape (rectangle, ellipse, wtv). I want to use the standard ones from the GoogleMaps. Gmarkergoogle doesn't seems to exists anymore in GMap API. How can I do it? There is some other library to use? Need a little help over here as well.
OverviewView CODE
public partial class OverviewView : UserControl
{
public OverviewView()
{
InitializeComponent();
// Initialize map:
myMap.MapProvider = GoogleMapProvider.Instance;
GMaps.Instance.Mode = AccessMode.ServerOnly; // get tiles from server only
PointLatLng point = new PointLatLng(42.742826, -77.030212);
PointLatLng point1 = new PointLatLng(41.742826, -77.030212);
//Not use proxy
//Center map on a point
//GMapMarker marker = new GMapMarker(point);
var marker = new GMapMarker(point);
var marker1 = new GMapMarker(point1);
marker.Shape = new Rectangle
{
Width = 1,
Height = 100,
Stroke = Brushes.Black,
StrokeThickness = 1.5
};
marker1.Shape = new Rectangle
{
Width = 1,
Height = 100,
Stroke = Brushes.Black,
StrokeThickness = 1.5
};
myMap.Markers.Add(marker);
myMap.Markers.Add(marker1);
myMap.ZoomAndCenterMarkers(5);
myMap.Position = point;
/* GMapOverlay markersOverlay = new GMapOverlay("markers");
GMarkerGoogle marker = new GMarkerGoogle(new PointLatLng(-25.966688, 32.580528),
GMarkerGoogleType.green);
markersOverlay.Markers.Add(marker);
gmap.Overlays.Add(markersOverlay);*/
}
}
Best regards and thanks in advance for the time,
Antoine
I have 4 float points (positions in 2D) and I want to draw and fill a polygon (with vertices at those 4 positions) in 2D.
How can I do that with sharpdx?
I have found how to draw and fill a polygon. I have used drawGeometry and fillGeometry methods of RenderTarget object.
I wont mention how to initialize the directx
PathGeometry geo1;
GeometrySink sink1;
FactoryD2D factory = new FactoryD2D();
var dpi = factory.DesktopDpi;
RenderTarget renderTarget;
renderTarget = new RenderTarget(factory, backBuffer, new RenderTargetProperties()
{
DpiX = dpi.Width,
DpiY = dpi.Height,
MinLevel = SharpDX.Direct2D1.FeatureLevel.Level_DEFAULT,
PixelFormat = new SharpDX.Direct2D1.PixelFormat(Format.Unknown, AlphaMode.Ignore),
Type = RenderTargetType.Hardware,
Usage = RenderTargetUsage.None
});
// and after all initialization
pta[0] = new SharpDX.Vector2(pts[4].X, pts[4].Y);
pta[1] = new SharpDX.Vector2(pts[5].X, pts[5].Y);
pta[2] = new SharpDX.Vector2(pts[6].X, pts[6].Y);
pta[3] = new SharpDX.Vector2(pts[7].X, pts[7].Y);
geo1 = new PathGeometry(factory);
sink1 = geo1.Open();
sink1.BeginFigure(pta[0], new FigureBegin());
sink1.AddLines(new SharpDX.Vector2[] { pta[1], pta[2], pta[3] });
sink1.EndFigure(new FigureEnd());
sink1.Close();
Color penColor = Color.Black;
SolidColorBrush penBrush = new SolidColorBrush(g, new SharpDX.Color(penColor.R, penColor.G, penColor.B));
Color color = AddColor(pt, zmin, zmax);
SolidColorBrush aBrush = new SolidColorBrush(g, new SharpDX.Color(color.R, color.G, color.B));
renderTarget.DrawGeometry(geo1, penBrush);
renderTarget.FillGeometry(geo1, aBrush);
geo1.Dispose();
sink1.Dispose();
You can add other geos and sinks in order to add more shapes on to it
Hope this help to somebody who is lost and trying to find an answer desperately like me