No line between points in Gmap.Net route - c#

I'm using Gmap.Net on windows form, I want to draw track of an object when I receive its position, I use Routes for this. When I add points to a route, no line is seen on the map, but when I change the zoom of the map, they appear on the map. Also when I set the position of the map after adding a point to the route (gMapControl1.Position = new PointLatLng(...)), it works correctly and I see the route lines on the map, any idea? My code is like below.
void NewDataReceived(DeviceInfo deviceinf)
{
//---some codes
//----For the first time I add layer and route
if (deviceOverLay == null)
{
deviceOverLay = new GMapOverlay(deviceinf.DeviceId.ToString());
gMapControl1.Overlays.Add(deviceOverLay);
deviceRoute = new GMapRoute(new List<PointLatLng>(), deviceinf.DeviceName);
deviceOverLay.Routes.Add(deviceRoute);
//Add all your points here
deviceRoute.Points.Add(new PointLatLng(deviceinf.Latitude, deviceinf.Longitude));
deviceRoute.Tag = deviceinf;
}
else
{
deviceOverLay.Routes[0].Points.Add(new PointLatLng(deviceinf.Latitude, deviceinf.Longitude));
}
//if I call this line it works, but I don't want it
// gMapControl1.Position = new PointLatLng(deviceinf.Latitude, deviceinf.Longitude);
//---some codes
}

Try using
gMapControl1.UpdateRouteLocalPosition(deviceRoute);
This updates the local positions and does a redraw.

Related

Retrieve interval of data currently shown

I'm using OxyPlot to show a data chart and to allow the user to select an interval of data he wants to do calculation on.
It looks like this:
Now, I would like the user to be able to set the data interval used for calculation himself by resizing the chart. For example, if he resized the chart on this particular interval it would only take the points located between the furthest left and the furthest right on screen.
I already found the event that triggers whenever the chart is resized :
plotModel.Updated += (s, e) =>
{
//reset interval used for calculation
};
What I couldn't find in OxyPlot documentation is a way to retrieve a certain set of points currently shown. It doesn't need to be points in particular, I could also use only the x components of each extremum.
You could use Series.GetScreenRectangle().Contains() method after transforming your points using the Transform() method to detect if the point is currently in display.
For example,
model.Updated += (s,e)=>
{
if(s is PlotModel plotModel)
{
var series = plotModel.Series.OfType<OxyPlot.Series.LineSeries>().Single();
var pointCurrentlyInDisplay = new List<DataPoint>();
foreach (var point in series.ItemsSource.OfType<DataPoint>())
{
if (series.GetScreenRectangle().Contains(series.Transform(point)))
{
pointCurrentlyInDisplay.Add(point);
}
}
}
};
You are iterating over the point collection and verifying if the Transformed point (Transform method transform DataPoint to screen point) falls within the Screen Rectangle used by the series.
Update
If you have used Series.Points.AddRange()/Add() for adding points instead of Series.ItemSource, use the following for retrieving points.
foreach (var point in series.Points.OfType<DataPoint>())
{
if (series.GetScreenRectangle().Contains(series.Transform(point)))
{
pointCurrentlyInDisplay.Add(point);
}
}

How to render image of a country on a globe using sharpmap

I have a list of region borders inside SQL database and i am using sharpmap to render thumbnail images for every country i need. It works really well.
But I would like to go one step further and add a small globe around it and position country on it's place in globe, but I do not know where to start.
Here's the code i'm using so far to render country thumbs. Any ideas?
var map = new Map(new Size(command.Width, command.Height));
map.BackColor = Color.Transparent;
var countryGeometry = GeometryFromWKT.Parse(command.CountryLevelWkt);
IProvider countryProvider = new GeometryFeatureProvider(countryGeometry);
var countryLayer = new VectorLayer("country", countryProvider);
var borderColor = System.Drawing.ColorTranslator.FromHtml(command.BorderColor);
countryLayer.Style.EnableOutline = true;
countryLayer.Style.Outline = new Pen(borderColor);
countryLayer.Style.Outline.Width = command.BorderWidth;
countryLayer.Style.Fill = Brushes.Transparent;
var transformationFactory = new CoordinateTransformationFactory();
countryLayer.CoordinateTransformation = transformationFactory.CreateFromCoordinateSystems(
GeographicCoordinateSystem.WGS84,
ProjectedCoordinateSystem.WebMercator);
map.Layers.Add(countryLayer);
var bottomLeft = new Coordinate(command.Extents.BottomLeft.Longitude, command.Extents.BottomLeft.Latitude);
var topRight = new Coordinate(command.Extents.TopRight.Longitude, command.Extents.TopRight.Latitude);
// transformations
var bottomLeftLongLat = countryLayer.CoordinateTransformation.MathTransform.Transform(bottomLeft);
var topRightLongLat = countryLayer.CoordinateTransformation.MathTransform.Transform(topRight);
map.ZoomToBox(new Envelope(bottomLeftLongLat, topRightLongLat));
var img = map.GetMap();
return img;
Start by drawing all countries on a new map, each on its own layer.
Draw the country in which you're interested on its own layer.
Set map center to the Envelope.Center of the layer from Step 2. Eg, if drawing Australia, the map will move to the left.
Render map as image. Draw image on a drawing sufrace (System.Drawing.Graphics).
Re-center the map, to cover the empty space. Eg, if drawing Australia, move map almost all the way to the right. You will need to work out these offsets programmatically.
Render map from step 5 as image. Add image to the same drawing sufrace (see step 4).
Repeat steps 5-6 covering empty space below/above the render made at step 3.
Here is an example:
Note that:
Australia is in the center
There is a gap between map layers near the mouse pointer (gap in screenshot is intentional to demonstrate the logic)
Some countries are very large (eg Russia) and obtaining Envelope.Center will not work very well - consider centering based on the largest polygon only
Here's a sample Windows Forms project. In the sample, I used maps from http://thematicmapping.org/downloads/world_borders.php.

