Iphone 设置MKMapView的缩放级别

Iphone 设置MKMapView的缩放级别,iphone,ios,mapkit,Iphone,Ios,Mapkit,我有一张正确显示的地图,现在我唯一想做的就是在加载时设置缩放级别。有办法做到这一点吗 谢谢它不是内置的,但我看过/使用过代码。 这允许您使用以下选项: [mapView setCenterCoordinate:myCoord zoomLevel:13 animated:YES]; 注意:这不是我的代码,我没有编写它,因此不能将其归功于它您还可以使用MKCoordinateRegion并设置其跨度纬度和经度增量进行缩放。下面是一个快速参考,是iOS参考。它不会做任何花哨的事情,但应该允许您在绘制

我有一张正确显示的地图,现在我唯一想做的就是在加载时设置缩放级别。有办法做到这一点吗


谢谢

它不是内置的,但我看过/使用过代码。 这允许您使用以下选项:

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

注意:这不是我的代码,我没有编写它,因此不能将其归功于它

您还可以使用MKCoordinateRegion并设置其跨度纬度和经度增量进行缩放。下面是一个快速参考,是iOS参考。它不会做任何花哨的事情,但应该允许您在绘制地图时设置缩放



编辑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];

我为自己找到了一个解决方案,它非常简单,而且很有效。使用
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)

我希望下面的代码片段能对您有所帮助

- (void)handleZoomOutAction:(id)sender {
    MKCoordinateRegion newRegion=MKCoordinateRegionMake(mapView.region.center,MKCoordinateSpanMake(mapView.region.s       pan.latitudeDelta/0.5, mapView.region.span.longitudeDelta/0.5));
    [mapView setRegion:newRegion];
}


- (void)handleZoomInAction:(id)sender {
    MKCoordinateRegion newRegion=MKCoordinateRegionMake(mapView.region.center,MKCoordinateSpanMake(mapView.region.span.latitudeDelta*0.5, mapView.region.span.longitudeDelta*0.5));
    [mapView setRegion:newRegion];
}

可以选择任何值(而不是0.5)来降低或提高缩放级别。我在点击两个按钮时使用了这些方法

基于经线在地图的任何一点上间隔相等的事实,有一个非常简单的实现来设置中心坐标和缩放级别:

@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

我知道这是一个迟来的答复,但我只是想自己解决设置缩放级别的问题。goldmine的答案很好,但我发现它在我的应用程序中工作得不够好

仔细观察后,goldmine表示“在地图的任何一点上,经线的间隔都是相等的”。这不是真的,事实上是从-90(南极)到+90(北极)等距分布的纬度线。经线在赤道处间隔最宽,在两极汇合到一点

因此,我采用的实施方法是使用纬度计算,如下所示:

@implementation MKMapView (ZoomLevel)

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

@end

希望它能在这个后期有所帮助。

基于@AdilSoomro的伟大作品。我想到了这个:

@接口MKMapView(ZoomLevel)
-(无效)设置中心坐标:(CLLocationCoordinate2D)中心坐标
zoomLevel:(整数)zoomLevel
动画:(BOOL)动画;
-(双)getZoomLevel;
@结束
@实现MKMapView(ZoomLevel)
-(无效)设置中心坐标:(CLLocationCoordinate2D)中心坐标
zoomLevel:(NSU整数)zoomLevel动画:(BOOL)动画{
MKCoordinateSpan=MKCoordinateSpan(0,360/pow(2,zoomLevel)*自帧大小宽度/256);
[self-setRegion:mkCoordinatereRegionMake(中心坐标,跨度)已设置动画:已设置动画];
}
-(双)getZoomLevel{
返回log2(360*((self.frame.size.width/256)/self.region.span.longitudeDelta));
}
@结束
快速实施

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

一个简单的Swift实现,如果您使用Outlet

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

基于@Carnal的回答

Swift 2.0回答,使用NSUserDefaults保存和恢复地图的缩放和位置

保存地图位置和缩放的功能:

