161

How can I set the location (as it's picked up in CoreLocation services) in the iPhone Simulator?

4

17 回答 17

134

从 iOS 5 开始,模拟器有一个可配置的位置。

在Debug菜单下,最后一项是“Location”;这为您提供了一个子菜单:

  • 没有任何
  • 自定义位置
  • 苹果专卖店
  • 苹果
  • 城市自行车骑行
  • 城市跑
  • 高速公路驱动器

自定义位置允许您输入纬度/经度值。骑自行车、City Run 和 Freeway Drive 是对移动位置的模拟(当然是在库比蒂诺)。

当然,这对调试 iOS 4(或更早版本)没有任何帮助;但这是一个明确的改进!

于 2011-11-17T22:46:32.727 回答
89
  1. 在 iPhone 模拟器中运行项目
  2. 在 TextEdit 文件中创建以下文件,例如将其称为 MyOffice。将扩展名设为 .gpx 在此处输入图像描述

    <?xml version="1.0"?> <gpx version="1.0" creator="MyName"> <wpt lat="53.936166" lon="27.565370"> <name>MyOffice</name> </wpt> </gpx>

  3. 在 Xcode 中的 Simulate 区域中选择Add GPX File to Project...在此处输入图像描述

  4. 将创建的文件从菜单添加到项目。
  5. 现在您可以在模拟区域中看到您的位置:在此处输入图像描述
于 2012-09-28T09:44:51.693 回答
48

XCode 11.3 及更早版本:

Debug -> Location -> Custom Location

在此处输入图像描述

XCode 11.4+:

Features -> Location -> Custom Location

在此处输入图像描述

找出您拥有的 XCode 版本

$ /usr/bin/xcodebuild -version
于 2020-04-14T12:13:14.397 回答
23

在 iOS 模拟器菜单中,转到调试 -> 位置 -> 自定义位置。您可以在那里设置纬度和经度并相应地测试应用程序。这适用于 mapkit 和 CLLocationManager。

于 2015-07-06T05:07:46.410 回答
17

In my delegate callback, I check to see if I'm running in a simulator (#if TARGET_ IPHONE_SIMULATOR) and if so, I supply my own, pre-looked-up, Lat/Long. To my knowledge, there's no other way.

于 2008-10-18T02:51:25.870 回答
7

您可以轻松地任何所需位置macOS 地图应用程序共享到Xcode 模拟器

  1. 像往常一样从 Xcode 在模拟器中运行应用程序。
  2. 在 macOS 上打开地图应用程序(为方便起见,我通常会创建一个新的桌面,其中有模拟器和地图应用程序)。
  3. 在地图 (macOS) 中,长按地图上的任意点,或搜索一个位置,让地图在您需要的地方画一个大头针。
  4. 单击图钉,然后单击信息 (i) 图标(见图)。
  5. 在信息视图的右上角,单击共享图标。
  6. 与模拟器共享位置(见图)。
  7. 确认并享受:)

这使您可以快速测试某些位置,了解该位置的地理位置(例如,用于测试地理围栏),并在运行时决定下一步要去哪里(例如,用于调试和微调)。

在 MacOS Catalina 10.15.4 和 Xcode 11.4 上对此进行了测试。

于 2020-03-30T17:28:59.723 回答
6

在 iOS 5 之前,你可以在代码中做到这一点:

@implementation我在需要我的假标题和位置数据的类之前使用这个片段。

#if (TARGET_IPHONE_SIMULATOR)
@interface MyHeading : CLHeading
    -(CLLocationDirection) magneticHeading;
    -(CLLocationDirection) trueHeading;
@end

@implementation MyHeading
    -(CLLocationDirection) magneticHeading { return 90; }
    -(CLLocationDirection) trueHeading { return 91; }
@end

@implementation CLLocationManager (TemporaryLocationFix)
- (void)locationFix {
    CLLocation *location = [[CLLocation alloc] initWithLatitude:55.932 longitude:12.321];
    [[self delegate] locationManager:self didUpdateToLocation:location fromLocation:nil];

    id heading  = [[MyHeading alloc] init];
    [[self delegate] locationManager:self didUpdateHeading: heading];
}

