地図の東の点と西の点の間の距離 (メートル単位) を計算するにはどうすればよいですか? ユーザーが地図をスクロールして位置を変更した場合、その動きを mapView:didChangeCameraPosition: delegate メソッドでキャッチしますが、距離の計算方法がわかりません。
7614 次
3 に答える
10
2 つの座標間のメートル単位の距離を計算するために使用できるヘルパー関数を次に示します。
double getDistanceMetresBetweenLocationCoordinates(
CLLocationCoordinate2D coord1,
CLLocationCoordinate2D coord2)
{
CLLocation* location1 =
[[CLLocation alloc]
initWithLatitude: coord1.latitude
longitude: coord1.longitude];
CLLocation* location2 =
[[CLLocation alloc]
initWithLatitude: coord2.latitude
longitude: coord2.longitude];
return [location1 distanceFromLocation: location2];
}
アップデート:
地図は回転または傾斜している可能性があるため、地図の「東」と「西」が何を意味するかによって異なります。傾けたり回転させたりしなくても、地球の曲率により、地図の上部と下部で幅がメートル単位で異なります (赤道に近い端は、端よりもメートル単位で広くなります)これは赤道から離れています)。拡大すればするほど、差は小さくなります。
しかし、マップの左下隅と右下隅の間のメートル単位の距離を取得したい場合は、次のように使用できます。
GMSMapView* mapView = ...;
CLLocationCoordinate2D bottomLeftCoord =
mapView.projection.visibleRegion.nearLeft;
CLLocationCoordinate2D bottomRightCoord =
mapView.projection.visibleRegion.nearRight;
double distanceMetres = getDistanceMetresBetweenLocationCoordinates(
bottomLeftCoord, bottomRightCoord);
回転/傾斜を考慮したい場合は、可視領域を でラップしてGMSCoordinateBounds
から、南東と南西の角の間の距離を計算できます。次に例を示します。
GMSMapView* mapView = ...;
GMSCoordinateBounds* bounds = [[GMSCoordinateBounds alloc]
initWithRegion: mapView.projection.visibleRegion];
CLLocationCoordinate2D southWest = bounds.southWest;
CLLocationCoordinate2D southEast = CLLocationCoordinate2DMake(
bounds.southWest.latitude, bounds.northEast.longitude);
double distanceMetres = getDistanceMetresBetweenLocationCoordinates(
southWest, southEast);
ただし、これは実際に画面に表示される領域よりも広くなる可能性があります。これはGMSCoordinateBounds
、 が軸に沿ったバウンディング ボックスで可視領域の四角形をラップするためです。
もう 1 つのオプションは、マップ ビューのフレームの両側でポイントを選択し、マップ ビューの投影法を使用してこれらを緯度/経度に変換することです。次に例を示します。
GMSMapView* mapView = ...;
CGPoint middleLeftPoint = CGPointMake(
0, mapView.frame.size.height / 2);
CGPoint middleRightPoint = CGPointMake(
mapView.frame.size.width, mapView.frame.size.height / 2);
CLLocationCoordinate2D middleLeftCoord =
[mapView.projection coordinateForPoint: middleLeftPoint];
CLLocationCoordinate2D middleRightCoord =
[mapView.projection coordinateForPoint: middleRightPoint];
double distanceMetres = getDistanceMetresBetweenLocationCoordinates(
middleLeftCoord, middleRightCoord);
于 2013-05-31T08:06:26.590 に答える
0
2 点間の距離を求めるコードは次のとおりです。
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager.requestSerializer setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
NSString *urlString =@"https://maps.googleapis.com/maps/api/directions/json";
NSDictionary *dictParameters = @{@"origin" : [NSString stringWithFormat:@"%@",_sourceAdd], @"destination" : [NSString stringWithFormat:@"%@",_destinationAdd], @"mode" : @"driving", @"key":@"AIzaSyD9cWTQkAxemELVXTNUCALOmzlDv5b9Dhg"};
[manager GET:urlString parameters:dictParameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSDictionary *arr=responseObject[@"routes"][0][@"legs"];
NSMutableArray *loc=[[NSMutableArray alloc]init];
NSString *dis,*dur;
loc=[[arr valueForKey:@"distance"]valueForKey:@"text"];
dis=loc[0];
loc=[[arr valueForKey:@"duration"]valueForKey:@"text"];
dur=loc[0];
UIAlertView *av=[[UIAlertView alloc]initWithTitle:@"Route Info" message:[NSString stringWithFormat:@"Distance:%@ \nDuration:%@",dis,dur] delegate:nil cancelButtonTitle:@"Okay" otherButtonTitles:nil, nil];
[av show];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
于 2015-05-28T08:58:16.767 に答える