func saveMapRegion() {
    let mapRegion = [
        "latitude" : mapView.region.center.latitude,
        "longitude" : mapView.region.center.longitude,
        "latitudeDelta" : mapView.region.span.latitudeDelta,
        "longitudeDelta" : mapView.region.span.longitudeDelta
    ]
    NSUserDefaults.standardUserDefaults().setObject(mapRegion, forKey: "mapRegion")
}
每次移动地图时运行该函数:

func mapView(mapView: MKMapView, regionDidChangeAnimated animated: Bool) 
{
        saveMapRegion();
}
保存地图缩放和位置的功能:

func restoreMapRegion() 
{
    if let mapRegion = NSUserDefaults.standardUserDefaults().objectForKey("mapRegion") 
    {

        let longitude = mapRegion["longitude"] as! CLLocationDegrees
        let latitude = mapRegion["latitude"] as! CLLocationDegrees
        let center = CLLocationCoordinate2D(latitude: latitude, longitude: longitude)

        let longitudeDelta = mapRegion["latitudeDelta"] as! CLLocationDegrees
        let latitudeDelta = mapRegion["longitudeDelta"] as! CLLocationDegrees
        let span = MKCoordinateSpan(latitudeDelta: latitudeDelta, longitudeDelta: longitudeDelta)

        let savedRegion = MKCoordinateRegion(center: center, span: span)

        self.mapView.setRegion(savedRegion, animated: false)
    }
}
将此添加到viewDidLoad:

restoreMapRegion()

对于Swift 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)
}
private func setMapRegion(位置:CLLocationCoordinate2D,动画:Bool)
{
让viewRegion=MKCoordinateRegionMakeWithDistance(位置)
MapView.setRegion(viewRegion,动画:动画)
}
只需定义横向和纵向米数,地图视图就会使缩放级别适合您的值。

Swift:

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

locValue是您的坐标。

在这里,我把我的答案和它在swift 4.2中的作用放在这里

基于昆蒂纳丹的

Swift 5.1

// size refers to the width/height of your tile images, by default is 256.0
// Seems to get better results using round()
// frame.width is the width of the MKMapView

let zoom = round(log2(360 * Double(frame.width) / size / region.span.longitudeDelta))

MKMapView
基于(+浮点缩放级别精度)的扩展:

<代码>导入基础 导入地图套件 扩展MKMapView{ 变量zoomLevel:Double{ 得到{ 返回log2(360*(双精度(self.frame.size.width/256)/self.region.span.longitudeDelta))+1 } 设置(新缩放级别){ setCenterCoordinate(坐标:self.centerCoordinate,zoomLevel:newZoomLevel,动画:false) } } 专用函数setCenterCoordinate(坐标:CLLocationCoordinate2D,zoomLevel:Double,动画:Bool){ 设span=MKCoordinateSpan(纬度:0,纵向:360/pow(2,缩放级别)*双精度(自帧大小/宽度)/256) setRegion(MKCoordinateRegion(中心:坐标,跨度:跨度),动画:动画) } }
哇,代码太多了,你会认为应该内置。谢谢您可以获取.m和.h文件,将其添加到您的项目中,然后在地图视图控制器中引用它,并将其用作MKMapView上的方法,哦,类别的乐趣!对我来说不起作用,它只是显示与以前相同的缩放级别。我一定是做错了什么。这对我来说没有什么区别,当我更改一些值时,它只是不加载贴图。您是在加载贴图时设置此设置,还是在加载后尝试进行操作?您是否使用1或更小的数字作为增量?只是试着理解需求。我在运行前设置它。我测试了高于1和低于1的值。答案很好,但尝试将纬度、经度增量更改为0.1-它被放大了更多。次要更正:
-(void)setCenterCoordinate:(CLLocationCoordinate2D)centerCoordinate zoomLevel:(nsuiger)zoomLevel动画:(BOOL)动画{MKCoordinateSpan span=MKCoordinateSpanMake(0,360/pow)(2,zoomLevel)*self.frame.size.width/256);[self-setRegion:MKCoordinateRegionMake(centerCoordinate,span)动画:动画];}
谢谢!是的,你是对的,我实际上从我的项目中取出了代码,它是一个函数,而不是对MKMapView的添加。