MapView相关

2019-11-28  本文已影响0人  喵喵粉

1. 设置缩放级别

- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view {
    //置于中点
//    [mapView setCenterCoordinate:view.annotation.coordinate animated:YES];
    [mapView setCenterCoordinate:view.annotation.coordinate zoomLevel:13 animated:YES];
}

MKMapView+ZoomLevel.h

@interface MKMapView (ZoomLevel)

/**
 CLLocationCoordinate2D centerCoord;
 [mapView setCenterCoordinate:centerCoord zoomLevel:ZOOM_LEVEL animated:NO];
 */
- (void)setCenterCoordinate:(CLLocationCoordinate2D)centerCoordinate
                  zoomLevel:(NSUInteger)zoomLevel
                   animated:(BOOL)animated;

@end

MKMapView+ZoomLevel.m

#import "MKMapView+ZoomLevel.h"

#define MERCATOR_OFFSET 268435456
#define MERCATOR_RADIUS 85445659.44705395

@implementation MKMapView (ZoomLevel)

#pragma mark - Map conversion methods

- (double)longitudeToPixelSpaceX:(double)longitude
{
    return round(MERCATOR_OFFSET + MERCATOR_RADIUS * longitude * M_PI / 180.0);
}

- (double)latitudeToPixelSpaceY:(double)latitude
{
    return round(MERCATOR_OFFSET - MERCATOR_RADIUS * logf((1 + sinf(latitude * M_PI / 180.0)) / (1 - sinf(latitude * M_PI / 180.0))) / 2.0);
}

- (double)pixelSpaceXToLongitude:(double)pixelX
{
    return ((round(pixelX) - MERCATOR_OFFSET) / MERCATOR_RADIUS) * 180.0 / M_PI;
}

- (double)pixelSpaceYToLatitude:(double)pixelY
{
    return (M_PI / 2.0 - 2.0 * atan(exp((round(pixelY) - MERCATOR_OFFSET) / MERCATOR_RADIUS))) * 180.0 / M_PI;
}

#pragma mark - Helper methods

- (MKCoordinateSpan)coordinateSpanWithMapView:(MKMapView *)mapView
                             centerCoordinate:(CLLocationCoordinate2D)centerCoordinate
                                 andZoomLevel:(NSUInteger)zoomLevel
{
    // convert center coordiate to pixel space
    double centerPixelX = [self longitudeToPixelSpaceX:centerCoordinate.longitude];
    double centerPixelY = [self latitudeToPixelSpaceY:centerCoordinate.latitude];
    
    // determine the scale value from the zoom level
    NSInteger zoomExponent = 20 - zoomLevel;
    double zoomScale = pow(2, zoomExponent);
    
    // scale the map’s size in pixel space
    CGSize mapSizeInPixels = mapView.bounds.size;
    double scaledMapWidth = mapSizeInPixels.width * zoomScale;
    double scaledMapHeight = mapSizeInPixels.height * zoomScale;
    
    // figure out the position of the top-left pixel
    double topLeftPixelX = centerPixelX - (scaledMapWidth / 2);
    double topLeftPixelY = centerPixelY - (scaledMapHeight / 2);
    
    // find delta between left and right longitudes
    CLLocationDegrees minLng = [self pixelSpaceXToLongitude:topLeftPixelX];
    CLLocationDegrees maxLng = [self pixelSpaceXToLongitude:topLeftPixelX + scaledMapWidth];
    CLLocationDegrees longitudeDelta = maxLng - minLng;
    
    // find delta between top and bottom latitudes
    CLLocationDegrees minLat = [self pixelSpaceYToLatitude:topLeftPixelY];
    CLLocationDegrees maxLat = [self pixelSpaceYToLatitude:topLeftPixelY + scaledMapHeight];
    CLLocationDegrees latitudeDelta = -1 * (maxLat - minLat);
    
    // create and return the lat/lng span
    MKCoordinateSpan span = MKCoordinateSpanMake(latitudeDelta, longitudeDelta);
    return span;
}

#pragma mark - Public methods

- (void)setCenterCoordinate:(CLLocationCoordinate2D)centerCoordinate
                  zoomLevel:(NSUInteger)zoomLevel
                   animated:(BOOL)animated
{
    // clamp large numbers to 28
    zoomLevel = MIN(zoomLevel, 28);
    
    // use the zoom level to compute the region
    MKCoordinateSpan span = [self coordinateSpanWithMapView:self centerCoordinate:centerCoordinate andZoomLevel:zoomLevel];
    MKCoordinateRegion region = MKCoordinateRegionMake(centerCoordinate, span);
    
    // set the region like normal
    [self setRegion:region animated:animated];
}

@end

2. 经纬度 度分秒-数字互转

经纬度(48.713140E,31.22908N)转化为度分秒

度分秒60进制,按照1度=60分,1分=60秒

48.713140 就是48度
60×0.71314=42.7884分,取整故分为42分
60×0.7884=47.304,取整故秒为47秒

(北纬31°13′44″N, 东经48°42′47″E)

负数
(南纬-31°13′44″S, 西经-48°42′47″W)

CoordinateHelper.h

@interface CoordinateHelper : NSObject

/**
 度分秒格式转换成经纬度
(34°01‘04“N, 93°20‘17“E) -> (34.0178524497, 93.3383025100)

 NSString *dLat = @"34°01‘04“N";
 NSString *dLon = @"93°20‘17“E";
 CLLocationCoordinate2D coor = [self getCoordinate2DWithLatitude:dLat longitude:dLon];
 
 //result 2 - (34.017776, 93.338058)
 NSLog(@"result 2 - (%f, %f)", coor.latitude, coor.longitude);
*/
+ (CLLocationCoordinate2D)getCoordinate2DWithLatitude:(NSString *)latitudeStr longitude:(NSString *)longitudestr;

