我正在我的应用程序上使用MKMapView。我需要在模拟器上显示当前位置。
是否可能在模拟器上显示当前位置。
command-1
以显示属性检查器。)showsUserLocation
属性设置为YES
。
最新版本的SDK(必须在Snow Leopard上运行)可以使用CLLocationManager获取模拟器正在运行的机器的位置。然后,您可以使用此位置创建注释以显示在地图视图上。它不会像内置的“用户位置指示器”一样运作(至少需要一些工作),但它将显示用户的当前位置。
有关此技术无法正常工作的详细信息,请参见此帖子。
请参见CLLocationManager文档的“相关示例代码”部分,其中包含使用CLLocationManager和CLLocationManagerDelegate的示例代码,然后在地图视图上显示用户的位置。
self.mapView.delegate = self;
self.mapView.showsUserLocation = YES;
这将显示MkMapview
中的当前位置。
如果您正在使用Interface Builder,在“Attributes Inspector”中,我们有一个选项Behaviour
,它有一个选项Show User Location
,选中该选项也会做同样的事情。
如果您无法在模拟器中看到,请执行以下操作:
使用CLLocationManager
也可以获取当前位置,
将Corelocation
FrameWork导入项目中。
在.h
文件中
#import <CoreLocation/CoreLocation.h>
@property (nonatomic, strong) CLLocationManager *locationManager;
@property (nonatomic, strong) CLLocation* currentLocation;
在.m
文件中
if ([CLLocationManager locationServicesEnabled] )
{
if (self.locationManager == nil )
{
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
self.locationManager.distanceFilter = kDistanceFilter; //kCLDistanceFilterNone// kDistanceFilter;
}
[self.locationManager startUpdatingLocation];
}
委托函数:
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
self.currentLocation = [locations lastObject];
// here we get the current location
}
希望这个答案能对你有所帮助。无论是 iOS 6、6.1 还是 iOS 7,模拟器都不会显示用户当前位置。要模拟位置,请单击此处。如果您想要显示用户当前位置,则需要在设备上运行您的应用程序或更改模拟器设置 -
从模拟器菜单中选择“调试”>“位置”>“自定义位置...”