Calculate Rectangle for BringIntoView

4 posts, 1 answers
  1. Caleb
    Caleb avatar
    4 posts
    Member since:
    Mar 2016

    Posted 28 Apr Link to this post

    Hello!

    I was wondering if anyone knew of a simple algorithm to take a bunch of existing Lat/Lon points and calculate a rectangle out of them? I'm trying to have the map view center and zoom to fit a list of points automatically. I know how to center on one particular point but not sure how to do so for a group of points. I'm used to using the Google Maps API which has a function where you can keep adding geo points and it creates a bounding rectangle. Does RadMap have something similar to that?

    Thanks! :)

  2. Answer
    Hristo
    Admin
    Hristo avatar
    1016 posts

    Posted 01 May Link to this post

    Hello Caleb,

    Thank you for writing.

    The RectangleG object has a constructor accepting North, West, South and East parameters. In order to achieve your task, you need to iterate your points and calculate the min and max values. Please check my code snippet below: 
    private void radButton1_Click1(object sender, EventArgs e)
    {
        double north = 0;
        double west = 0;
        double south = 0;
        double east = 0;
     
        List<PointG> points = new List<PointG>();
        foreach (MapVisualElement element in this.radMap1.MapElement.Layers["PointG"].Overlays)
        {
     
            MapPin pin = element as MapPin;
            if (pin == null)
            {
                continue;
            }
     
            double lat = pin.Location.Latitude;
            double lng = pin.Location.Longitude;
            if (lat > north)
            {
                north = lat;
            }
     
            if (lat < south)
            {
                south = lat;
            }
     
            if (lng > east)
            {
                east = lng;
            }
     
            if (lng < west)
            {
                west = lng;
            }
        }
     
        RectangleG rect = new RectangleG(north, west, south, east);
        this.radMap1.BringIntoView(rect);
    }

    I hope this helps. Should you have further questions please do not hesitate to write back.

    Regards,
    Hristo
    Telerik by Progress
    Try our brand new, jQuery-free Angular 2 components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
  3. Caleb
    Caleb avatar
    4 posts
    Member since:
    Mar 2016

    Posted 01 May in reply to Hristo Link to this post

    Thanks a lot for the help! Your code sample tipped me off to something I had wrong in my calculation code. :) Here's the finished code below I'm using now.

    private RectangleG GetBestView(List<MapPin> pins)
    {
        var northEast = new PointG(-9999, -9999);
        var southWest = new PointG(9999, 9999);
     
        foreach (var pin in pins)
        {
            northEast = new PointG(
                Math.Max(northEast.Latitude, pin.Location.Latitude),
                Math.Max(northEast.Longitude, pin.Location.Longitude)
            );
     
            southWest = new PointG(
                Math.Min(southWest.Latitude, pin.Location.Latitude),
                Math.Min(southWest.Longitude, pin.Location.Longitude)
            );
        }
     
        return new RectangleG(northEast.Latitude, southWest.Longitude,
            southWest.Latitude, northEast.Longitude);
    }

     

    Thanks again!

  4. Hristo
    Admin
    Hristo avatar
    1016 posts

    Posted 02 May Link to this post

    Hi Caleb,

    Thank you for writing.

    I am glad that I managed to help. The provided code snippet seems to be valid and I see no issues with it.

    Please let me know if you need further assistance.

    Regards,
    Hristo
    Telerik by Progress
    Try our brand new, jQuery-free Angular 2 components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
Back to Top