-(void)startUpdatingHeading {
    [self performSelector:@selector(locationFix) withObject:nil afterDelay:0.1];
}

- (void)startUpdatingLocation {
    [self performSelector:@selector(locationFix) withObject:nil afterDelay:0.1];
}
@end
#endif

在 iOS 5 之后,只需在您的项目中包含一个 GPX 文件,这样就可以持续更新 Hillerød.gpx 的位置:

<?xml version="1.0"?>
<gpx version="1.1" creator="Xcode"> 
    <wpt lat="55.93619760" lon="12.29131930"></wpt>
    <wpt lat="55.93625770" lon="12.29108330"></wpt>
    <wpt lat="55.93631780" lon="12.29078290"></wpt>
    <wpt lat="55.93642600" lon="12.29041810"></wpt>
    <wpt lat="55.93653420" lon="12.28998890"></wpt>
    <wpt lat="55.93660630" lon="12.28966710"></wpt>
    <wpt lat="55.93670240" lon="12.28936670"></wpt>
    <wpt lat="55.93677450" lon="12.28921650"></wpt>
    <wpt lat="55.93709900" lon="12.28945250"></wpt>
    <wpt lat="55.93747160" lon="12.28949540"></wpt>
    <wpt lat="55.93770000" lon="12.28966710"></wpt>
    <wpt lat="55.93785620" lon="12.28977440"></wpt>
    <wpt lat="55.93809660" lon="12.28988170"></wpt>
    <wpt lat="55.93832490" lon="12.28994600"></wpt>
    <wpt lat="55.93845710" lon="12.28996750"></wpt>
    <wpt lat="55.93856530" lon="12.29007480"></wpt>
    <wpt lat="55.93872150" lon="12.29013910"></wpt>
    <wpt lat="55.93886570" lon="12.28975290"></wpt>
    <wpt lat="55.93898590" lon="12.28955980"></wpt>
    <wpt lat="55.93910610" lon="12.28919500"></wpt>
    <wpt lat="55.93861330" lon="12.28883020"></wpt>
    <wpt lat="55.93845710" lon="12.28868000"></wpt>
    <wpt lat="55.93827680" lon="12.28850840"></wpt>
    <wpt lat="55.93809660" lon="12.28842250"></wpt>
    <wpt lat="55.93796440" lon="12.28831520"></wpt>
    <wpt lat="55.93780810" lon="12.28810070"></wpt>
    <wpt lat="55.93755570" lon="12.28790760"></wpt>
    <wpt lat="55.93739950" lon="12.28775730"></wpt>
    <wpt lat="55.93726730" lon="12.28767150"></wpt>
    <wpt lat="55.93707500" lon="12.28760710"></wpt>
    <wpt lat="55.93690670" lon="12.28734970"></wpt>
    <wpt lat="55.93675050" lon="12.28726380"></wpt>
    <wpt lat="55.93649810" lon="12.28713510"></wpt>
    <wpt lat="55.93625770" lon="12.28687760"></wpt>
    <wpt lat="55.93596930" lon="12.28679180"></wpt>
    <wpt lat="55.93587310" lon="12.28719940"></wpt>
    <wpt lat="55.93575290" lon="12.28752130"></wpt>
    <wpt lat="55.93564480" lon="12.28797190"></wpt>
    <wpt lat="55.93554860" lon="12.28833670"></wpt>
    <wpt lat="55.93550050" lon="12.28868000"></wpt>
    <wpt lat="55.93535630" lon="12.28900190"></wpt>
    <wpt lat="55.93515200" lon="12.28936670"></wpt>
    <wpt lat="55.93505580" lon="12.28958120"></wpt>
    <wpt lat="55.93481550" lon="12.29001040"></wpt>
    <wpt lat="55.93468320" lon="12.29033230"></wpt>
    <wpt lat="55.93452700" lon="12.29063270"></wpt>
    <wpt lat="55.93438280" lon="12.29095450"></wpt>
    <wpt lat="55.93425050" lon="12.29121200"></wpt>
    <wpt lat="55.93413040" lon="12.29140520"></wpt>
    <wpt lat="55.93401020" lon="12.29168410"></wpt>
    <wpt lat="55.93389000" lon="12.29189870"></wpt>
    <wpt lat="55.93372170" lon="12.29239220"></wpt>
    <wpt lat="55.93385390" lon="12.29258530"></wpt>
    <wpt lat="55.93409430" lon="12.29295010"></wpt>
    <wpt lat="55.93421450" lon="12.29320760"></wpt>
    <wpt lat="55.93433470" lon="12.29333630"></wpt>
    <wpt lat="55.93445490" lon="12.29350800"></wpt>
    <wpt lat="55.93463520" lon="12.29374400"></wpt>
    <wpt lat="55.93479140" lon="12.29410880"></wpt>
    <wpt lat="55.93491160" lon="12.29419460"></wpt>
    <wpt lat="55.93515200" lon="12.29458090"></wpt>
    <wpt lat="55.93545250" lon="12.29494570"></wpt>
    <wpt lat="55.93571690" lon="12.29505300"></wpt>
    <wpt lat="55.93593320" lon="12.29513880"></wpt>
    <wpt lat="55.93617360" lon="12.29522460"></wpt>
    <wpt lat="55.93622170" lon="12.29537480"></wpt>
    <wpt lat="55.93713510" lon="12.29505300"></wpt>
    <wpt lat="55.93776000" lon="12.29378700"></wpt>
    <wpt lat="55.93904600" lon="12.29531040"></wpt>
    <wpt lat="55.94004350" lon="12.29552500"></wpt>
    <wpt lat="55.94023570" lon="12.29561090"></wpt>
    <wpt lat="55.94019970" lon="12.29591130"></wpt>
    <wpt lat="55.94017560" lon="12.29629750"></wpt>
    <wpt lat="55.94017560" lon="12.29670520"></wpt>
    <wpt lat="55.94017560" lon="12.29713430"></wpt>
    <wpt lat="55.94019970" lon="12.29754200"></wpt>
    <wpt lat="55.94024780" lon="12.29816430"></wpt>
    <wpt lat="55.94051210" lon="12.29842180"></wpt>
    <wpt lat="55.94084860" lon="12.29820720"></wpt>
    <wpt lat="55.94105290" lon="12.29799270"></wpt>
    <wpt lat="55.94123320" lon="12.29777810"></wpt>
    <wpt lat="55.94140140" lon="12.29749910"></wpt>
    <wpt lat="55.94142550" lon="12.29726310"></wpt>
    <wpt lat="55.94147350" lon="12.29687690"></wpt>
    <wpt lat="55.94155760" lon="12.29619020"></wpt>
    <wpt lat="55.94161770" lon="12.29576110"></wpt>
    <wpt lat="55.94148550" lon="12.29531040"></wpt>
    <wpt lat="55.94093270" lon="12.29522460"></wpt>
    <wpt lat="55.94041600" lon="12.29518170"></wpt>
    <wpt lat="55.94056020" lon="12.29398010"></wpt>
    <wpt lat="55.94024780" lon="12.29352950"></wpt>
    <wpt lat="55.94001940" lon="12.29335780"></wpt>
    <wpt lat="55.93992330" lon="12.29325050"></wpt>
    <wpt lat="55.93969490" lon="12.29299300"></wpt>
    <wpt lat="55.93952670" lon="12.29277840"></wpt>
    <wpt lat="55.93928630" lon="12.29260680"></wpt>
    <wpt lat="55.93915410" lon="12.29232780"></wpt>
    <wpt lat="55.93928630" lon="12.29202740"></wpt>
    <wpt lat="55.93933440" lon="12.29174850"></wpt>
    <wpt lat="55.93947860" lon="12.29116910"></wpt>
    <wpt lat="55.93965890" lon="12.29095450"></wpt>
    <wpt lat="55.94001940" lon="12.29061120"></wpt>
    <wpt lat="55.94041600" lon="12.29084730"></wpt>
    <wpt lat="55.94076450" lon="12.29101890"></wpt>
    <wpt lat="55.94080060" lon="12.29065410"></wpt>
    <wpt lat="55.94086060" lon="12.29031080"></wpt>
    <wpt lat="55.94092070" lon="12.28990310"></wpt>
    <wpt lat="55.94099280" lon="12.28975290"></wpt>
    <wpt lat="55.94119710" lon="12.28986020"></wpt>
    <wpt lat="55.94134130" lon="12.28998890"></wpt>
    <wpt lat="55.94147350" lon="12.29007480"></wpt>
    <wpt lat="55.94166580" lon="12.29003190"></wpt>
    <wpt lat="55.94176190" lon="12.28938810"></wpt>
    <wpt lat="55.94183400" lon="12.28893750"></wpt>
    <wpt lat="55.94194220" lon="12.28850840"></wpt>
    <wpt lat="55.94199030" lon="12.28835820"></wpt>
    <wpt lat="55.94215850" lon="12.28859420"></wpt>
    <wpt lat="55.94250700" lon="12.28883020"></wpt>
    <wpt lat="55.94267520" lon="12.28893750"></wpt>
    <wpt lat="55.94284350" lon="12.28902330"></wpt>
    <wpt lat="55.94304770" lon="12.28915210"></wpt>
    <wpt lat="55.94325200" lon="12.28925940"></wpt>
    <wpt lat="55.94348030" lon="12.28953830"></wpt>
    <wpt lat="55.94366060" lon="12.28966710"></wpt>
    <wpt lat="55.94388890" lon="12.28975290"></wpt>
    <wpt lat="55.94399700" lon="12.28994600"></wpt>
    <wpt lat="55.94379280" lon="12.29065410"></wpt>
    <wpt lat="55.94364860" lon="12.29095450"></wpt>
    <wpt lat="55.94350440" lon="12.29127640"></wpt>
    <wpt lat="55.94340820" lon="12.29155540"></wpt>
    <wpt lat="55.94331210" lon="12.29198450"></wpt>
    <wpt lat="55.94315590" lon="12.29269260"></wpt>
    <wpt lat="55.94310780" lon="12.29318610"></wpt>
    <wpt lat="55.94301170" lon="12.29361530"></wpt>
    <wpt lat="55.94292760" lon="12.29408740"></wpt>
    <wpt lat="55.94290350" lon="12.29436630"></wpt>
    <wpt lat="55.94287950" lon="12.29453800"></wpt>
    <wpt lat="55.94283140" lon="12.29533190"></wpt>
    <wpt lat="55.94274730" lon="12.29606150"></wpt>
    <wpt lat="55.94278340" lon="12.29621170"></wpt>
    <wpt lat="55.94280740" lon="12.29649060"></wpt>
    <wpt lat="55.94284350" lon="12.29679100"></wpt>
    <wpt lat="55.94284350" lon="12.29734890"></wpt>
    <wpt lat="55.94308380" lon="12.29837890"></wpt>
    <wpt lat="55.94315590" lon="12.29852910"></wpt>
    <wpt lat="55.94263920" lon="12.29906550"></wpt>
    <wpt lat="55.94237480" lon="12.29910850"></wpt>
    <wpt lat="55.94220660" lon="12.29915140"></wpt>
    <wpt lat="55.94208640" lon="12.29902260"></wpt>
    <wpt lat="55.94196620" lon="12.29887240"></wpt>
    <wpt lat="55.94176190" lon="12.29794970"></wpt>
    <wpt lat="55.94156970" lon="12.29760640"></wpt>
