This is a migrated thread and some comments may be shown as answers.

Calculate Rectangle for BringIntoView

3 Answers 187 Views
Map
This is a migrated thread and some comments may be shown as answers.
Caleb
Top achievements
Rank 1
Caleb asked on 29 Apr 2017, 04:59 AM

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! :)

3 Answers, 1 is accepted

Sort by
0
Accepted
Hristo
Telerik team
answered on 01 May 2017, 09:02 AM
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.
0
Caleb
Top achievements
Rank 1
answered on 01 May 2017, 04:31 PM

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!

0
Hristo
Telerik team
answered on 02 May 2017, 05:26 AM
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.
Tags
Map
Asked by
Caleb
Top achievements
Rank 1
Answers by
Hristo
Telerik team
Caleb
Top achievements
Rank 1
Share this question
or