MKMapKit 不显示用户位置

发布于 2024-11-07 13:28:06 字数 1975 浏览 1 评论 0原文

我需要帮助在我的 MapView 上显示用户的位置。我已经做了我能在网上找到的所有方法,但仍然不起作用。

我的 AppDelegate 中有一个 CLLocationManager,它调用 viewController 中的 locationUpdate。

每次都会调用 (void)locationUpdate:(CLLocation *)location 方法,并且 使用 NSLog() 记录时,位置 坐标是正确的。 不过,我的 iPhone 屏幕上没有“蓝点”。区域也没有设置。

请看一下并告诉我是否遗漏了什么。

这是我的头文件:

//My .h file
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#import <MapKit/MKAnnotation.h>
#import <MapKit/MKReverseGeocoder.h>
@interface FirstViewController : UIViewController <MKMapViewDelegate>
{
    MKMapView *mapView;
}
-(void)locationUpdate:(CLLocation *)location;
-(void)locationError:(NSError *)error;
@end

这是我的实现文件的一部分:

//My .m file
#import "FirstViewController.h"
@implementation FirstViewController
- (void)viewDidLoad
{
    mapView = [[MKMapView alloc] initWithFrame:self.view.bounds];
    mapView.showsUserLocation = YES;
    mapView.mapType = MKMapTypeStandard;
    mapView.delegate = self;

    CLLocationCoordinate2D location;
    MKCoordinateRegion region;// = {{0.0,0.0},{0.0,0.0}};
    location.latitude = -33.8771;
    location.longitude = 18.6155;
    MKCoordinateSpan span;
    span.latitudeDelta = 0.01;
    span.longitudeDelta = 0.01;
    region.span = span;
    region.center = location;
    [mapView setRegion:region animated:TRUE];
    [mapView regionThatFits:region];
    [super viewDidLoad];
}
-(void)locationUpdate:(CLLocation *)location
{
    CLLocationCoordinate2D loc = [location coordinate];
    [mapView setCenterCoordinate:loc];
    if([mapView showsUserLocation] == NO)
    [mapView setShowsUserLocation:YES];
    NSLog(@"User Loc: %f, %f", mapView.userLocation.location.coordinate.latitude,
                     mapView.userLocation.location.coordinate.longitude);
    NSLog(@"In MapView: %@",[location description]);
}
@end

感谢您的宝贵时间! 非常感谢!

I need help with showing the user's location on my MapView. I have done everything I could possibly find online, but it still does not work.

I have a CLLocationManager in my AppDelegate that calls the locationUpdate in my viewController.

The (void)locationUpdate:(CLLocation *)location method gets called every time and the location coordinates is correct when logged with the NSLog(). Still, there is no "Blue Dot" on my iPhone's screen. The region is not set as well.

Please take a look and tell me if I am missing anything.

This is my header file:

//My .h file
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#import <MapKit/MKAnnotation.h>
#import <MapKit/MKReverseGeocoder.h>
@interface FirstViewController : UIViewController <MKMapViewDelegate>
{
    MKMapView *mapView;
}
-(void)locationUpdate:(CLLocation *)location;
-(void)locationError:(NSError *)error;
@end

This is my part of my implementation file:

//My .m file
#import "FirstViewController.h"
@implementation FirstViewController
- (void)viewDidLoad
{
    mapView = [[MKMapView alloc] initWithFrame:self.view.bounds];
    mapView.showsUserLocation = YES;
    mapView.mapType = MKMapTypeStandard;
    mapView.delegate = self;

    CLLocationCoordinate2D location;
    MKCoordinateRegion region;// = {{0.0,0.0},{0.0,0.0}};
    location.latitude = -33.8771;
    location.longitude = 18.6155;
    MKCoordinateSpan span;
    span.latitudeDelta = 0.01;
    span.longitudeDelta = 0.01;
    region.span = span;
    region.center = location;
    [mapView setRegion:region animated:TRUE];
    [mapView regionThatFits:region];
    [super viewDidLoad];
}
-(void)locationUpdate:(CLLocation *)location
{
    CLLocationCoordinate2D loc = [location coordinate];
    [mapView setCenterCoordinate:loc];
    if([mapView showsUserLocation] == NO)
    [mapView setShowsUserLocation:YES];
    NSLog(@"User Loc: %f, %f", mapView.userLocation.location.coordinate.latitude,
                     mapView.userLocation.location.coordinate.longitude);
    NSLog(@"In MapView: %@",[location description]);
}
@end

Thank you for your time!
It is much appreciated!

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

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

发布评论

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

评论(1

塔塔猫 2024-11-14 13:28:06

Interface Builder 中的地图工具包需要选中(查找用户位置)!

Your Map kit in Interface Builder needs to have the (Find User Location) checked!!

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