Mapsui: [Xamarin Forms] Question : How can I detect if a point is inside or outside a circle?

Created on 18 Jul 2019  路  4Comments  路  Source: Mapsui/Mapsui

Hello, I would like to know if there is a method that can detect if a point (in this case the GPS) is inside or outside a circle (geofencing or geofencing).

Something similar to this code that is for Android (Java)

float[] disResult = new float[2];

Location.distanceBetween( pos.latitude, pos.longitude,
                          circle.getCenter().latitude, 
                          circle.getCenter().longitude, 
                          disResult);

if(disResult[0] > circle.getRadius()){
    Log outside
 } else {
    Log inside
 }

Computes the approximate distance in meters between two locations, and optionally the initial and final bearings of the shortest path between them. Distance and bearing are defined using the WGS84 ellipsoid.

The computed distance is stored in results[0]. If results has length 2 or greater, the initial bearing is stored in results[1]. If results has length 3 or greater, the final bearing is stored in results[2].

Parameters

startLatitude | double: the starting latitude
startLongitude | double: the starting longitude
endLatitude | double: the ending latitude
endLongitude | double: the ending longitude
results | float: an array of floats to hold the results

I've been looking at the documentation but I have not found something similar to help me implement it.

If you have any ideas, I would appreciate it. :)

question

All 4 comments

To know if a point is within a circle it is enough to check if the distance to the center is less than the radius (no fancy geometry). So what you need is a method to calculate the distance between two points in latlon (wgs84, https://epsg.io/4326). There is no such method in Mapsui right now but you should be able to google one. This is the first thing I hit upon: https://stackoverflow.com/a/11172685/85325

Thanks @pauldendulk for the information, I will investigate a little more what you comment.

This is the code I use and it has served me very well :)

```c#
public static double Distance(double Lat_gps, double Long_gps)
{

        var lat1 = Lat_gps;
        var lon1 = Long_gps;
        var lat2 = Lat_geo;
        var lon2 = Long_geo;

        var R = 6378.137; // Radius of earth in KM
        var dLat = lat2 * Math.PI / 180 - lat1 * Math.PI / 180;
        var dLon = lon2 * Math.PI / 180 - lon1 * Math.PI / 180;
        var a = Math.Sin(dLat / 2) * Math.Sin(dLat / 2) + Math.Cos(lat1 * Math.PI / 180) * Math.Cos(lat2 * Math.PI / 180) * Math.Sin(dLon / 2) * Math.Sin(dLon / 2);
        var c = 2 * Math.Atan2(Math.Sqrt(a), Math.Sqrt(1 - a));
        var d = (R * c) * 1000;
        return d;

    }

```

@Jesrigt Thanks for giving this back!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

TarasParashchuk picture TarasParashchuk  路  4Comments

pauldendulk picture pauldendulk  路  7Comments

willy40 picture willy40  路  6Comments

lukaszpitulski picture lukaszpitulski  路  3Comments

charlenni picture charlenni  路  3Comments