xcode iOS 编程中的中心映射

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13325434/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 02:03:40  来源:igfitidea点击:

Center maps in iOS Programming

iphoneobjective-ciosxcodemaps

提问by Programmer...

How do we follow the user in maps. I want to have the blue dot (user location) be in the center of the map, But I also what to allow the user to zoom in and zoom out and then after a couple seconds zoom in back in the user location.

我们如何在地图中关注用户。我想让蓝点(用户位置)位于地图的中心,但我也想让用户放大和缩小,然后几秒钟后放大回用户位置。

My Educated Guess for the Solution:We detect if the user is zooming in or out, after three seconds of no zooming in or out detection, we starting follow the user :). Your HELP would be awesome :)

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

This code zoom in the user location but doesn't delay for zoom in and out:

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

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

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


    }

采纳答案by lukaswelte

I made a little example to show how you can delegate this job to the Map SDK. Of course you could listen to the Location change but MKUserTrackingModeFollow automatically does this for you, so just a single line of code

我做了一个小例子来展示如何将这项工作委托给 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];
}

Then the app looks like this: App with TrackingModeApp zoomed out in TrackingMode

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

For more information just read the Apple Documentation: http://developer.apple.com/library/ios/#documentation/MapKit/Reference/MKMapView_Class/MKMapView/MKMapView.html

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

回答by yinkou

A quick look in the docs reveals the magic.
Set the userTrackingModeof your map to MKUserTrackingModeFollow.
See here.

快速浏览一下文档就会发现神奇之处。
userTrackingMode地图的设置为MKUserTrackingModeFollow
这里



Update:

更新:

Since you've updated your question, here's the new answer.
To recenter the map to the user location i would recommend to write a simple helper Method:

由于您更新了问题,这是新答案。
要将地图重新​​定位到用户位置,我建议编写一个简单的辅助方法:

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

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

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

And now you should call it after a short delay if user has stopped moving the map. You can do this in the regionDidChangedelegate method of the mapView.

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

But you can get problems if you don't lock the reset-method if the user changes the region multiple times before it really resets the map. So it would be wise to make a flag if it is possible to recenter the map. Like a property BOOL canRecenter.

但是,如果用户在真正重置地图之前多次更改区域,则如果不锁定重置方法,则会出现问题。因此,如果可以重新定位地图,最好制作一个标志。就像一个财产BOOL canRecenter

Init it with YESand update the recenterUserLocationmethod to:

初始化它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;
}

Now you can call it safely after the user has moved the map in any way with a small delay:

现在你可以在用户以任何方式移动地图后安全地调用它,并有一个小的延迟:

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

回答by Warif Akhand Rishi

I had the same problem. I guessed:

我有同样的问题。我猜的:

  1. If the user drag the map, he wants to stay on that position.
  2. If the user do nothing or reset to show current location, I need to follow the user.
  1. 如果用户拖动地图,他想留在那个位置。
  2. 如果用户什么都不做或重置以显示当前位置,我需要关注用户。

I added a reset button to show the current user location like this: enter image description here

我添加了一个重置​​按钮来显示当前用户位置,如下所示: 在此处输入图片说明

On the reset button clicked, changed the needToCenterMapto TRUE

在复位按钮点击后,改变了needToCenterMapTRUE

Added a drag gesture recognizer on map

在地图上添加了拖动手势识别器

// Map drag handler
    UIPanGestureRecognizer* panRec = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(didDragMap:)];



- (void)didDragMap:(UIGestureRecognizer*)gestureRecognizer {
    if (gestureRecognizer.state == UIGestureRecognizerStateEnded){
        NSLog(@"Map drag ended");
        self.needToCenterMap = FALSE;
    }
}

Followed the user on map depending on needToCenterMapflag

根据needToCenterMap标志在地图上跟随用户

- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
if (self.needToCenterMap == TRUE) 
    [mapView setCenterCoordinate:userLocation.location.coordinate animated:YES];
}

回答by Misha

This shell do the trick: mkMapview.showsUserLocation = YES;

这个外壳可以解决问题: mkMapview.showsUserLocation = YES;