Xamarin - iOS Multiple polygons on map

I am currently following this tutorial for adding a polygon to a map. I need to be able to add multiple polygons to my map, so I have slightly altered the code so that I can use addOverlays which takes in an array of IMKOverlay objects instead of one addOverlay which just takes in a single IMKOverlay object.
This doesn't work however... It only draws the first polygon on the map!
void addPolygonsToMap()
{
overlayList = new List<IMKOverlay>();
for (int i = 0; i < polygons.Count; i++)
{
CLLocationCoordinate2D[] coords = new CLLocationCoordinate2D[polygons[i].Count];
int index=0;
foreach (var position in polygons[i])
{
coords[index] = new CLLocationCoordinate2D(position.Latitude, position.Longitude);
index++;
}
var blockOverlay = MKPolygon.FromCoordinates(coords);
overlayList.Add(blockOverlay);
}
IMKOverlay[] imko = overlayList.ToArray();
nativeMap.AddOverlays(imko);
}
In this discussion, it would appear that I have to call a new instance of MKPolygonRenderer each time I need to add another polygon to my map, but I'm unsure how this example translates to my code. Here is my MKPolygonRenderer function:
MKOverlayRenderer GetOverlayRenderer(MKMapView mapView, IMKOverlay overlayWrapper)
{
if (polygonRenderer == null && !Equals(overlayWrapper, null)) {
var overlay = Runtime.GetNSObject(overlayWrapper.Handle) as IMKOverlay;
polygonRenderer = new MKPolygonRenderer(overlay as MKPolygon) {
FillColor = UIColor.Red,
StrokeColor = UIColor.Blue,
Alpha = 0.4f,
LineWidth = 9
};
}
return polygonRenderer;
}
Create a new renderer instance each time OverlayRenderer is called, there is no need to cache the renderer in a class level variable as the MKMapView will cache the renderers as needed.
Subclass MKMapViewDelegate:
class MyMapDelegate : MKMapViewDelegate
{
public override MKOverlayRenderer OverlayRenderer(MKMapView mapView, IMKOverlay overlay)
{
switch (overlay)
{
case MKPolygon polygon:
var prenderer = new MKPolygonRenderer(polygon)
{
FillColor = UIColor.Red,
StrokeColor = UIColor.Blue,
Alpha = 0.4f,
LineWidth = 9
};
return prenderer;
default:
throw new Exception($"Not supported: {overlay.GetType()}");
}
}
}
Instance and assign the delegate to your map:
mapDelegate = new MyMapDelegate();
map.Delegate = mapDelegate;
Note: Store the instance of your MyMapDelegate in a class level variable as you do not want to get GC'd
Update:
MKMapView has two steps involved to display an overlay on its map.
1. Calling `AddOverlay` and `AddOverlays`
First you add overlays to the map that conform to IMKOverlay. There are basic built-in types such as MKCircle, MKPolygon, etc... but you can also design your own overlays; i.e. overlays that define the location of severe weather (lightning, storm clouds, tornados, etc..). These MKOverlays describe the geo-location of the item but not how to draw it.
2. Responding to `OverlayRenderer` requests
When the display area of the map intersects with one of the overlays, the map need to draw it on the screen. The map's delegate (your MKMapViewDelegate subclass) is called to supply a MKOverlayRenderer that defines the drawing routines to paint the overlay on the map.
This drawing involves converting the geo-coordinates of the overlay to local display coordinates (helper methods are available) using Core Graphics routines (UIKit can be used with some limitations). There are basic built-in renderers for MKCircleRenderer, MKPolygonRenderer, etc.. that can be used or you can write your own MKOverlayRenderer subclass.
You could supply a custom way to renderer a MKCircle overlay, maybe a target-style red/white multi-ringed bullseye, instead of the way the default circle renderer draws it, or custom renderers that draw severe storm symbols within the bounds of a MKPolygon to match your custom severe storm overlays.
My Example code:
Since you are using MKPolygon to build your overlays, you can use the MKPolygonRenderer to display them. In my example, I provide a pattern matching switch (C# 6) that returns a semi-transparent Red/Blue MKPolygonRenderer for every MKPolygon that you added to the map (if you added a non-MKPolygon based overlay it will throw an exception).
I was also stuck in this issue and I have found the way to create the sub class of MKPolygon.
I have checked it with my example and it works like a charm. But not sure that Apple may reject my app or not.
public class CvPolyon : MKPolygon
{
public CustomObject BoundaryOption { get; }
public CvPolyon1(MKPolygon polygon, CustomObject boundaryOption)
:base(polygon.Handle)
{
BoundaryOption = boundaryOption;
}
}
We can add polygon on map like this.
var polygon = MKPolygon.FromCoordinates(coordinates);
var overlay = new CvPolyon(polygon, new CustomObject());
mapView.AddOverlay(overlay);
We can recognize our polygon in the class which extends MKMapViewDelegate like this.
public override MKOverlayRenderer OverlayRenderer(MKMapView mapView, IMKOverlay overlay)
{
if (overlay is CvPolyon polygon)
{
var polygonRenderer = new MKPolygonRenderer(polygon)
{
FillColor = polygon.BoundaryOption.AreaColor,
StrokeColor = polygon.BoundaryOption.LineColor,
Alpha = polygon.BoundaryOption.Alpha,
LineWidth = polygon.BoundaryOption.LineWidth
};
if (polygon.BoundaryOption.IsDashedLine)
polygonRenderer.LineDashPattern = new[] { new NSNumber(2), new NSNumber(5) };
return polygonRenderer;
}
return mapView.RendererForOverlay(overlay);
}

HelixToolkit how to exclude elements from HitTest

I have a scene with a skybox and I would like to get the point the user clicked projected onto the skybox.
I'm using HelixViewport3D.FindNearestPoint(Point pt) to get the point, which works very well, except when there's anything between the click and the skybox. In this situation it returns the point projected onto the object in front of the skybok.
Is there any way to flag an element so it would be ignored in HitTests?
You can catch point on any Visual3D or Geometry3D
Give names to your Visual3D objects.
ModelVisual3D modelVisual3D = new ModelVisual3D();
modelVisual3D.SetName("ModelName");
You can use FindHits method with your HelixViewPort3D
Point3D point3D;
var hitList = yourHelixViewPort.ViewPort.FindHits(Point point);
foreach (var hit in hitList)
{
if (hit.Visual != null)
{
if (hit.Visual.GetName() == "ModelName")
{
point3D = hit.Position;
// You can use also hit.Mesh
// also hit.Model
// also hit.Visual
// also hit.Normal
}
}
}

Map pushpin click event

I'm building a windows phone 8.1 app that reads a file ( which holds GPS points, each point consists of e.g. latitude and longitude fields). For each point I have it so it's displayed with a little pushpin icon on the map control, based on its coordinated from the above file. Is there any way I can implement a click event on the push pin element and use it to e.g. to display another window where user can change/display specific point/pushpin info ( latitude or longitude) ? This is what I use to read my file and display on the map and it works fine:
info: notes is public ObservableCollection<Point> notes;
protected async override void OnNavigatedTo(NavigationEventArgs e)
{
bool exist; // used to check if JSON file exists
// get current position and set the map to point at it
Geoposition position = await App.DataModel.GetCurrentPosition();
await MyMap.TrySetViewAsync(position.Coordinate.Point, 16D);
mySlider.Value = MyMap.ZoomLevel; // set it to 16D from previous line
exist = await App.DataModel.FileExistsAsync();
if (exist == true)
{
// read the file and load points into a list
await App.DataModel.ReadFile();
// put points on the map - little map icon
foreach (var point in App.DataModel.notes)
{
MapIcon myMapIcon = new MapIcon();
myMapIcon.Location = new Geopoint(new BasicGeoposition()
{
Latitude = point.Latitude,
Longitude = point.Longitude,
});
myMapIcon.NormalizedAnchorPoint = new Point(0.5, 1.0);
MyMap.MapElements.Add(myMapIcon);
}
}
}
Not as you have implemented, no. You will need to add XAML-based pushpins to the map using MyMap.Children.Add(myXamlControl) instead of using MyMap.MapElements.Add(myMapIcon) if you want any sort of end-user events. MapIcons get integrated into the map "image" instead of floating on top and cannot react to anything.
Edit: Here's how you set the point and anchor for a XAML control:
MyMap.SetLocation(myXamlControl, myPoint);
MyMap.SetNormalizedAnchorPoint(myXamlControl, new Point(0.5, 0.5));

Categories