简体   繁体   English

如何使用MapKit关注用户位置

[英]How to follow user location with MapKit

I'm using MapKit to display the user's location relative to pins around them. 我正在使用MapKit来显示用户相对于它们周围的引脚的位置。 I'd like to be able to mimic the functionality that Maps provides via the crosshair button in the lower left-hand corner of the screen. 我希望能够通过屏幕左下角的十字准线按钮模仿地图提供的功能。 I'm already aware that MapKit provides a CLLocation object with the user's location via MKUserLocation, I just wanted to seek advice on how I should keep focus on that location. 我已经知道MapKit通过MKUserLocation提供了一个CLLocation对象和用户的位置,我只是想就如何关注​​该位置寻求建议。 My initial inclination was to use an NSTimer to center the map on that coordinate every 500ms or so. 我最初的倾向是使用NSTimer将地图集中在每500ms左右的坐标上。

Is there a better way to do this? 有一个更好的方法吗? Is there something built in to MapKit that I'm missing that will accomplish this? 是否有一些内置于MapKit中的东西,我将失踪,这将实现这一目标?

Thanks so much, Brendan 非常感谢,布兰登

If you're on IOS5+ this is VERY easy. 如果您使用的是IOS5 +,这非常简单。 Just change the "userTrackingMode" using code such as: 只需使用以下代码更改“userTrackingMode”:

[_mapView setUserTrackingMode:MKUserTrackingModeFollow animated:YES];

This will smoothly follow the users current location. 这将顺利地跟随用户当前位置。 If you drag the map it will even set the tracking mode back to MKUserTrackingModeNone which is usually the behaviour you want. 如果拖动地图,它甚至会将跟踪模式设置回MKUserTrackingModeNone ,这通常是您想要的行为。

It's really simple to have the map update the user location automatically just like the google maps. 让地图像谷歌地图一样自动更新用户位置非常简单。 Simply set showsUserLocation to YES 只需将showsUserLocation设置为YES即可

self.mapView.showsUserLocation = YES

...and then implement the MKMapViewDelegate to re-center the map when the location is updated. ...然后实现MKMapViewDelegate,以便在更新位置时重新定位地图。

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

And to allow the user to zoom & pan without stealing the view back to the current location... 并允许用户缩放和平移而不会将视图窃回当前位置...

-(void) mapView:(MKMapView *)mapView regionWillChangeAnimated:(BOOL)animated
{
    if( isTracking && ! pendingRegionChange )
    {
         isTracking = NO;
         [trackingButton setImage: [UIImage imageNamed: @"Location.png"] 
                         forState: UIControlStateNormal];
    }

    pendingRegionChange = NO;
}

-(IBAction) trackingPressed
{
    pendingRegionChange = YES;
    isTracking = YES;
    [mapView setCenterCoordinate: mapView.userLocation.coordinate 
                        animated: YES];

    [trackingButton setImage: [UIImage imageNamed: @"Location-Tracking.png"] 
                    forState: UIControlStateNormal];
}

I think that I would actually use the CoreLocation CLLocationManager and use its delegate method locationManager:didUpdateToLocation:fromLocation: . 我认为我实际上会使用CoreLocation CLLocationManager并使用其委托方法locationManager:didUpdateToLocation:fromLocation: .

This way, you don't have the overhead of an NSTimer , and it only updates when there's a new location available. 这样,您就没有NSTimer的开销,只有在有新位置可用时才会更新。

You can pull the longitude and latitude from the CLLocation object sent to the locationManager:didUpdateToLocation:fromLocation: method and pass it to the map view. 您可以从发送到locationManager:didUpdateToLocation:fromLocation:方法的CLLocation对象中提取经度和纬度,并将其传递给地图视图。

I go with Jacob Relkin's answer. 我和Jacob Relkin一起回答。 This tutorial provides a step-by-step procedure of using CoreLocation in an iPhone app. 教程提供了在iPhone应用程序中使用CoreLocation的分步过程。 Hope this helps you. 希望这对你有所帮助。

All the Best. 祝一切顺利。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM