0

I am working on Xamarin Forms with Maps. I need to get a certain area within the map and check if the user are inside of this area. for example, There is a certain village that I want to get the whole area and coordinates? I only i tried pin location but there is only 2 coordinates and cannot really determine if the user is inside the area.

I am going to save the area in my database and I already figured how to do this. but I don't know how can I get the coordinates of the certain area. How can I achieve this in Xamarin Forms?

I added an image for example, How Can I the the boxed area?enter image description here

Milo
  • 99
  • 7
  • https://stackoverflow.com/questions/58816089/how-to-convert-screen-x-and-y-into-lat-lon – Jason Sep 06 '22 at 13:00
  • translate the bounds of the rectangle into lat/long using the link above. Find the current location of the user. Use math to determine if the user's location is inside the bounds of the rectangle – Jason Sep 08 '22 at 01:06
  • @Jason, currently, I have no idea working with converting the rectangle to lat/long. can you help me with this one so I can check if the user is inside the rectangle? – Milo Sep 15 '22 at 07:16
  • I still have no idea what you're really trying to do. Your question shows a black box on the map - is this something drawn by the user? Where does the box come from? You question also mentions "certain village" - where does this come from? Do you either have a set of screen coordinates (box drawn by user) or geo-coordinates (boundaries of the "village") to work with? – Jason Sep 15 '22 at 12:54
  • @Jason, I have a set of coordinates inside my database, I'm going to use it as reference to create this polylines on the map. I already created the polylines and currently visible on the map, my goal is, I want to check if the user is inside that rectangular shape drawn in the map. But as you have said, I need to translate the bounds of the rectangular shape to lat/lng for me to check is the user is inside. Currently, I have no idea on converting the bounds of the rectangle to lat/lng. So if you are willing to help me, that would be appreciated so much. – Milo Sep 15 '22 at 23:12
  • Are the db coordinates lat/long or x/y? – Jason Sep 15 '22 at 23:19
  • @Jason, It's a lat/long coordinates. – Milo Sep 15 '22 at 23:21
  • Then you don’t need to convert anything. You just need to determine if the users location is inside the box. You have the coordinates for the four corners of the box and the users location, correct? This is basic geometry. For a very large area you have to worry about the curvature of the earth but for a region this size you can ignore it. – Jason Sep 15 '22 at 23:27
  • https://www.google.com/search?q=check+if+geo+coordinates+are+inside+a+shape+site:stackoverflow.com – Jason Sep 15 '22 at 23:29
  • @Jason, Thank you for your time. will check on this one. I really appreciate your help. – Milo Sep 15 '22 at 23:39

1 Answers1

1

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.

Milo
  • 99
  • 7