问题
I am dynamically plotting several points onto a google map. I'm trying to find the best way to find the center of the given points. I've tried using the following:
var mapArray = new Array;
mapArray[0] = new Array(42, 35.391228, -119.008401);
mapArray[1] = new Array(34, 33.874277, -118.131555);
mapArray[2] = new Array(214, 32.6922592, -115.4962203);
mapArray[3] = new Array(216, 33.3818875, -117.2449785);
mapArray[4] = new Array(40, 36.805231, -119.770192);
mapArray[5] = new Array(47, 37.638266, -122.117398);
mapArray[6] = new Array(218, 37.638266, -122.117398);
mapArray[7] = new Array(39, 33.70677, -116.241719);
mapArray[8] = new Array(219, 33.666489, -117.30137);
mapArray[9] = new Array(37, 34.0625743, -118.354077);
mapArray[10] = new Array(217, 34.0625743, -118.354077);
mapArray[11] = new Array(43, 34.195561, -119.179495);
mapArray[12] = new Array(220, 37.671111, -121.873443);
mapArray[13] = new Array(215, 33.736294, -116.405587);
mapArray[14] = new Array(35, 33.978778, -117.383186);
mapArray[15] = new Array(36, 32.8321559, -117.1264585);
mapArray[16] = new Array(46, 37.312298, -121.930904);
mapArray[17] = new Array(221, 1, 1);
mapArray[18] = new Array(41, 33.7453974, -117.8502537);
mapArray[19] = new Array(44, 34.426024, -119.697417);
mapArray[20] = new Array(45, 34.952801, -120.440045);
mapArray[21] = new Array(38, 34.199697, -118.571618);
var avgLat = 0;
var avgLng = 0;
var j = 0;
for (var i in mapArray) {
avgLat = (avgLat + mapArray[i][1]);
avgLng = (avgLng + mapArray[i][2]);
j++;
}
avgLat = avgLat / j;
avgLng = avgLng / j;
map.setCenter(new GLatLng(avgLat, avgLng), 6);
But that doesn't give me an accurate center. What's the best way to dynamically plot the center of the map?
回答1:
Find the max and min for both lat and long and then center on (max-min)/2 for each.
That should be (max + min) / 2, the average.
回答2:
Note that you've computed the centroid, which in geography is called the geographic center.
This doesn't really work as well if you don't have a well defined shape because it's an average, so you can have a collection of points in one area that skew the center towards them.
Instead you could take find the maximum and minimum latitudes and longitudes and take the midpoint for each one as your new coordinates.
You could also do something more complex and try to get an idea of what the region these points trace out looks like. I don't have a good algorithm for you, but the idea would be to remove points that are "too close", where "close" is defined by the distances between your points. Once you pare out some of the clusters, you can use the centroid method with better results.
回答3:
Stever I saw your image and had the same problem. To solved it what I did was to assign the value of the first point to the min and max values.
Here is the code so you can review it. Note: The "Point" is a structure i made to store the lat and lng values.
x = 0;
y = 0;
Point point;
if (points.Count.Equals(0))
return;
Double minLat = points[0].lat, maxLat = points[0].lat;
Double minLng = points[0].lng, maxLng = points[0].lng;
for (int index = 1, count = points.Count; index < count; index++)
{
point = points[index];
if (minLat > point.lat)
minLat = point.lat;
if (maxLat < point.lat)
maxLat = point.lat;
if (minLng > point.lng)
minLng = point.lng;
if (maxLng < point.lng)
maxLng = point.lng;
}
x = (minLat + maxLat) / 2;
y = (minLng + maxLng) / 2;
来源:https://stackoverflow.com/questions/7235430/find-the-average-center-of-lats-lngs