29

更新:从 iOS 5 和 Xcode 4.1 开始,现在可以在模拟器中测试位置,甚至定义路线。有关详细信息,请参阅http://developer.apple.com 。

遗留问题

无论如何要在 iPhone 模拟器上测试 CoreLocation 吗?

我所需要的只是能够自己设置位置并让 CoreLocation 返回它。

4

8 回答 8

20

这是我的简单技巧,它强制 CLLocationMager 仅在模拟器上返回鲍威尔科技书店的地理坐标:

#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
于 2010-07-21T22:24:46.797 回答
18

感谢您的反馈,它促使我找到了一个强大的解决方案。

所有代码都可以在这里找到:

http://code.google.com/p/dlocation/

非常凌乱,但是当我使用它时,它会变得更好。

解决方案是子类化CLLocationManager并定义一个新的委托@protocol,称为DLocationManagerDelegate.

它被设计为一个简单的替代品,当部署在实际设备上时,CLLocationManagerDelegate它可以编译成一个非常薄的层。

在设备上运行时,它将使用 正常返回数据CoreLocation,但在模拟器中,它将从文本文件(在 DLocationManager.h 文件中定义)读取纬度和经度。

我希望这会有所帮助,实现很简单,您必须startUpdatingLocation更新stopUpdatingLocation显示。

评论和反馈将不胜感激。

于 2009-05-01T17:39:22.017 回答
13

在模拟器上运行时,使用过滤功能交换测试实例。无论您以前在哪里收到位置(代表呼叫等),都可以通过以下方式传递:

+ (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
}

除了内存管理问题...

于 2009-04-29T17:41:22.263 回答
9

我认为这里有另一种(更好的恕我直言)方法,而不是像在

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协议“推送”到委托。

于 2010-01-13T21:12:50.847 回答
2

如果您需要设置模拟器提供给您的位置以外的位置,请从测试类触发核心位置回调。

于 2009-04-29T15:04:08.397 回答
2

在iphone 模拟器中永远不会调用locationManager:didUpdateToLocationlocationManager:didFailedWithError重载回调,这有点奇怪,我得到的只是 0.0000 经纬度和 0.0000 经度。作为位置。在您开发某些东西的情况下,仅使用模拟器环境很难实现位置处理期间可能发生的所有可能情况。

于 2010-10-11T18:43:07.730 回答
2

如果您有兴趣使用模拟位置更新更新 MKMapView 中的蓝色 userLocation 点,请查看我的 FTLocationSimulator,网址为http://github.com/futuretap/FTLocationSimulator

它读取由 Google Earth 生成的 KML 文件以提供持续的位置更新。

于 2010-10-27T08:36:45.080 回答
1

在 iPhone 模拟器上测试 CoreLocation

1) 要在模拟器中测试位置,最好的方法是使用 GPX 文件,只需转到文件 -> 新建 -> 资源 -> GPX 文件。

2) 添加 GPX 文件后,根据需要更新位置坐标。

3)将GPX文件添加到项目后,选择Scheme -> Edit Scheme -> Run -> Allow Location Simulation。勾选位置模拟并选择您刚刚创建的GPX文件的名称。

这种方式模拟器将始终选择您想要的坐标,我们已添加到我们的 GPX 文件中。

创建 GPX 文件

于 2017-06-30T10:36:48.770 回答