Find Max/Min Latitude and longitude from coordinates array

Balasaheb Molawade 136 Reputation points
2022-06-09T13:09:24.923+00:00

Hi Team,

We have a requirement to find max and min latitude and longitude from the collection of coordinates and perform some further operations.

We plot the region on the map using code available in Bing map SDK as shown below. Then we read all coordinates (Lat/Long) from plotted polygon and store them in one variable. Now we want to find max and min latitude and longitude from that coordinates.

https://www.bing.com/api/maps/sdk/mapcontrol/isdk/sdsloadmultipleboundaries

Can anyone have an idea and sample code for the finding Max/Min Latitude and longitude from coordinates array

Thanks!

Azure Maps
Azure Maps
An Azure service that provides geospatial APIs to add maps, spatial analytics, and mobility solutions to apps.
595 questions
Windows Maps
Windows Maps
A Microsoft app that provides voice navigation and turn-by-turn driving, transit, and walking directions.
245 questions
{count} votes

1 answer

Sort by: Most helpful
  1. rbrundritt 15,386 Reputation points Microsoft Employee
    2022-06-09T15:54:37.897+00:00

    There are a couple of options, depending on your definition of min/max coordinates. Number min/max or minimal bounding box min/max values (can cross anti-meridian). For number min/max you can loop through the locations can calculate these values. Here is a code block that returns an array of numbers in the form of a GeoJSON bounding box [minLon, minLat, maxLon, maxLat]

       function getExtents(locations) {  
       	if(locations && locations.length > 0){  
       		var minLat = 90  
       		var maxLat = -90;  
       		var minLon = 180;  
       		var maxLon = -180;  
         
       		for(var i = 0, len = locations.length; i < len; i++){  
       			var lat = locaitons[i].latitude;  
       			var lon = locaitons[i].longitude;  
       			  
       			if(lat < minLat) {  
       				minLat = lat;  
       			}  
       			  
       			if(lat > maxLat) {  
       				maxLat = lat;  
       			}  
       			  
       			if(lon < minLon) {  
       				minLon = lon;  
       			}  
       			  
       			if(lon > maxLon) {  
       				maxLon = lon;  
       			}  
       		}  
       		  
       		return [minLon, minLat, maxLon, maxLat];  
       	}  
         
       	return null;  
       }  
    

    For getting the minimal bounding box you can use the LocationRect class. It has two useful functions for your scenario, one that takes in an array of coordinates, and one that takes in an array of shapes.

       var bbox = Microsoft.Maps.LocationRect.fromLocations([/*your locations*/]);  
         
       var bbox = Microsoft.Maps.LocationRect.fromShapes([/*your shapes*/]);  
    

    Docs for this class can be found here: https://learn.microsoft.com/en-us/bingmaps/v8-web-control/map-control-api/locationrect-class