First of all, Thank you @Jason for the provided links of answers.
I came up with this solution referencing to this question.
Checking if a longitude/latitude coordinate resides inside a complex polygon in an embedded device?
As I have mention on the comments above, I have a set of coordinates(lat/lng)
inside my database and draw it on my maps in android to form a rectangular shape using polyline.
To check if the user is inside the drawn rectangle. I have modified the answer of Drew Noakes in the provided link above of this answer.
CODE
/// <summary>
/// Check if the user's location is inside the rectangle
/// </summary>
/// <param name="location">Current user's location</param>
/// <param name="_vertices">Rectangular shape for checking</param>
/// <returns></returns>
private bool CheckUserLocation(Location location, Polyline _vertices)
{
var lastPoint = _vertices.Geopath[_vertices.Geopath.Count - 1];
var isInside = false;
var x = location.Longitude;
foreach (var point in _vertices.Geopath)
{
var x1 = lastPoint.Longitude;
var x2 = point.Longitude;
var dx = x2 - x1;
if (Math.Abs(dx) > 180.0)
{
if (x > 0)
{
while (x1 < 0)
x1 += 360;
while (x2 < 0)
x2 += 360;
}
else
{
while (x1 > 0)
x1 -= 360;
while (x2 > 0)
x2 -= 360;
}
dx = x2 - x1;
}
if ((x1 <= x && x2 > x) || (x1 >= x && x2 < x))
{
var grad = (point.Latitude - lastPoint.Latitude) / dx;
var intersectAtLat = lastPoint.Latitude + ((x - x1) * grad);
if (intersectAtLat > location.Latitude)
isInside = !isInside;
}
lastPoint = point;
}
return isInside;
}
If you are curious on how I tested it, here is what I did.
First, I retrieved my current location and manually draw a rectangle circulating on my location.
Second, Draw a second rectangle not so far away from my current location but outside the first drawn rectangle.
Third, I passed the first Polyline drawn on the function
CheckUserLocation(myCurrentLocation, firstRectangle) //THIS RETURNS TRUE
Another test is I passed the second rectangle to the function
CheckUserLocation(myCurrentLocation, secondRectangle) //THIS RETURNS FALSE
I will test it again if there is something wrong with this answer. But I hope someone will find this answer helpful.