question

WeiWen-3421 avatar image
0 Votes"
WeiWen-3421 asked LeonLu-MSFT commented

How to zoom out a map to show all its elements (Pins, Circles, Polygons)?

As the title says, I want to be able to have all map components displayed on map. More importantly, I want to get the bounds of the map that has all components visible. This information needs to be saved in the backend so that when this information is accessed, the map can display correctly with all map components visible.

If I find the min Latitude and Longitude, and max Latitude and Longitude from all the (latitude, longitude) of the map components, and then draw the map bounded by them, will I get all components shown on the map?

dotnet-xamarinforms
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

LeonLu-MSFT avatar image
0 Votes"
LeonLu-MSFT answered LeonLu-MSFT commented

Hello,​

Welcome to our Microsoft Q&A platform!

Firstly, here are no api to achieve it, Following way is an idea of realization.

customMap.MoveToRegion(MapSpan.FromCenterAndRadius(new Position(37.79752, -122.40183), Distance.FromMiles(1.0))); we use this code to make sure the center point of map and radius.

Now you need how all its elements (Pins, Circles, Polygons), So you have to cacluate the center point from all of the Position. Then get the (longest distance )/2 from Latitude and Longitude. to set the Distance.FromMiles(longest distance )/2)).


You can try to following method to get the CentralPoint.

public static double[] CentralPoint(List<Position> points)
        {
            double[] centroid = { 0.0, 0.0 };

            for (int i = 0; i < points.Count; i++)
            {
                centroid[0] += points[i].Latitude;
                centroid[1] += points[i].Longitude;
            }

            int totalPoints = points.Count();
            centroid[0] = centroid[0] / totalPoints;
            centroid[1] = centroid[1] / totalPoints;

            return centroid;
        }


Here is get the distance from the point. We need to compare all of points. then get the Longest distance. then longest distance/2 = Radius

private double distance(double lat1, double lon1, double lat2, double lon2)
        {
            double theta = lon1 - lon2;
            double dist = Math.Sin(deg2rad(lat1))
                            * Math.Sin(deg2rad(lat2))
                            + Math.Cos(deg2rad(lat1))
                            * Math.Cos(deg2rad(lat2))
                            * Math.Cos(deg2rad(theta));
            dist = Math.Acos(dist);
            dist = rad2deg(dist);
            dist = dist * 60 * 1.1515;
            return (dist);
        }

        private double deg2rad(double deg)
        {
            return (deg * Math.PI / 180.0);
        }

        private double rad2deg(double rad)
        {
            return (rad * 180.0 / Math.PI);
        }


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.



· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@Leon-Lu Thank you for the detailed answer. I have a question about the unit of the distance calculation. Is it kilometers or miles? What do the constants 60 and 1.1515 mean?

Also, is it possible to get the bounds of whatever is displaying on the screen, if not doing the calculations?

0 Votes 0 ·

Is it kilometers or miles? What do the constants 60 and 1.1515 mean?

miles, 60 is the number of minutes in a degree; //1.1515 is the number of statute miles in a nautical mile.One nautical mile is the length of one minute of latitude at the equator.
dist = dist * 1.609344;

is it possible to get the bounds of whatever is displaying on the screen if not doing the calculations?

Above method use zoom in or zoom out the map to achieve. it is not related to the screen, I cannot find a api or method to achieve it without calculations

0 Votes 0 ·