[iphone] MKMapView의 확대 / 축소 수준 설정

올바르게 표시되는지도가 있는데로드 할 때 확대 / 축소 수준을 설정하는 것뿐입니다. 이를 수행하는 방법이 있습니까?

감사



답변

나는 매우 간단하고 트릭을 수행하는 해결책을 찾았습니다. 사용하여 MKCoordinateRegionMakeWithDistance원하는 줌을 얻기 위해 수평 수직 미터의 거리를 설정하기 위해합니다. 그리고 물론 위치를 업데이트하면 올바른 좌표를 얻거나 CLLocationCoordinate2D시작할 때 직접 지정할 수 있습니다 .

CLLocationCoordinate2D noLocation;
MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(noLocation, 500, 500);
MKCoordinateRegion adjustedRegion = [self.mapView regionThatFits:viewRegion];
[self.mapView setRegion:adjustedRegion animated:YES];
self.mapView.showsUserLocation = YES;

빠른:

let location = ...
let region = MKCoordinateRegion( center: location.coordinate, latitudinalMeters: CLLocationDistance(exactly: 5000)!, longitudinalMeters: CLLocationDistance(exactly: 5000)!)
mapView.setRegion(mapView.regionThatFits(region), animated: true)


답변

경도선이지도의 어느 지점에서든 균등하게 간격을두고 있다는 사실을 기반으로 centerCoordinate 및 zoomLevel을 설정하는 매우 간단한 구현이 있습니다.

@interface MKMapView (ZoomLevel)

@property (assign, nonatomic) NSUInteger zoomLevel;

- (void)setCenterCoordinate:(CLLocationCoordinate2D)centerCoordinate
                  zoomLevel:(NSUInteger)zoomLevel
                   animated:(BOOL)animated;

@end


@implementation MKMapView (ZoomLevel)

- (void)setZoomLevel:(NSUInteger)zoomLevel {
    [self setCenterCoordinate:self.centerCoordinate zoomLevel:zoomLevel animated:NO];
}

- (NSUInteger)zoomLevel {
    return log2(360 * ((self.frame.size.width/256) / self.region.span.longitudeDelta)) + 1;
}

- (void)setCenterCoordinate:(CLLocationCoordinate2D)centerCoordinate
zoomLevel:(NSUInteger)zoomLevel animated:(BOOL)animated {
    MKCoordinateSpan span = MKCoordinateSpanMake(0, 360/pow(2, zoomLevel)*self.frame.size.width/256);
    [self setRegion:MKCoordinateRegionMake(centerCoordinate, span) animated:animated];
}

@end


답변

내장되어 있지 않지만 코드를 보거나 사용 했습니다 . 이를 통해 다음을 사용할 수 있습니다.

[mapView setCenterCoordinate:myCoord zoomLevel:13 animated:YES];

참고 : 이것은 내 코드가 아니므로 작성하지 않았으므로 크레딧을받을 수 없습니다.


답변

MKCoordinateRegion을 사용하고 스팬 위도 및 경도 델타를 설정하여 확대 할 수도 있습니다. 아래는 빠른 참조이고 여기는 iOS 참조입니다. 멋진 것은 아니지만지도를 그릴 때 확대 / 축소를 설정할 수 있습니다.


MKCoordinateRegion region;
region.center.latitude = {desired lat};
region.center.longitude = {desired lng};
region.span.latitudeDelta = 1;
region.span.longitudeDelta = 1;
mapView.region = region;

편집 1 :

MKCoordinateRegion region;
region.center.latitude = {desired lat};
region.center.longitude = {desired lng};
region.span.latitudeDelta = 1;
region.span.longitudeDelta = 1;
region = [mapView regionThatFits:region];
[mapView setRegion:region animated:TRUE];


답변

콘센트를 사용하는 경우 간단한 Swift 구현.

@IBOutlet weak var mapView: MKMapView! {
    didSet {
        let noLocation = CLLocationCoordinate2D()
        let viewRegion = MKCoordinateRegionMakeWithDistance(noLocation, 500, 500)
        self.mapView.setRegion(viewRegion, animated: false)
    }
}

@Carnal의 답변을 기반으로합니다.


답변

신속한 구현

import Foundation
import MapKit

class MapViewWithZoom: MKMapView {

    var zoomLevel: Int {
        get {
            return Int(log2(360 * (Double(self.frame.size.width/256) / self.region.span.longitudeDelta)) + 1);
        }

        set (newZoomLevel){
            setCenterCoordinate(coordinate:self.centerCoordinate, zoomLevel: newZoomLevel, animated: false)
        }
    }

    private func setCenterCoordinate(coordinate: CLLocationCoordinate2D, zoomLevel: Int, animated: Bool) {
        let span = MKCoordinateSpan(latitudeDelta: 0, longitudeDelta: 360 / pow(2, Double(zoomLevel)) * Double(self.frame.size.width) / 256)
        setRegion(MKCoordinateRegion(center: coordinate, span: span), animated: animated)
    }
}


답변

들어 스위프트 3 꽤 빨리 감기입니다 :

private func setMapRegion(for location: CLLocationCoordinate2D, animated: Bool)
{
    let viewRegion = MKCoordinateRegionMakeWithDistance(location, <#T##latitudinalMeters: CLLocationDistance##CLLocationDistance#>, <#T##longitudinalMeters: CLLocationDistance##CLLocationDistance#>)
    MapView.setRegion(viewRegion, animated: animated)
}

<CLLocationDistance>lat- , long-Meters를 정의하기 만하면 mapView가 확대 / 축소 수준을 값에 맞 춥니 다.