【问题标题】:Show User's current location on map在地图上显示用户的当前位置
【发布时间】:2013-07-07 11:19:18
【问题描述】:

我正在尝试在地图上显示用户的当前位置,但由于某种原因,没有采用我的默认坐标。

我有一个在 UIViewController 内部调用的 MKMapView 类。

MapView.h

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

@interface MapView : MKMapView <MKMapViewDelegate, CLLocationManagerDelegate>{

    CLLocationManager *locationManager;
}

@end

地图视图.m

#import "MapView.h"

@interface MapView ()

@end

@implementation MapView

- (id)initWithFrame:(CGRect)frame{

    self = [super initWithFrame:frame];
    if (self) {

        locationManager = [[CLLocationManager alloc] init];
        locationManager.delegate = self;

        self.delegate = self;

        [self addAnnotation:[self userLocation]];
        [locationManager startUpdatingLocation];
    }
    return self;
}

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
    NSLog(@"didFailWithError: %@", error);
    UIAlertView *errorAlert = [[UIAlertView alloc]
                               initWithTitle:@"Error" message:@"Failed to Get Your Location" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [errorAlert show];
}

- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
    NSLog(@"didUpdateUserLocation");

    MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(userLocation.coordinate, 250, 250);
    [self setRegion:[self regionThatFits:region] animated:YES];

    MKPointAnnotation *point = [[MKPointAnnotation alloc] init];
    point.coordinate = userLocation.coordinate;
    point.title = @"Where am I?";
    point.subtitle = @"I'm here!!!";

    [self addAnnotation:point];
}

- (MKAnnotationView*)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation{
    NSLog(@"viewForAnnotation");
    static NSString *identifier = @"MyAnnotation";

    if ([annotation isKindOfClass:[MKUserLocation class]]) {
        NSLog(@"Is the user %f, %f", [annotation coordinate].latitude, [annotation coordinate].longitude);
        return nil;
    }

    return nil;
}

- (void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views{
    NSLog(@"didAddAnnotationViews");
    for (MKAnnotationView *view in views){
        if ([[view annotation] isKindOfClass:[MKUserLocation class]]){

            MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance([[view annotation] coordinate] , 250, 250);

            [mapView setRegion:region animated:YES];
        }
    }
}

@end

访问地图后,我会得到下面的输出。

viewForAnnotation
Is the user 0.000000, 0.000000
didAddAnnotationViews

我不明白为什么即使重新提交自定义坐标,我的didUpdateUserLocation: 也不会被调用。

我在这里缺少什么?

【问题讨论】:

  • 首先,为什么要扩展MKMapView而不是UIViewController?

标签: ios mkmapview mkannotationview


【解决方案1】:

您正在使用CLLocationManager 并将其设置为delegate,但您的委托方法都是MKMapViewDelegate 的方法。

您可以设置地图视图的委托,也可以使用locationManager:didUpdateLocations: CLLocationManagerDelegate 方法。

【讨论】:

  • 哦,我怎么错过了!你说的对。我之前正在测试一些解决方案,这就是我有 CLLocationManager 委托的原因。我已将[self addAnnotation:[self userLocation]]; 替换为[self setShowsUserLocation:YES];,现在似乎可以正常工作了。
  • 使用setShowsUserLocation方法时不需要CLLocationManager
【解决方案2】:

如果用户可以选择在地图上打开和关闭其位置对您很重要,那么 Nevan King 是正确的。但是,如果您可以持续显示用户位置,则无需编写脚本。只需单击 Storyboard 中的 mapView 并在右侧(我认为在属性中)选中“显示用户位置”框。

【讨论】:

    猜你喜欢
    • 2015-08-09
    • 2023-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-29
    • 2015-08-14
    • 1970-01-01
    相关资源
    最近更新 更多