以免从查询的开头开始。你有两个 latlng,现在你想得到矩形的 latlng。
我们将先进行计算部分,然后再进行编程部分。假设是-
中心点 = e
左上角 = A
右上角 = B
右下角 = C
左下 = D
AD的中点=f
AB的中点=g
计算部分
位置 g 参数 - lat-A.lat, long-e.long
位置 f 参数 - lat-e.lat, long-A.long
A 到 g 的距离 = A.distanceTo(g)
A 到 f 的距离 = A.distanceTo(f)
点 B = 2Ag 从 A
点 C = 2Af 从 B
点 D = 2Af 从 A
编程部分
LatLng A = null, B = null, C = null, D = null, e = null, f = null, g = null,temp=null;
e.latitude = your center latitude value;
e.longitude = your center longitude value;
A.latitude=your top left point latitude value;
A.longitude=your top left point longitude value;
f.latitude = e.latitude;
f.longitude = A.longitude;
g.latitude = A.latitude;
g.longitude = e.longitude;
double[] Ag = new double[1];
double[] Af = new double[1];
Location.distanceBetween(A.latitude, A.longitude, g.latitude, g.longitude, Ag);
Location.distanceBetween(A.latitude, A.longitude, f.latitude, f.longitude, Af);
temp=getDestinationPoint(A,90,(2*Ag));
B.latitude=temp.latitude;
B.longitude=temp.longitude;
temp=getDestinationPoint(B,180,(2*Af));
C.latitude=temp.latitude;
C.longitude=temp.longitude;
temp=getDestinationPoint(A,180,(2*Af));
D.latitude=temp.latitude;
D.longitude=temp.longitude;
private LatLng getDestinationPoint (LatLng source,double brng, double dist){
dist = dist / 6371;
brng = Math.toRadians(brng);
double lat1 = Math.toRadians(source.latitude), lon1 = Math.toRadians(source.longitude);
double lat2 = Math.asin(Math.sin(lat1) * Math.cos(dist) +
Math.cos(lat1) * Math.sin(dist) * Math.cos(brng));
double lon2 = lon1 + Math.atan2(Math.sin(brng) * Math.sin(dist) *
Math.cos(lat1),
Math.cos(dist) - Math.sin(lat1) *
Math.sin(lat2));
if (Double.isNaN(lat2) || Double.isNaN(lon2)) {
return null;
}
return new LatLng(Math.toDegrees(lat2), Math.toDegrees(lon2));
}
解释
f和g分别是线AD和AB的中点。我们可以通过改变 A 和 e 点的 lat 和 long 值来得到它。通过这两个长度(Af 和 Ag),我们可以根据需要获得矩形的四个纬度点。
谢谢你