Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,293 questions
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
**Hi , I need help to Check if array of lat/long point is inside polygons in map.**
Hello,
Welcome to our Microsoft Q&A platform!
Xamarin.Forms.Maps
do not have this API to check the point is inside polygons, but we can judge it by ourselves. I refer to this thread, then I change it get following answer.
public bool IsPointInPolygon(Position p, Position[] polygon)
{
double minX = polygon[0].Latitude;
double maxX = polygon[0].Latitude;
double minY = polygon[0].Longitude;
double maxY = polygon[0].Longitude;
for (int i = 1; i < polygon.Length; i++)
{
Position q = polygon[i];
minX = Math.Min(q.Latitude, minX);
maxX = Math.Max(q.Latitude, maxX);
minY = Math.Min(q.Longitude, minY);
maxY = Math.Max(q.Longitude, maxY);
}
if (p.Latitude < minX || p.Latitude > maxX || p.Longitude < minY || p.Longitude > maxY)
{
return false;
}
// https://wrf.ecse.rpi.edu/Research/Short_Notes/pnpoly.html
bool inside = false;
for (int i = 0, j = polygon.Length - 1; i < polygon.Length; j = i++)
{
if ((polygon[i].Longitude > p.Longitude) != (polygon[j].Longitude > p.Longitude) &&
p.Latitude < (polygon[j].Latitude - polygon[i].Latitude) * (p.Longitude - polygon[i].Longitude) / (polygon[j].Longitude - polygon[i].Longitude) + polygon[i].Latitude)
{
inside = !inside;
}
}
return inside;
}
Best Regards,
Leon Lu
If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.