我想通了,如何做到這一點的硬路上面給出。我選擇了LatLngBounds的一個方形區域來簡化軸承編號(只要它適合在屏幕中,這對我來說是可以的:你可以計算出軸承從中心到非方形矩形的角點與一些更多的三角)。我還添加了一些填充,因爲點之間的距離將與從中心到東北角的對角線相同,並且這將大於從中心到頂邊的距離,這將意味着第二個點可能在頂部邊緣之上並且不可見(如果是,例如正北)。我借巨資從http://www.movable-type.co.uk/scripts/latlong.html和calculating Lat and Long from Bearing and Distance,
這是我對獲得的LatLngBounds方法:
public LatLngBounds getBounds(double lat1, double lng1, double lat2,
double lng2) {
// defines a square area with point lat1,lng1 at center and includes
// point
// lat2, long2 with a buffer between edge and second point
// get distance between two points
float[] results = new float[1];
Location.distanceBetween(lat1, lng1, lat2, lng2, results);
double d = results[0];
d = (d * 1.5); // add padding. The shortest distance to an edge of the
//square box is d * cos(45 degrees). Thus it's possible that the second point will be out of the box. To compensate I make the radius larger (d * 1/cos(45) = d * 1.41). I put a bit extra so that the second point isn't right on the edge of the screen.
long R = 6371000; // distance of earth's radius in meters
d = d /(double) R;
lat1 = Math.toRadians(lat1); // Current lat point converted to radians
lng1 = Math.toRadians(lng1); // Current long point converted to radians
// calculate northeast corner of LatLngBounds
double brng = Math.toRadians(45); // bearing from center to northeast in
// radians
double resultLat1 = Math.asin(Math.sin(lat1) * Math.cos(d)
+ Math.cos(lat1) * Math.sin(d) * Math.cos(brng));
double resultLng1 = lng1
+ Math.atan2(Math.sin(brng) * Math.sin(d) * Math.cos(lat1),
Math.cos(d) - Math.sin(lat1) * Math.sin(resultLat1));
resultLat1 = Math.toDegrees(resultLat1);
resultLng1 = Math.toDegrees(resultLng1);
Log.i("My Code", "resultLat1: " + resultLat1 + " resultLng1: " + resultLng1);
LatLng northEast = new LatLng(resultLat1, resultLng1);
// calculate southwest corner of LatLngBounds. Everything is the same
// except the bearing value
brng = Math.toRadians(225); // bearing from center to southwest corner
// in radians
double resultLat2 = Math.asin(Math.sin(lat1) * Math.cos(d)
+ Math.cos(lat1) * Math.sin(d) * Math.cos(brng));
double resultLng2 = lng1
+ Math.atan2(Math.sin(brng) * Math.sin(d) * Math.cos(lat1),
Math.cos(d) - Math.sin(lat1) * Math.sin(resultLat2));
resultLat2 = Math.toDegrees(resultLat2);
resultLng2 = Math.toDegrees(resultLng2);
Log.i("My Code", "resultLat2: " + resultLat2 + " resultLng2: " + resultLng2);
LatLng southWest = new LatLng(resultLat2, resultLng2);
LatLngBounds bounds = new LatLngBounds(southWest, northEast);
return bounds;
}
@ MES-請儘可能添加屏幕截圖。 – TheFlash 2013-05-04 04:10:08
添加屏幕截圖。 – 2013-05-04 04:43:44
您可以將地圖居中放在第一個點,然後縮小,直到第二個點可見。我沒有嘗試過這個,但也許你可以使用'GoogleMap#getProjection()。getVisibleRegion()。latLngBounds'來獲取當前可見的地圖區域,然後使用'LatLngBounds#contains ()'。如果第二個點不可見,則可以縮小並再次檢查,直到該點可見。我會嘗試這個爲我的項目之一,如果它的作品,我會發佈一個答案。 – rubenlop 2013-05-04 05:08:02