在运行期间设置位置模拟器City / GPX [英] Set location simulator City/GPX during Runtime

查看:179
本文介绍了在运行期间设置位置模拟器City / GPX的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一种方法可以在运行时以编程方式更改位置模拟器城市?例如,这将允许测试模拟伦敦或东京。

Is there a method to programmatically change the location simulator city during runtime? For example this would allow tests to simulate London or Tokyo.

下图显示了如何手动切换位置(GPX文件)。如何在应用程序运行时以编程方式实现此结果?

The image below shows how to switch between locations (GPX files) manually. How can I achieve this result programmatically while the app is running?

推荐答案

设置位置的替代方法是通过调用CLLocationManager类的位置。在obj-c中,

Alternate way to set location is by swizzling 'location' of 'CLLocationManager' class. In obj-c,

+(void) load {
   // replace 'location' with 'custom_location' method
 }

然后通过简单地改变'来实现custom_location方法,无论你想要设置什么位置kMockedLatitude'和'kMockedLongitude'变量。

Then implement custom_location method with whatever the location you want to set by simply changing 'kMockedLatitude' and 'kMockedLongitude' variables.

//Portland, USA
CLLocationDegrees kMockedLatitude = 45.52306;
CLLocationDegrees kMockedLongitude = -122.67648;

-(CLLocation *)custom_location  
{  
   return [[CLLocation alloc] initWithLatitude:kMockedLatitude longitude:kMockedLongitude];
}

这甚至可以在iOS设备上使用。

This will work even in iOS device.

这篇关于在运行期间设置位置模拟器City / GPX的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