当用户位置已知或发生变化时,如何使 MKMapView 地图在视觉上居中?

发布于 2024-10-16 06:31:28 字数 2393 浏览 2 评论 0原文

我已阅读并遵循 如何在没有 CLLocationManager 的情况下将 MKMapView 缩放到用户当前位置?

但是,虽然我确实获取了当前用户位置,但实际上无法使地图在视觉上居中。

例如,在 viewDidLoad 函数中,如何使地图在视觉上以用户已知位置为中心?我已经粘贴了下面 CurrentLocation 中的代码(请参阅 viewDidLoad 中的 XXX 注释


#import "MapViewController.h"
#import "PlacemarkViewController.h"

@implementation MapViewController

@synthesize mapView, reverseGeocoder, getAddressButton;

- (void)viewDidLoad
{
    [super viewDidLoad];

    // XXX HERE: How can I cause self.mapView to actually recenter itself at self.mapview.userLocation.location?


    mapView.showsUserLocation = YES;
}

- (void)viewDidUnload
{
    self.mapView = nil;
    self.getAddressButton = nil;
}

- (void)dealloc
{
    [reverseGeocoder release];
    [mapView release];
    [getAddressButton release];

    [super dealloc];
}

- (IBAction)reverseGeocodeCurrentLocation
{
    self.reverseGeocoder =
        [[[MKReverseGeocoder alloc] initWithCoordinate:mapView.userLocation.location.coordinate] autorelease];
    reverseGeocoder.delegate = self;
    [reverseGeocoder start];
}

- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFailWithError:(NSError *)error
{
    NSString *errorMessage = [error localizedDescription];
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Cannot obtain address."
                                                        message:errorMessage
                                                       delegate:nil
                                              cancelButtonTitle:@"OK"
                                              otherButtonTitles:nil];
    [alertView show];
    [alertView release];
}

- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFindPlacemark:(MKPlacemark *)placemark
{
    PlacemarkViewController *placemarkViewController =
        [[PlacemarkViewController alloc] initWithNibName:@"PlacemarkViewController" bundle:nil];
    placemarkViewController.placemark = placemark;
    [self presentModalViewController:placemarkViewController animated:YES];
}

- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
    // we have received our current location, so enable the "Get Current Address" button
    [getAddressButton setEnabled:YES];
}

@end

I have read and followed the instructions on How do I zoom an MKMapView to the users current location without CLLocationManager?

However, while I do get the current user location, I am unable to actually make the map visually center there.

For example, in the viewDidLoad function, how can I cause the map to center visually around the users known location? I have pasted the code from CurrentLocation below (See the XXX comment in viewDidLoad


#import "MapViewController.h"
#import "PlacemarkViewController.h"

@implementation MapViewController

@synthesize mapView, reverseGeocoder, getAddressButton;

- (void)viewDidLoad
{
    [super viewDidLoad];

    // XXX HERE: How can I cause self.mapView to actually recenter itself at self.mapview.userLocation.location?


    mapView.showsUserLocation = YES;
}

- (void)viewDidUnload
{
    self.mapView = nil;
    self.getAddressButton = nil;
}

- (void)dealloc
{
    [reverseGeocoder release];
    [mapView release];
    [getAddressButton release];

    [super dealloc];
}

- (IBAction)reverseGeocodeCurrentLocation
{
    self.reverseGeocoder =
        [[[MKReverseGeocoder alloc] initWithCoordinate:mapView.userLocation.location.coordinate] autorelease];
    reverseGeocoder.delegate = self;
    [reverseGeocoder start];
}

- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFailWithError:(NSError *)error
{
    NSString *errorMessage = [error localizedDescription];
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Cannot obtain address."
                                                        message:errorMessage
                                                       delegate:nil
                                              cancelButtonTitle:@"OK"
                                              otherButtonTitles:nil];
    [alertView show];
    [alertView release];
}

- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFindPlacemark:(MKPlacemark *)placemark
{
    PlacemarkViewController *placemarkViewController =
        [[PlacemarkViewController alloc] initWithNibName:@"PlacemarkViewController" bundle:nil];
    placemarkViewController.placemark = placemark;
    [self presentModalViewController:placemarkViewController animated:YES];
}

- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
    // we have received our current location, so enable the "Get Current Address" button
    [getAddressButton setEnabled:YES];
}

@end

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

场罚期间 2024-10-23 06:31:28

看起来您可以简单地使用 MKMapView 类 - 文档说:

更改此属性中的值会使地图以新坐标为中心,而不更改当前的缩放级别。它还更新 region 属性中的值,以反映新的中心坐标和维持当前缩放级别所需的新跨度值。

所以基本上,你只需要做:

self.mapView.centerCoordinate = self.mapView.userLocation.location.coordinate;

It looks like you can simply use the centerCoordinate property of the MKMapView class - the docs say:

Changing the value in this property centers the map on the new coordinate without changing the current zoom level. It also updates the values in the region property to reflect the new center coordinate and the new span values needed to maintain the current zoom level.

So basically, you just do:

self.mapView.centerCoordinate = self.mapView.userLocation.location.coordinate;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文