ios 在 iPhone 模拟器上测试 CoreLocation
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/802156/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Testing CoreLocation on iPhone Simulator
提问by Richard Stelling
UPDATE:As of iOS 5 and Xcode 4.1 is is now possible to test location in the simulator and even define routes. See http://developer.apple.comfor more details.
更新:从 iOS 5 和 Xcode 4.1 开始,现在可以在模拟器中测试位置,甚至定义路线。有关更多详细信息,请参阅http://developer.apple.com。
Legacy Question
遗留问题
Is there anyway to test CoreLocation on the iPhone Simulator?
有没有办法在 iPhone 模拟器上测试 CoreLocation?
All I require is to be able to set the location myself and have CoreLocation return it.
我所需要的只是能够自己设置位置并让 CoreLocation 返回它。
采纳答案by Richard Stelling
Thanks for the great feedback, it has prompted me to find a robust solution.
感谢伟大的反馈,它促使我找到一个强大的解决方案。
All the code can be found here:
所有代码都可以在这里找到:
http://code.google.com/p/dlocation/
http://code.google.com/p/dlocation/
It is verymessy but as I use it it will be become much better.
它非常凌乱,但当我使用它时,它会变得更好。
The solution was to subclass CLLocationManager
and define a new delegate @protocol
, called DLocationManagerDelegate
.
解决方案是子类化CLLocationManager
并定义一个新的委托@protocol
,称为DLocationManagerDelegate
.
It is designed to be a simple drop-in replacement for CLLocationManagerDelegate
that compiles down to a verythin layer when deployed on an actual device.
它被设计为一个简单的替代品,当部署在实际设备上时,CLLocationManagerDelegate
它会编译成一个非常薄的层。
When running on the device it will return data as normal using CoreLocation
, but in the simulator it will read latitude and longitude from a text file (defined in the DLocationManager.h file).
在设备上运行时,它将使用 正常返回数据CoreLocation
,但在模拟器中,它将从文本文件(在 DLocationManager.h 文件中定义)读取纬度和经度。
I hope this helps, the implementation is on the simple side and you have to startUpdatingLocation
and stopUpdatingLocation
to update the display.
我希望这有助于实现是在简单的方,你必须startUpdatingLocation
和stopUpdatingLocation
更新显示。
Comments and feedback will be gratefully received.
将不胜感激地收到评论和反馈。
回答by wcochran
Here is my simple hack that forces the CLLocationMager to return the geocoords of Powell's Tech Bookstore only on the simulator:
这是我的简单 hack,它强制 CLLocationMager 仅在模拟器上返回 Powell's 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
回答by Nick Veys
Use a filtering function to swap in a test instance when running on the simulator. Wherever you previously received the location (delegate call, etc), pass it through this:
在模拟器上运行时,使用过滤功能交换测试实例。无论您之前在哪里收到位置(委托电话等),请通过以下方式传递它:
+ (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
}
Memory management issues aside...
除了内存管理问题...
回答by yonel
I think there's another (better IMHO) approach here than subclassing CLLocationManager like in
我认为这里有另一种(更好的恕我直言)方法,而不是像这样子类化 CLLocationManager
http://code.google.com/p/dlocation/
http://code.google.com/p/dlocation/
In ObjectiveC it seems to be possible to replacean existing method from a class without overriding it. This is often called "method swizzling" : you define your own category for an existing class an implement an existing method in it.
在 ObjectiveC 中,似乎可以从类中替换现有方法而不覆盖它。这通常称为“方法调配”:您为现有类定义自己的类别并在其中实现现有方法。
From the client perspective, everything is transparent : he has the feeling he's dealing with the real CLLocationManager
but actually, you "took the control from it". So that he doesn't need to deal with any special subclass or any special delegate protocol : he keeps on using the same class / protocol as the one from CoreLocation.
从客户的角度来看,一切都是透明的:他有一种他在处理真实CLLocationManager
事物的感觉,但实际上,您“从中获得了控制权”。这样他就不需要处理任何特殊的子类或任何特殊的委托协议:他继续使用与 CoreLocation 相同的类/协议。
Here's an example to took the control over the delegate a client would inject :
这是一个控制客户端将注入的委托的示例:
@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
Then in this implementation, you're free to deal with a pre defined set of coordinates (coming from a file of whatever) that will be "pushed" to the delegate using the standard CLLocationManagerDelegate
protocol.
然后在这个实现中,你可以自由地处理一组预定义的坐标(来自任何文件),这些坐标将使用标准CLLocationManagerDelegate
协议“推送”到委托。
回答by Kendall Helmstetter Gelner
Trigger the Core Location callbacks from a test class, if you need to set a location other than the one the simulator gives you.
如果您需要设置模拟器提供的位置以外的位置,则从测试类触发核心位置回调。
回答by cybercow
the locationManager:didUpdateToLocationand locationManager:didFailedWithErroroverloaded callbacks are never called in the iphone simulator, that's kinda strange, all i get is 0.0000 for lat., and 0.0000 for lon. as the position. In the situation you develop something, that's kinda hard to implement all the possible situations that can occur during the location handling, using only simulator environment.
在的LocationManager:didUpdateToLocation和的LocationManager:didFailedWithError重载回调将不会在iPhone模拟器调用,还有点儿奇怪,我得到的是0.0000纬度,和0.0000 LON。作为职位。在您开发某些东西的情况下,仅使用模拟器环境很难实现位置处理期间可能发生的所有可能情况。
回答by Ortwin Gentz
If you're interested in updating the blue userLocation dot in a MKMapView with the simulated location updates, check out my FTLocationSimulator at http://github.com/futuretap/FTLocationSimulator
如果您有兴趣使用模拟位置更新更新 MKMapView 中的蓝色 userLocation 点,请查看我的 FTLocationSimulator,网址为http://github.com/futuretap/FTLocationSimulator
It reads a KML file generated by Google Earth to provide continuous location updates.
它读取由 Google 地球生成的 KML 文件以提供持续的位置更新。
回答by Dhaval Mistry
Testing CoreLocation on iPhone Simulator
在 iPhone 模拟器上测试 CoreLocation
1) To test the location in simulator,best way is to use GPX files,just go to Files -> New -> Resource -> GPX File.
1) 要在模拟器中测试位置,最好的方法是使用 GPX 文件,只需转到Files -> New -> Resource -> GPX File。
2) After Adding the GPX file update the location coordinates as desired.
2) 添加 GPX 文件后,根据需要更新位置坐标。
3) once the GPX file is added to the project,Select the Scheme -> Edit Scheme -> Run -> Allow Location Simulation.tick the location simulation and select the name of the GPX file you just created.
3)一旦GPX文件被添加到项目中,选择方案->编辑方案->运行->允许位置模拟。勾选位置模拟并选择您刚刚创建的GPX文件的名称。
this way simulator will always pick your desired coordinates,that we have added in our GPX File.
这样模拟器将始终选择您想要的坐标,我们已添加到我们的 GPX 文件中。