[英]Testing CoreLocation on iPhone Simulator
更新:从iOS 5和Xcode 4.1开始,现在可以在模拟器中测试位置,甚至可以定义路线。 有关详细信息,请访问http://developer.apple.com 。
无论如何在iPhone模拟器上测试CoreLocation?
我需要的是能够自己设置位置并让CoreLocation返回它。
这是我的简单黑客,迫使CLLocationMager仅在模拟器上返回Powell的Tech Bookstore的地理解决方案:
#ifdef TARGET_IPHONE_SIMULATOR
@interface CLLocationManager (Simulator)
@end
@implementation CLLocationManager (Simulator)
-(void)startUpdatingLocation {
CLLocation *powellsTech = [[[CLLocation alloc] initWithLatitude:45.523450 longitude:-122.678897] autorelease];
[self.delegate locationManager:self
didUpdateToLocation:powellsTech
fromLocation:powellsTech];
}
@end
#endif // TARGET_IPHONE_SIMULATOR
感谢您的反馈,它促使我找到了一个强大的解决方案。
所有代码都可以在这里找到:
http://code.google.com/p/dlocation/
它非常混乱,但随着我的使用它会变得更好。
解决方案是@protocol
CLLocationManager
并定义一个名为DLocationManagerDelegate
的新委托DLocationManagerDelegate
。
它被设计为CLLocationManagerDelegate
的简单替代品,当在实际设备上部署时,它可以编译为非常薄的层。
在设备上运行时,它将使用CoreLocation
正常返回数据,但在模拟器中,它将从文本文件(在DLocationManager.h文件中定义)中读取纬度和经度。
我希望这有帮助,实现是简单的一面,你必须startUpdatingLocation
和stopUpdatingLocation
来更新显示。
将非常感谢您的意见和反馈。
在模拟器上运行时,使用过滤功能交换测试实例。 无论您以前收到过哪个位置(代表电话等),都要通过以下方式:
+ (CLLocation *) wakkawakka: (CLLocation*) loc {
#ifdef TARGET_IPHONE_SIMULATOR
/* replace with a test instance */
return [[CLLocation alloc] initWithLatitude:10.0 longitude:20.0];
#else
return loc;
#endif
}
内存管理问题不谈......
我认为这里有另一种(更好的恕我直言)方法,而不是继承CLLocationManager
http://code.google.com/p/dlocation/
在ObjectiveC中,似乎可以从类替换现有方法而不覆盖它。 这通常被称为“方法调配”:您为现有类定义自己的类别,并在其中定义现有方法。
从客户的角度来看,一切都是透明的: 他感觉他正在处理真正的CLLocationManager
但实际上,你“从中获取了控制权” 。 因此,他不需要处理任何特殊的子类或任何特殊的委托协议:他继续使用与CoreLocation相同的类/协议。
这是一个控制客户端将注入的委托的示例:
@implementation CLLocationManager (simulator)
-(void) setDelegate:(id)delegate { //your own implementation of the setDelegate... } -(id)delegate { //your own implementation of the delegate.... } -(void)startUpdatingLocation { } -(void)stopUpdatingLocation { } //.... //same for the rest of any method available in the standard CLLocationManager @end
然后在这个实现中,您可以自由地处理一组预定义的坐标(来自任何文件),这些坐标将使用标准CLLocationManagerDelegate
协议“推送”到委托。
在iphone模拟器中永远不会调用locationManager:didUpdateToLocation和locationManager:didFailedWithError重载的回调,这有点奇怪,我得到的是lat的0.0000和lon的0.0000。 作为立场。 在您开发某些东西的情况下,仅使用模拟器环境很难实现位置处理期间可能发生的所有可能情况。
如果您有兴趣使用模拟位置更新更新MKMapView中的蓝色userLocation点,请查看我的FTLocationSimulator, 网址为http://github.com/futuretap/FTLocationSimulator
它会读取Google地球生成的KML文件,以提供连续的位置更新。
如果您需要设置模拟器为您提供的位置以外的位置,则从测试类触发Core Location回调。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.