Calculate Latitude and Longitude Coordinates

I am looking for an elegant solution that calculates the center between several latitude-longitude (for example, just center the map in the center polygon).

Table: locations :

 id | city | latitude | longitude ----------------------------------------------- 1 | Berlin | 52.524268 | 13.406290 ----------------------------------------------- 2 | London | 51.508129 | -0.1280050 ----------------------------------------------- 3 | Hamburg | 53.551084 | 9.9936817 ----------------------------------------------- 4 | Amsterdam | 52.370215 | 4.8951678 ----------------------------------------------- 

Current calculation:

 function calculateCenter($array_locations) { $minlat = false; $minlng = false; $maxlat = false; $maxlng = false; foreach ($array_locations as $geolocation) { if ($minlat === false) { $minlat = $geolocation['lat']; } else { $minlat = ($geolocation['lat'] < $minlat) ? $geolocation['lat'] : $minlat; } if ($maxlat === false) { $maxlat = $geolocation['lat']; } else { $maxlat = ($geolocation['lat'] > $maxlat) ? $geolocation['lat'] : $maxlat; } if ($minlng === false) { $minlng = $geolocation['lon']; } else { $minlng = ($geolocation['lon'] < $minlng) ? $geolocation['lon'] : $minlng; } if ($maxlng === false) { $maxlng = $geolocation['lon']; } else { $maxlng = ($geolocation['lon'] > $maxlng) ? $geolocation['lon'] : $maxlng; } } // Calculate the center $lat = $maxlat - (($maxlat - $minlat) / 2); $lon = $maxlng - (($maxlng - $minlng) / 2); return array($lat, $lon); } 
+4
source share
3 answers

When using Google Maps, you can use the getBounds () and getCenter () methods .

I rebuilt your coordinates to form a convex polygon (all the vertices of the outward point, away from the center). The polygon closes when the first coordinate is the first and last value in the polygonCoords array.

See jsfiddle

 var map; var polygon; var bounds = new google.maps.LatLngBounds(); var i; var myLatLng = new google.maps.LatLng(52.5,6.6); var myOptions = { zoom: 5, center: myLatLng, mapTypeId: google.maps.MapTypeId.TERRAIN }; map = new google.maps.Map(document.getElementById("map_canvas"), myOptions); var polygonCoords = [ new google.maps.LatLng(52.524268,13.406290), new google.maps.LatLng(53.551084,9.9936817), new google.maps.LatLng(51.508129,-0.1280050), new google.maps.LatLng(52.370215,4.8951678), new google.maps.LatLng(52.524268,13.406290)//Start & end point ]; polygon = new google.maps.Polygon({ paths: polygonCoords, strokeColor: "#FF0000", strokeOpacity: 0.8, strokeWeight: 3, fillColor: "#FF0000", fillOpacity: 0.05 }); polygon.setMap(map); for (i = 0; i < polygonCoords.length; i++) { bounds.extend(polygonCoords[i]); } // The Center of the polygon var latlng = bounds.getCenter(); var marker = new google.maps.Marker({ position: latlng, map: map, title:latlng.toString() }); 
+8
source

Averaging your latitudes and longitudes works in many cases, but in some cases there are problems. For example, you have 2 quotes, Tokyo (long = 140) and Seattle (long -122), the average longitude is 18, somewhere in Europe. You would expect something closer to the international date line, 180 degrees.

The most direct, problem-free method is averaging vectors, as if each of them came from the center of the earth.

Pseudocode (accepts radians)

 for each lat,long // assume 1 radii from the earth center. // covert lat, long, and radii into x,y,z (spherical to cartesian coordinates) r=1, theta=pi/2 - lat, phi=long x = r*sin(theta)*cos(phi) y = r*sin(theta)*sin(phi) z = r*cos(theta) N++; // accumulate x,y,z sum_x += x, etc. // average x,y,z avg_x = sum_x/N, etc. // convert x,y,z back to spherical co-ordinates to get the lat/long center. rho = sqrt(avg_x*avg_x + avg_y*avg_y + avg_z*avg_z) lat = pi/2 - acos(avg_z/rho) // acos() results are 0 to pi long = atan2(avg_y, avg_x) // 4 quadrant arctangent 

[Change fixed spherical coordinates to Cartesian]

+3
source

Google uses the Mercator projection, viewing the earth as an elongated cylinder. Thus, the task is to find the center of this projection.

For each lat / long pair, convert the x, y scale coordinates to the map (using radians):

 x = long y = ln(tan(pi/4 + lat/2)) // Mercator projection 

Then for x and y, find the average of the minimum and maximum to get your center. Convert back to lat / long as shown below.

 Pseudo code center_long = average(minimum_x, maximum_x) center_lat = (atan(exp(average(minimum_y, maximum_y))) - pi/4)*2 

The calculation of central longitude works great if it were not for the circular nature of the cylindrical projection of the Earth. If longitudes are in both the eastern and western hemispheres (some negative, some positive), additional work may be required.

 Pseudo code sort the longitudes into ascending order for each longitude difference = longitude(i-1) - longitude(i) // for first, use longitude(0) - longitude(last) if (difference < 0) add 2*pi (360 degrees) Keep track of index of minimal difference The pair with the minimal difference represents the pair that most tightly contains all longitudes. Average this pair for the center longitude. If this pair was index 0 & last, add pi (180 degrees) 

City result OP 4: (52.4 N, 7.0 E)


This is my second answer, because the first does not get the gist of the OP message. Since it has some value, it remains.

+2
source

Source: https://habr.com/ru/post/1495569/


All Articles