/**
 经纬度转换成度分秒格式
(34.0178524497, 93.3383025100) -> (34°01‘04“N, 93°20‘17“E)
 
 //青海省玉树藏族自治州治多县索加乡
 NSString *latString = @"34.0178524497";
 NSString *lonString = @"93.3383025100";
 NSString *rst =[self formatDegreeMinuteSecondWithLatitude:latString longitude:lonString];
 
 //result 1 - (34°01‘04“N, 93°20‘17“E)
 NSLog(@"result 1 - %@", rst);
*/
+ (NSString *)formatDegreeMinuteSecondWithLatitude:(NSString *)latitude longitude:(NSString *)longitude;

@end

CoordinateHelper.m

#import "CoordinateHelper.h"

@implementation CoordinateHelper

#pragma mark - ================= 度分秒格式转换成经纬度 ======================

/**
 度分秒格式转换成经纬度
(34°01‘04“N, 93°20‘17“E) -> (34.0178524497, 93.3383025100)

 NSString *dLat = @"34°01‘04“N";
 NSString *dLon = @"93°20‘17“E";
 CLLocationCoordinate2D coor = [self getCoordinate2DWithLatitude:dLat longitude:dLon];
 
 //result 2 - (34.017776, 93.338058)
 NSLog(@"result 2 - (%f, %f)", coor.latitude, coor.longitude);
*/
+ (CLLocationCoordinate2D)getCoordinate2DWithLatitude:(NSString *)latitudeStr longitude:(NSString *)longitudestr {
    if (!([[latitudeStr uppercaseString] containsString:@"N"] || [[latitudeStr uppercaseString] containsString:@"S"])) {
        return CLLocationCoordinate2DMake(latitudeStr.doubleValue, longitudestr.doubleValue);
    }
    
    NSMutableString *mStr = [NSMutableString stringWithString:latitudeStr];
    NSString *tmp = [mStr stringByTrimmingCharactersInSet:[NSCharacterSet letterCharacterSet]];
    float latitude = [self string2Float:tmp];
    
    mStr = [NSMutableString stringWithString:longitudestr];
    tmp = [mStr stringByTrimmingCharactersInSet:[NSCharacterSet letterCharacterSet]];
    float longitude = [self string2Float:tmp];
    
    return CLLocationCoordinate2DMake(latitude, longitude);
}

/**
 34°01‘04“ -> [34,01,04,] -> [34, 01/60, 04/60/60] -> 34+01/60+04/60/60
 */
+ (float)string2Float:(NSString *)string {
    NSMutableArray *dmsStrs = [NSMutableArray arrayWithArray:[string componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"°‘“"]]];
    if (dmsStrs.count>3) [dmsStrs removeLastObject];
    if (dmsStrs.count<3) return 0.0f;
    
    float degree = [dmsStrs[0] doubleValue]/1;
    float minute = [dmsStrs[1] doubleValue]/60;
    float second = [dmsStrs[2] doubleValue]/60/60;
    
    return degree+minute+second;
}


#pragma mark - ================= 经纬度转换成度分秒格式 ======================
/**
 经纬度转换成度分秒格式
(34.0178524497, 93.3383025100) -> (34°01‘04“N, 93°20‘17“E)
 
 //青海省玉树藏族自治州治多县索加乡
 NSString *latString = @"34.0178524497";
 NSString *lonString = @"93.3383025100";
 NSString *rst =[self formatDegreeMinuteSecondWithLatitude:latString longitude:lonString];
 
 //result 1 - (34°01‘04“N, 93°20‘17“E)
 NSLog(@"result 1 - %@", rst);
*/
+ (NSString *)formatDegreeMinuteSecondWithLatitude:(NSString *)latitude longitude:(NSString *)longitude {

    int lan = [latitude intValue];
    int lon = [longitude intValue];
    
    //纬度 N S
    NSString *suffixLan = lan>0?@"N":@"S";
    //经度 E W
    NSString *suffixLon = lon>0?@"E":@"W";
    
    NSString *lanStr = [NSString stringWithFormat:@"%@%@", [self convert2DMSFormat:latitude], suffixLan];
    NSString *lonStr = [NSString stringWithFormat:@"%@%@", [self convert2DMSFormat:longitude], suffixLon];
    
    //(34°01‘04“N, 93°20‘17“E)
    return [NSString stringWithFormat:@"(%@, %@)", lanStr, lonStr];
}

+ (NSString *)convert2DMSFormat:(NSString *)coordinateString {
    /** 将经度或纬度整数d部分提取出来 */
    int degree = [coordinateString intValue];
    int minute = 0;
    int second = 0;
    
    NSString *passStr = [NSString stringWithFormat:@"%@", coordinateString];
    passStr = [self convertStringToInt:passStr outNum:&minute];
    passStr = [self convertStringToInt:passStr outNum:&second];
    
    /** 将经度或纬度字符串合并为(xx°xx')形式 */
    NSString *string = [NSString stringWithFormat:@"%.2d°%.2d‘%.2d“", degree, minute, second];
    return string;
}

//取小数点后面的数 度->分/秒
+ (NSString *)convertStringToInt:(NSString *)string outNum:(int *)num {
    NSArray *array = [string componentsSeparatedByString:@"."];
    NSString *tmpStr = [NSString stringWithFormat:@"0.%@", array.lastObject];
    
    double number = [tmpStr doubleValue] * 60.0;
    string = [NSString stringWithFormat:@"%f", number];
    
    *num = (int)number;
    
    return string;
}

@end
上一篇下一篇

猜你喜欢

热点阅读