MapView 路线居中显示在地图可见区域

思路

要把Track 显示到地图可见区域的中心位置,需要让map 知道Track的中心点,以及需要显示的区域,下面我们来一步一步实现。
文中locations是一个存放了CLLocation类型的一些点。

首先定义一个结构体,用来返回地图显示区域,需要的一些参数

typedef struct {
    CLLocationDistance latitudinalMeters;   //纬度距离
      CLLocationDistance longitudinalMeters;  //经度距离
    CLLocationCoordinate2D centerCoord;     //地图中心点
} CoordinateRegion;

通过找出Track上经纬度最大点和最小点,取平均值可以拿到中心点。
通过计算Track上起点和终点的经度最大距离/纬度最大距离,可以定制Map的显示区域。

/**
*  找出地图可显示区域参数
*  @param locations 存放地图CLLocation点的数组
*  @return CoordinateRegion(经度距离、纬度距离、地图中心点)
*/
- (CoordinateRegion *)makeCoordinateCoordinateRegionWithLocation:(NSArray *)locations {

    CoordinateRegion *coordinateRegion = malloc(sizeof(CoordinateRegion));

    NSArray *latitudes = [self rankRouteWithLocations:locations latitude:YES];
    NSArray *longitudes = [self rankRouteWithLocations:locations latitude:NO];

    //  找到地图中的最大点和小点
    CLLocation *MinLatitude = [latitudes firstObject];
    CLLocation *MaxLatitude = [latitudes lastObject];
    CLLocation *MinLongitude = [longitudes firstObject];
    CLLocation *MaxLongitude = [longitudes lastObject];

    // 转成CLLocationDegrees
    CLLocationDegrees MinLatitudeDegrees = MinLatitude.coordinate.latitude;
    CLLocationDegrees MaxLatitudeDegrees = MaxLatitude.coordinate.latitude;
    CLLocationDegrees MinLongitudeDegrees = MinLongitude.coordinate.longitude;
    CLLocationDegrees MaxLongitudeDegrees = MaxLongitude.coordinate.longitude;
    CLLocationDegrees centerLa = (MinLatitudeDegrees + MaxLatitudeDegrees)/2;  // 纬度中心点
    CLLocationDegrees centerLo = (MinLongitudeDegrees + MaxLongitudeDegrees)/2;// 经度中心店

    // 计算经纬度  最远点和最近点之前的距离
    CLLocationDistance latitudeDistance = [MinLatitude distanceFromLocation:MaxLatitude] + 100;
    CLLocationDistance longitudeDistance = [MinLongitude distanceFromLocation:MaxLongitude] + 100;

    CLLocationCoordinate2D centerCoord = CLLocationCoordinate2DMake(centerLa, centerLo);

    coordinateRegion->latitudinalMeters = latitudeDistance;
    coordinateRegion->longitudinalMeters = longitudeDistance;
    coordinateRegion->centerCoord = centerCoord;

    return coordinateRegion;

}

关于经/纬度点的排序方法,请看下面

/**
*  排序
*  @param locations 存放地图CLLocation点的数组
*  @param isLatitude   按照纬度排序
*  @return 排序后的经度/ 纬度 CLLocation 数组
*/
-(NSArray*)rankRouteWithLocations:(NSArray *)locations  latitude:(BOOL)isLatitude
{
    NSMutableArray *pointArray=[NSMutableArray arrayWithArray:locations];
        NSSortDescriptor *sort1 = [NSSortDescriptor sortDescriptorWithKey:@"self" ascending:YES
                                                       comparator:^NSComparisonResult(id obj1, id obj2) {
                                                           CLLocation *point1= obj1;
                                                           CLLocation *point2 = obj2;
                                                           double lat1 = isLatitude?point1.coordinate.latitude:point1.coordinate.longitude;
                                                           double lat2 = isLatitude?point2.coordinate.latitude:point2.coordinate.longitude;
                                                           if (lat1 > lat2) {
                                                               return NSOrderedDescending;
                                                           }else if (lat1 < lat2) {
                                                               return NSOrderedAscending;
                                                           }else{
                                                               return NSOrderedSame;
                                                           }
                                                           
                                                       }];
    [pointArray sortUsingDescriptors:[NSArray arrayWithObject:sort1]];

    return pointArray;
}

最后一步,给Map 设置显示区域

/**
*  地图显示区域
*  @param locations 存放地图CLLocation点的数组
*/
- (void)mapTrackShowRegionWithLocation:(NSArray *)locations {
    // 地图缩放显示。缩放区域
    MKCoordinateSpan span;
    span.latitudeDelta = 0.003f;
    span.longitudeDelta = 0.003f;
    CoordinateRegion *reginCoordinate = [self       makeCoordinateCoordinateRegionWithLocation:locations];
    MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(reginCoordinate->centerCoord, reginCoordinate->latitudinalMeters, reginCoordinate->longitudinalMeters);
    [_mapView regionThatFits:region];
    [_mapView setRegion:region animated:YES];

}

效果图如下:

MapView 路线居中显示在地图可见区域_第1张图片
Track

你可能感兴趣的:(MapView 路线居中显示在地图可见区域)