</gpx>

我使用GPSies.com为 gpx 数据创建基础文件。不过需要进行一些清理。

通过运行模拟器并选择您的文件来激活


(来源:castalandersen.dk

于 2012-02-10T12:06:24.220 回答
6

从 Xcode 11.6 和 Swift 5.3 开始,模拟自定义位置的功能已从 iOS 模拟器菜单中的“调试”移至“功能”。

于 2020-08-17T07:20:03.787 回答
5

您可以将 gpx 文件添加到您的项目并使用它:
编辑方案 > 选项 > 允许位置模拟 > 选择包含例如的文件名:

<?xml version="1.0"?>
<gpx version="1.1" creator="Xcode"> 
    <wpt lat="41.92296" lon="-87.63892"></wpt>
</gpx>

可选地,只需硬编码位置管理器返回的纬度/经度值。虽然这是旧风格。

所以你不会将它添加到模拟器中,而是添加到你的 Xcode 项目中。

于 2011-08-19T04:38:16.387 回答
4

Open iOS Simulator application from Debug Menu -> Location ->

  1. None
  2. Custom Location
  3. Apple Stores ...
于 2014-10-16T10:17:53.100 回答
3

FutureTap的好心人在GitHub 上免费提供了FTLocationSimulator 。它允许您使用例如 Google Earth 准备测试路线,然后让模拟器将这些坐标提供给您的应用程序。

我写了一篇关于如何在测试位置功能期间使用 FTLocationSimulator 在多条路线之间轻松切换的博客文章。

于 2011-02-21T09:20:48.057 回答
2

在撰写本文时,IOS 模拟器的位置选项已移至功能 -> 位置 -> 自定义位置

于 2020-03-26T23:31:14.443 回答
0

迟到总比不到好 :)

