mapview自动缩放当前位置 [英] mapview autozoom on current location automatic

查看:102
本文介绍了mapview自动缩放当前位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题.我一直在寻找关于同一问题的堆栈溢出问题的其他答案,但我无法让它们为我工作.所以我将尝试在这里询问.我想做的是当我在地图视图上并拥有要自动缩放到该位置的用户位置时.

h文件

#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>

@interface WalkingTableViewController: UIViewController <MKMapViewDelegate>




@property (weak, nonatomic) IBOutlet MKMapView *MKMapView;







@end

m文件

#import "WalkingTableViewController.h"


@interface UITableViewController ()

@end

@implementation WalkingTableViewController

- (void)viewDidLoad

{
    [super viewDidLoad];
    self.MKMapView.showsUserLocation=YES;
    self.MKMapView.delegate = self;
    [self.MKMapView setUserTrackingMode:MKUserTrackingModeFollow animated:YES];



}



- (IBAction)StopButton:(id)sender 

- (IBAction)StartButton:(id)sender 



@end

解决方案

如果您希望以动画方式查看缩放,我会将代码放在viewDidAppear方法中,以便在显示视图控制器后开始动画.

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    CLLocationManager* tempLocationManager = [[CLLocationManager alloc] init];
    MKCoordinateRegion tRegion = MKCoordinateRegionMakeWithDistance([tempLocationManager.location coordinate], 20000, 20000);
    [self.MKMapView setRegion:tRegion animated:animated];
}

根据需要调整区域(缩放级别,在上面的示例中为20000).

I have one problem. I've been looking at the other answers here on stack overflow about the same question and I can't get them to work for me. So I'm going to try asking here. What I want to do is when I'm on map view and have got the user location I want to automatically zoom into the location.

the h-file

#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>

@interface WalkingTableViewController: UIViewController <MKMapViewDelegate>




@property (weak, nonatomic) IBOutlet MKMapView *MKMapView;







@end

the m-file

#import "WalkingTableViewController.h"


@interface UITableViewController ()

@end

@implementation WalkingTableViewController

- (void)viewDidLoad

{
    [super viewDidLoad];
    self.MKMapView.showsUserLocation=YES;
    self.MKMapView.delegate = self;
    [self.MKMapView setUserTrackingMode:MKUserTrackingModeFollow animated:YES];



}



- (IBAction)StopButton:(id)sender 

- (IBAction)StartButton:(id)sender 



@end

解决方案

If you like to see the zoom animated, I would place the code in the viewDidAppear method, so that the animation starts once the view controller is displayed.

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    CLLocationManager* tempLocationManager = [[CLLocationManager alloc] init];
    MKCoordinateRegion tRegion = MKCoordinateRegionMakeWithDistance([tempLocationManager.location coordinate], 20000, 20000);
    [self.MKMapView setRegion:tRegion animated:animated];
}

Adjust the region (zoom level, 20000 in the example above) to your needs.

这篇关于mapview自动缩放当前位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