如果我理解正確,你想讓你的地圖適合圈子邊界?如果是這樣
添加了一圈像this
mMap.addCircle(new CircleOptions()
.center(new LatLng(location.getLatitude(), location.getLongitude()))
.radius(100)
.strokeColor(Color.RED)
.fillColor(Color.BLUE));
**那麼你需要你的邊界圈中,閱讀this **
LatLngBounds bounds = boundsWithCenterAndLatLngDistance(new LatLng(location.getLatitude(), location.getLongitude()),200,200);
**和**嘗試
private static final double ASSUMED_INIT_LATLNG_DIFF = 1.0;
private static final float ACCURACY = 0.01f;
public static LatLngBounds boundsWithCenterAndLatLngDistance(LatLng center, float latDistanceInMeters, float lngDistanceInMeters) {
latDistanceInMeters /= 2;
lngDistanceInMeters /= 2;
LatLngBounds.Builder builder = LatLngBounds.builder();
float[] distance = new float[1];
{
boolean foundMax = false;
double foundMinLngDiff = 0;
double assumedLngDiff = ASSUMED_INIT_LATLNG_DIFF;
do {
Location.distanceBetween(center.latitude, center.longitude, center.latitude, center.longitude + assumedLngDiff, distance);
float distanceDiff = distance[0] - lngDistanceInMeters;
if (distanceDiff < 0) {
if (!foundMax) {
foundMinLngDiff = assumedLngDiff;
assumedLngDiff *= 2;
} else {
double tmp = assumedLngDiff;
assumedLngDiff += (assumedLngDiff - foundMinLngDiff)/2;
foundMinLngDiff = tmp;
}
} else {
assumedLngDiff -= (assumedLngDiff - foundMinLngDiff)/2;
foundMax = true;
}
} while (Math.abs(distance[0] - lngDistanceInMeters) > lngDistanceInMeters * ACCURACY);
LatLng east = new LatLng(center.latitude, center.longitude + assumedLngDiff);
builder.include(east);
LatLng west = new LatLng(center.latitude, center.longitude - assumedLngDiff);
builder.include(west);
}
{
boolean foundMax = false;
double foundMinLatDiff = 0;
double assumedLatDiffNorth = ASSUMED_INIT_LATLNG_DIFF;
do {
Location.distanceBetween(center.latitude, center.longitude, center.latitude + assumedLatDiffNorth, center.longitude, distance);
float distanceDiff = distance[0] - latDistanceInMeters;
if (distanceDiff < 0) {
if (!foundMax) {
foundMinLatDiff = assumedLatDiffNorth;
assumedLatDiffNorth *= 2;
} else {
double tmp = assumedLatDiffNorth;
assumedLatDiffNorth += (assumedLatDiffNorth - foundMinLatDiff)/2;
foundMinLatDiff = tmp;
}
} else {
assumedLatDiffNorth -= (assumedLatDiffNorth - foundMinLatDiff)/2;
foundMax = true;
}
} while (Math.abs(distance[0] - latDistanceInMeters) > latDistanceInMeters * ACCURACY);
LatLng north = new LatLng(center.latitude + assumedLatDiffNorth, center.longitude);
builder.include(north);
}
{
boolean foundMax = false;
double foundMinLatDiff = 0;
double assumedLatDiffSouth = ASSUMED_INIT_LATLNG_DIFF;
do {
Location.distanceBetween(center.latitude, center.longitude, center.latitude - assumedLatDiffSouth, center.longitude, distance);
float distanceDiff = distance[0] - latDistanceInMeters;
if (distanceDiff < 0) {
if (!foundMax) {
foundMinLatDiff = assumedLatDiffSouth;
assumedLatDiffSouth *= 2;
} else {
double tmp = assumedLatDiffSouth;
assumedLatDiffSouth += (assumedLatDiffSouth - foundMinLatDiff)/2;
foundMinLatDiff = tmp;
}
} else {
assumedLatDiffSouth -= (assumedLatDiffSouth - foundMinLatDiff)/2;
foundMax = true;
}
} while (Math.abs(distance[0] - latDistanceInMeters) > latDistanceInMeters * ACCURACY);
LatLng south = new LatLng(center.latitude - assumedLatDiffSouth, center.longitude);
builder.include(south);
}
return builder.build();
}
終於打電話給
CameraUpdateFactory#newLatLngBounds(bounds, padding);
我覺得從其他答案複製代碼是不好的,並且鏈接到原始帖子好多了。我打算重構這段代碼,因爲它不是最理想的,並且還需要在這裏重構它。無論如何,我還修正了發送半徑(100米)而非直徑(200米)的代碼使用情況。最初的問題是關於顯示由中心,寬度和高度所定義的區域。圓的寬度和高度等於2 *半徑。對於任何感興趣的人來說,這是原創的答案:http://stackoverflow.com/questions/6224671/mkcoordinateregionmakewithdistance-equvivalent-in-android/16417396#16417396 –
謝謝你是對的,鏈接到原來的答案總是最好避免多帖子。 ..無論如何,我會看看它 –
好吧,我發現了另一個答案,最適合我的需求。但我標記你的答案,因爲你提供了一些解決方案http://stackoverflow.com/questions/15220404/adjusting-google-map-api-v2-zoom-level-in-android –