Google Maps iOS SDK:地图上东西点之间的距离 [英] Google Maps iOS SDK: Distance between east and west points on the map

查看:151
本文介绍了Google Maps iOS SDK:地图上东西点之间的距离的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何计算地图东点和西点之间的距离(以米为单位)?
假设用户改变了滚动地图的位置,然后我用mapView:didChangeCameraPosition:delegate方法捕获了运动,但我不知道如何计算距离。

How can I calculate the distance (in meters) between the east point and the west point of the map? Suppose the user changed the position scrolling the map, then I catch the movement with mapView:didChangeCameraPosition: delegate method, but I don't know how to calculate the distance.

推荐答案

这是一个辅助函数,可以用来计算两个坐标之间的距离:

Here's a helper function which can be used to calculate the distance in metres between two coordinates:

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];
}

更新:

由于地图可能会旋转或倾斜,因此取决于地图东和西的含义。即使它没有倾斜或旋转,由于地球的曲率,地图的顶部和底部之间的地图宽度将以米为单位(靠近赤道的边缘将比边缘更宽,以米为单位)这是赤道的更远)。你变焦越大,差异越小。

It depends what you mean by 'east' and 'west' of the map, since the map may be rotated or tilted. Even if it's not tilted or rotated, due to the curvature of the earth, the width of the map will be different in metres between the top of the map and the bottom (the edge closer to the equator will be wider in metres than the edge which is further from the equator). The difference will be less the more zoomed in you are.

但是假设你想要在地图的左下角和右下角之间获得以米为单位的距离,可以使用这个:

But supposing you wanted to get the distance in metres between the bottom left and bottom right corners of the map, you could use this:

GMSMapView* mapView = ...;

CLLocationCoordinate2D bottomLeftCoord = 
    mapView.projection.visibleRegion.nearLeft;
CLLocationCoordinate2D bottomRightCoord = 
    mapView.projection.visibleRegion.nearRight;
double distanceMetres = getDistanceMetresBetweenLocationCoordinates(
    bottomLeftCoord, bottomRightCoord);

如果要考虑旋转/倾斜,可以将可见区域包装在<$ c中$ c> GMSCoordinateBounds ,然后计算东南角和西南角之间的距离,例如:

If you want to factor in rotation/tilt, you could wrap the visible region in a GMSCoordinateBounds, and then calculate the distance between the south east and south west corners, for example:

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 将可见区域的四边形包裹在轴对齐的边界框中。

However, this could be wider than the area actually visible on the screen, as the GMSCoordinateBounds wraps the quadrilateral of the visible region in an axis-aligned bounding box.

另一种选择是选择每边的点地图视图的框架,然后使用地图视图的投影将这些转换为lat / lon,例如:

Another option would be to choose points on each side of the map view's frame, then use the map view's projection to convert these into lat/lon, for example:

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

这篇关于Google Maps iOS SDK:地图上东西点之间的距离的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