简体   繁体   English

"在 iPhone 模拟器中设置位置"

[英]Set the location in iPhone Simulator

如何在 iPhone 模拟器中设置位置(在 CoreLocation 服务中获取)?

As of iOS 5, the simulator has a configurable location.从 iOS 5 开始,模拟器有一个可配置的位置。

Under the Debug menu, the last entry is "Location";在Debug菜单下,最后一个条目是“Location”; this gives you a sub menu with:这为您提供了一个子菜单:

  • None没有任何
  • Custom Location自定义位置
  • Apple Stores苹果专卖店
  • Apple苹果
  • City Bicycle Ride城市自行车骑行
  • City Run城市跑
  • Freeway Drive高速公路驱动器

Custom Location lets you enter a Lat/Long value.自定义位置允许您输入纬度/经度值。 Bicycle ride, City Run, and Freeway Drive are simulation of a moving location (in Cupertino, of course).骑自行车、City Run 和 Freeway Drive 是对移动位置的模拟(当然是在库比蒂诺)。

Of course, this does nothing to help with debugging for iOS 4 (or earlier);当然,这对 iOS 4(或更早版本)的调试没有帮助; but it's a definite improvement!但这是一个明确的改进!

  1. Run project in iPhone Simulator在 iPhone 模拟器中运行项目
  2. Create in TextEdit file following file, call it MyOffice for example.在 TextEdit 文件中创建以下文件,例如将其命名为 MyOffice。 Make extension as .gpx将扩展名设为 .gpx在此处输入图片说明

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

  3. Select in Xcode at the Simulate area Add GPX File to Project...在 Xcode 中的 Simulate 区域中选择Add GPX File to Project...在此处输入图片说明

  4. Add created file from menu to project.将创建的文件从菜单添加到项目。
  5. Now you can see your location in Simulate area:现在您可以在模拟区域看到您的位置:在此处输入图片说明

XCode 11.3 and prior: XCode 11.3 及更早版本:

Debug -> Location -> Custom Location

在此处输入图片说明

XCode 11.4+: XCode 11.4+:

Features -> Location -> Custom Location

在此处输入图片说明

To find out which XCode version you have找出您拥有的 XCode 版本

$ /usr/bin/xcodebuild -version

in iOS Simulator menu, go to Debug -> Location -> Custom Location.在 iOS 模拟器菜单中,转到调试 -> 位置 -> 自定义位置。 There you can set the latitude and longitude and test the app accordingly.您可以在那里设置纬度和经度并相应地测试应用程序。 This works with mapkit and also with CLLocationManager.这适用于 mapkit 和 CLLocationManager。

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.在我的委托回调中,我检查我是否在模拟器中运行( #if TARGET_ IPHONE_SIMULATOR ),如果是,我提供我自己的、预先查找的纬度/经度。 To my knowledge, there's no other way.据我所知,没有其他办法。

Pre iOS 5 you could do it in code:在 iOS 5 之前,您可以在代码中完成:

I use this snippet just before the @implementation of the class where I need my fake heading and location data.我在需要我的假标题和位置数据的类的@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

After iOS 5 simply include a GPX file in your project like this to have the location updated continuously Hillerød.gpx:在 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>

I use GPSies.com to create the base file for the gpx data.我使用GPSies.com为 gpx 数据创建基本文件。 A bit of cleanup is required though.虽然需要一些清理。

Activate by running the simulator and choosing your file通过运行模拟器并选择您的文件来激活


(source: castleandersen.dk ) (来源: Castleandersen.dk

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

Open iOS Simulator application from Debug Menu -> Location ->从调试菜单打开 iOS 模拟器应用程序 -> 位置 ->

  1. None没有任何
  2. Custom Location自定义位置
  3. Apple Stores ...苹果专卖店...

You can easily share any desired location from the macOS Maps application to the Xcode simulator .您可以轻松地将macOS Maps 应用程序中的任何所需位置共享Xcode 模拟器

  1. From Xcode run the application in the simulator as you usually do.像往常一样,从 Xcode 在模拟器中运行应用程序。
  2. Open the maps application on macOS (for convenience I usually make a new Desktop where I have both the simulator and the map application).在 macOS 上打开地图应用程序(为方便起见,我通常会创建一个新的桌面,其中既有模拟器也有地图应用程序)。
  3. Inside the maps (macOS), long click on any point on the map, or search for a place to let the map draw a pin where you need it.在地图 (macOS) 内,长按地图上的任意点,或搜索某个位置,让地图在您需要的位置绘制图钉。
  4. Click on the pin, then on the information (i) icon ( see image ).单击图钉,然后单击信息 (i) 图标(见图)。
  5. In the up right of the information view click on the share icon.在信息视图的右上角,单击共享图标。
  6. Share the location with the simulator ( see image ).与模拟器共享位置(见图)。
  7. Confirm and enjoy :)确认并享受:)

