polygonmarkergmap.net

Gmap.net show only markers within polygon


I am currently working with gmap.net to create a certain radius with a polygon. I currently have made a polygon for the radius but now I come to the problem that I want to create multipule markers but only show the markers who are inside the polygon. Is this possible?

_polygonOverlay = new GMapOverlay("destination");
_gMap.Overlays.Add(_polygonOverlay);

private void CreateCircle(PointLatLng destination, double radius)
    {
        List<PointLatLng> radiusPoint = new List<PointLatLng>();

        double seg = Math.PI * 2 / 40;

        for (int i = 0; i < 40; i++)
        {
            double theta = seg * i;
            double latitude = destination.Lat + Math.Cos(theta) * radius;
            double longitude = destination.Lng + Math.Sin(theta) * radius;

            PointLatLng cirlePoint = new PointLatLng(latitude, longitude);

            radiusPoint.Add(cirlePoint);
        }
        GMapPolygon radiusCircle = new GMapPolygon(radiusPoint, "radius");
        _polygonOverlay.Polygons.Add(radiusCircle);
    }

private void CreateMarkers()
        {
            _polygonOverlay.Markers.Add(new GMarkerGoogle(new PointLatLng(xxx, xxx), GMarkerGoogleType.blue));
            _polygonOverlay.Markers.Add(new GMarkerGoogle(new PointLatLng(xxx, xxx), GMarkerGoogleType.blue));
            _polygonOverlay.Markers.Add(new GMarkerGoogle(new PointLatLng(xxx, xxx), GMarkerGoogleType.blue));
        }

Here is a little sample of the code I have that create a circle (still needs some work on it) and some markers.

Already thanks is advance


Solution

  • Since you are dealing with a circle, you should be able to simply check the distance of your marker from the center of the circle. If the distance is greater than the radius, don't add it to the overlay.

    GMap gives you access to the necessary methods to determine this information. Do something like this:

    //Assuming p1 is your marker and p2 is your circle center coordinate
    double markerDist = GMap.NET.MapProviders.EmptyProvider.Instance.Projection.GetDistance(p1.Position, p2); 
    
    if(markerDist <= circleRadius)
    {
        //Add the marker to the overlay
    }