我刚刚遇到了这个iSimulate,它允许您向应用发送假位置。该解决方案不是免费的。

> Q: How does iSimulate work? 

> A: When added to your project, the iSimulate
> SDK library creates a listening server
> on your iPhone Simulator that waits
> for a connection from an iPhone/iPod
> running the iSimulate client. When
> such connection is established, the
> iSimulate client running on your
> iPhone/iPod captures all data from the
> accelerometer sensor, the touch
> events, the location and device ID and
> streams them to the server. The
> iSimulate SDK library then recreates
> all input events synthetically. This
> is entirely transparent to your
> application and does not interfere
> with your application's functionality.

无论如何,我打算得到这个。将很快更新更多!

于 2009-08-29T14:31:40.440 回答
0

你想在哪里设置你的位置?你可以使用 mapkit api 来显示你的位置。有关如何使用 mapkit 的更多详细信息,请参阅 icodeblog.com。您也可以存储您想要的坐标,只需创建一个对象 CLLocation2D *location; location.longitude=您想要的经度值;location.latitude=您想要的纬度值;

于 2009-11-07T08:19:22.107 回答
0

在 Xcode 本身(不是 Simulator 应用程序)中,可以通过 Debug 菜单 > Simulate Location 设置模拟位置。

当应用程序正在运行时(在模拟器上或在真实设备上),该菜单选项将启用。

XCode > 调试菜单 > 模拟位置 > 带有位置列表的子菜单

(从 XCode 13.0 开始)

于 2021-10-15T14:23:09.430 回答
0

更改模拟器的位置不会影响已构建、加载的应用程序。例如,如果您有一个在模拟器上运行的带有 Google 地图视图的应用程序,那么在您重新构建应用程序之前,地图不会更新。另外,请记住,在输入自定义纬度和经度时,您应该注意带有 (-) 符号和不带 (-) 符号的经度。用新的模拟器替换现有的自​​定义经度,但错误地保留 (-) 符号或输入不正确的纬度/经度将导致带有蓝色背景的空白地图屏幕 - 可能表示海洋或两极。

于 2021-09-25T06:16:15.727 回答
0

从 iOS 15.2 和 Xcode 13.2.2 开始,模拟器具有可配置的位置。

在功能菜单下,最后一个条目是“位置”;这为您提供了一个子菜单:


自定义位置
City Run
City 自行车骑行
Freeway Drive
Apple

自定义位置允许您输入纬度/经度值。骑自行车、City Run 和 Freeway Drive 是对移动位置的模拟(当然是在库比蒂诺)。

于 2022-02-06T07:48:07.613 回答