This let you test some locations quickly, having an understanding on where geographically the location is (eg for testing geofencing), and deciding at runtime where to go next (eg for debugging and fine-tuning stuff).这让您可以快速测试某些位置,了解该位置的地理位置(例如用于测试地理围栏),并在运行时决定下一步去哪里(例如用于调试和微调)。

Tested this on MacOS Catalina 10.15.4 and Xcode 11.4.在 MacOS Catalina 10.15.4 和 Xcode 11.4 上对此进行了测试。

The kind folks at FutureTap have made the FTLocationSimulator available for free at GitHub . FutureTap的好心人已经在GitHub 上免费提供了 FTLocationSimulator。 It allows you to prepare a test route using for instance Google Earth, and then have the simulator feed these coordinates to your app.它允许您使用例如 Google Earth 准备测试路线,然后让模拟器将这些坐标提供给您的应用程序。

I've written a blog post about how to use FTLocationSimulator to easily switch between multiple routes during testing of the location features.我写了一篇关于如何在测试位置功能期间使用 FTLocationSimulator 在多条路线之间轻松切换的博客文章

you can add gpx files to your project and use it:您可以将 gpx 文件添加到您的项目中并使用它:
edit scheme > options > allow location simulation > pick the file name that contains for example:编辑方案> 选项> 允许位置模拟> 选择包含例如的文件名:

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

optionally just hardcode the lat/lon values that are returned by the location manager.可选地只是硬编码位置管理器返回的纬度/经度值。 This is old style though.虽然这是旧风格。

so you won't add it to the simulator, but to your Xcode project.所以你不会把它添加到模拟器中,而是添加到你的 Xcode 项目中。

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

Better late than never :)迟到总比不到好 :)

I just came across this iSimulate which allows you to send Fake location to the app.我刚刚遇到了这个iSimulate ,它允许您将假位置发送到应用程序。 The solution is NOT free.解决方案不是免费的。

> 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.

Anyway, i am planning to get this.无论如何,我打算得到这个。 will update more soon!很快就会更新更多!

Where you want to set your location?您想在哪里设置您的位置? you can use mapkit api to show u location's.您可以使用 mapkit api 来显示您的位置。 see icodeblog.com for more detail on how to use mapkit.有关如何使用 mapkit 的更多详细信息,请参阅 icodeblog.com。 Also you can store your desired cordinates just create an object CLLocation2D *location;您也可以存储所需的坐标,只需创建一个对象 CLLocation2D *location; location.longitude=your desired longitude value; location.longitude=你想要的经度值; location.latitude=your desired latitude value; location.latitude=你想要的纬度值;

Changing Location of simulator does not affect already built, loaded apps.更改模拟器的位置不会影响已构建、已加载的应用程序。 For example, if you have an app with Google map view running on simulator the map won't be updated till you rebuild the app.例如,如果您有一个在模拟器上运行 Google 地图视图的应用程序,则在您重新构建应用程序之前,地图不会更新。 Also, keep in mind that when entering custom lat and long you should be careful with longitudes with (-) sign and without.另外,请记住,在输入自定义纬度和经度时,您应该注意带 (-) 号和不带 (-) 号的经度。 Replacing existing custom longitude of the simulator with new one but keeping (-) sign mistakenly or entering incorrect lat/long will result empty map screen with blue background - probably indicates oceans or poles.用新的模拟器替换现有的自​​定义经度但错误地保留 (-) 符号或输入不正确的纬度/经度将导致带有蓝色背景的空白地图屏幕 - 可能表示海洋或极地。

In Xcode itself (not the Simulator app), a simulated location can be set via Debug menu > Simulate Location.在 Xcode 本身(不是模拟器应用程序)中,可以通过 Debug 菜单 > Simulate Location 设置模拟位置。

That menu option becomes enabled when the app is actively running (either on the Simulator, or on a real device).当应用程序正在运行(在模拟器上或在真实设备上)时,该菜单选项将启用。

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

(As of XCode 13.0) (截至 XCode 13.0)

As of iOS 15.2 and Xcode 13.2.2, the simulator has a configurable location.从 iOS 15.2 和 Xcode 13.2.2 开始,模拟器具有可配置的位置。

Under the Features menu, the last entry is "Location";在功能菜单下,最后一个条目是“位置”; this gives you a sub menu with:这为您提供了一个子菜单:

None没有任何
Custom Location自定义位置
City Run城市跑
City Bicycle Ride城市自行车骑行
Freeway Drive高速公路驱动器
Apple苹果

Custom Location lets you enter a Lat\/Long value.自定义位置允许您输入纬度\/经度值。 Bicycle ride, City Run, and Freeway Drive are simulation of a moving location (in Cupertino, of course).骑自行车、City Run 和 Freeway Drive 是对移动位置的模拟(当然是在库比蒂诺)。

"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM