>我需要帮助在我的MapView上显示用户的位置。我已经做了我能在网上找到的所有事情,但它仍然不起作用。
我的应用程序委托中有一个CLLocationManager,它调用我的视图控制器中的位置更新。
每次都会调用(void)locationUpdate:(CLLocation *)location
方法,并且使用NSLog().
记录时location
坐标是正确的 尽管如此,我的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
谢谢你的时间!非常感谢!
界面生成器中的地图工具包需要选中(查找用户位置)!!