iOS编程中的中心地图

Pro*_*... 7 iphone maps xcode objective-c ios

我们如何在地图中关注用户.我希望蓝点(用户位置)位于地图的中心,但我还要允许用户放大和缩小,然后在用户位置放大几秒后放大.

我的教育猜测解决方案:我们检测用户是否放大或缩小,在没有放大或缩小检测三秒后,我们开始关注用户:).你的帮助会很棒:)

此代码放大用户位置,但不会延迟放大和缩小:

     - (void)locationManager:(CLLocationManager *)manager
        didUpdateToLocation:(CLLocation *)newLocation
              fromLocation:(CLLocation *)oldLocation {

  MKCoordinateRegion userLocation = MKCoordinateRegionMakeWithDistance(newLocation.coordinate, 1500.0, 1500.0); [mapView setRegion:userLocation animated:YES];


    }
Run Code Online (Sandbox Code Playgroud)

yin*_*kou 14

快速浏览文档可以发现魔术.
userTrackingMode地图设置为MKUserTrackingModeFollow.
看到这里.


更新:

既然你已经更新了你的问题,这是新答案.
要将地图重新​​定位到用户位置,我建议写一个简单的帮助方法:

- (void)recenterUserLocation:(BOOL)animated{
    MKCoordinateSpan zoomedSpan = MKCoordinateSpanMake(1000, 1000);

    MKCoordinateRegion userRegion = MKCoordinateRegionMake(self.mapView.userLocation.coordinate, zoomedSpan);

    [self.mapView setRegion:userRegion animated:animated];
}
Run Code Online (Sandbox Code Playgroud)

现在,如果用户停止移动地图,您应该在短暂延迟后调用它.您可以regionDidChange在mapView 的delegate方法中执行此操作.

但是,如果用户在真正重置地图之前多次更改区域,则如果不锁定reset-method,则会出现问题.因此,如果可以重新定位地图,那么制作一个标志是明智的.像一个财产BOOL canRecenter.

使用它YES并将recenterUserLocation方法更新为:

- (void)recenterUserLocation:(BOOL)animated{
    MKCoordinateSpan zoomedSpan = MKCoordinateSpanMake(1000, 1000);

    MKCoordinateRegion userRegion = MKCoordinateRegionMake(self.mapView.userLocation.coordinate, zoomedSpan);

    [self.mapView setRegion:userRegion animated:animated];

    self.canRecenter = YES;
}
Run Code Online (Sandbox Code Playgroud)

现在,您可以在用户以任何方式移动地图后以较小的延迟安全地调用它:

- (void)mapView:(MKMapView *)mMapView regionDidChangeAnimated:(BOOL)animated{
    if (self.canRecenter){
        self.canRecenter = NO;
        [self performSelector:@selector(recenterUserLocation:) withObject:@(animated) afterDelay:3];
    }
}
Run Code Online (Sandbox Code Playgroud)


luk*_*lte 0

我做了一个小例子来展示如何将这项工作委托给 Map SDK。当然,您可以监听位置更改,但 MKUserTrackingModeFollow 会自动为您执行此操作,因此只需一行代码

#import <MapKit/MapKit.h>

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    MKMapView *mapView = [[MKMapView alloc] initWithFrame:self.view.frame];

    //Always center the dot and zoom in to an apropriate zoom level when position changes
    [mapView setUserTrackingMode:MKUserTrackingModeFollow];

    //don't let the user drag around the the map -> just zooming enabled
    [mapView setScrollEnabled:NO];

    [self.view addSubview:mapView];
}
Run Code Online (Sandbox Code Playgroud)

然后应用程序看起来像这样: 具有跟踪模式的应用程序 应用程序在 TrackingMode 下缩小

有关更多信息,请阅读 Apple 文档: http://developer.apple.com/library/ios/#documentation/MapKit/Reference/MKMapView_Class/MKMapView/MKMapView.html