思路<br />
要把Track 顯示到地圖可見(jiàn)區(qū)域的中心位置胧瓜,需要讓map 知道Track的中心點(diǎn),以及需要顯示的區(qū)域郑什,下面我們來(lái)一步一步實(shí)現(xiàn)府喳。
文中locations
是一個(gè)存放了CLLocation
類型的一些點(diǎn)。
首先定義一個(gè)結(jié)構(gòu)體蘑拯,用來(lái)返回地圖顯示區(qū)域钝满,需要的一些參數(shù)
typedef struct {
CLLocationDistance latitudinalMeters; //緯度距離
CLLocationDistance longitudinalMeters; //經(jīng)度距離
CLLocationCoordinate2D centerCoord; //地圖中心點(diǎn)
} CoordinateRegion;
通過(guò)找出Track上經(jīng)緯度最大點(diǎn)和最小點(diǎn),取平均值可以拿到中心點(diǎn)申窘。
通過(guò)計(jì)算Track上起點(diǎn)和終點(diǎn)的經(jīng)度最大距離/緯度最大距離弯蚜,可以定制Map的顯示區(qū)域。
/**
* 找出地圖可顯示區(qū)域參數(shù)
* @param locations 存放地圖CLLocation點(diǎn)的數(shù)組
* @return CoordinateRegion(經(jīng)度距離剃法、緯度距離碎捺、地圖中心點(diǎn))
*/
- (CoordinateRegion *)makeCoordinateCoordinateRegionWithLocation:(NSArray *)locations {
CoordinateRegion *coordinateRegion = malloc(sizeof(CoordinateRegion));
NSArray *latitudes = [self rankRouteWithLocations:locations latitude:YES];
NSArray *longitudes = [self rankRouteWithLocations:locations latitude:NO];
// 找到地圖中的最大點(diǎn)和小點(diǎn)
CLLocation *MinLatitude = [latitudes firstObject];
CLLocation *MaxLatitude = [latitudes lastObject];
CLLocation *MinLongitude = [longitudes firstObject];
CLLocation *MaxLongitude = [longitudes lastObject];
// 轉(zhuǎn)成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; // 緯度中心點(diǎn)
CLLocationDegrees centerLo = (MinLongitudeDegrees + MaxLongitudeDegrees)/2;// 經(jīng)度中心店
// 計(jì)算經(jīng)緯度 最遠(yuǎn)點(diǎn)和最近點(diǎn)之前的距離
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;
}
關(guān)于經(jīng)/緯度點(diǎn)的排序方法,請(qǐng)看下面
/**
* 排序
* @param locations 存放地圖CLLocation點(diǎn)的數(shù)組
* @param isLatitude 按照緯度排序
* @return 排序后的經(jīng)度/ 緯度 CLLocation 數(shù)組
*/
-(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 設(shè)置顯示區(qū)域
/**
* 地圖顯示區(qū)域
* @param locations 存放地圖CLLocation點(diǎn)的數(shù)組
*/
- (void)mapTrackShowRegionWithLocation:(NSArray *)locations {
// 地圖縮放顯示牵寺。縮放區(qū)域
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];
}
效果圖如下:
Track