gpt4 book ai didi

android - MKCoordinateRegionMakeWithDistance 在 Android 中等效

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:58:27 28 4
gpt4 key购买 nike

我们可以在 iPhone 的 map 上设置特定位置的周边区域,如下所示

CLLocationCoordinate2D coord = {latitude:37.09024, longitude:-95.712891};
CLLocationDistance latitudinalMeters;
latitudinalMeters =NoOfMiles * 1609.344;
CLLocationDistance longitudinalMeters;
longitudinalMeters = NoOfMiles * 1609.344;
mapViewHome.region = MKCoordinateRegionMakeWithDistance(coord, latitudinalMeters, longitudinalMeters);

Android有没有等价的方法?

最佳答案

此代码不是产品质量。改用克里斯在此处评论中的建议:https://issuetracker.google.com/issues/35823607#comment4


这个问题最初是针对 Maps API v1 提出的。此答案适用于 v2,但可以轻松更改为 v1,所以...

没有简单的方法来做到这一点。

您可能想要 request this feature on gmaps-api-issues .

由于等待在 Google 方面实现此功能可能需要几个月的时间,所以这就是我要做的:

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();
}

用法:

LatLngBounds bounds = AndroidMapsExtensionsUtils.boundsWithCenterAndLatLngDistance(new LatLng(51.0, 19.0), 1000, 2000);
map.moveCamera(CameraUpdateFactory.newLatLngBounds(bounds, 0));

注意事项:

  • 此代码尚未经过全面测试,可能不适用于边缘情况
  • 您可能想要调整私有(private)常量以使其执行得更快
  • 您可以删除计算 LatLng south 的第 3 部分,并像计算经度一样这样做:这对于较小的 latDistance 值来说是准确的(假设您不会看到 100 公里以下的差异)
  • 代码很丑,请随意重构

关于android - MKCoordinateRegionMakeWithDistance 在 Android 中等效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6224671/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com