-->

Simulating location changes for `startMonitoringSi

2019-01-16 16:26发布

问题:

I am writing an iPhone app and using Location Services. I am using the startMonitoringSignificantLocationChanges method on CLLocationManager. (As I understand, this is for low power, low accuracy location services.) When I build and launch my app in iOS Simulator, it gets my location fine. However, I want to simulate updates to the location beyond the initial fix.

I did notice these two options:

  • iOS Simulator : Debug > Location menu
  • Xcode 4: Product > Debug > Simulate Location menu

Both of these work acceptably for simulating an initial fix. (For example, choose 'Apple' from the former, or 'Moscow' from the latter.) However, subsequent changes to either of these will NOT trigger another location update in the simulated app.

What I desire is to be notified when the simulated location changes when using startMonitoringSignificantLocationChanges. (iOS Simulator does generate a series of updates when I use startUpdatingLocation instead.) This makes it way easier to test location-enabled applications without taking the device out into the field.

Two questions:

  • What is the difference between these two?
  • How do I use iOS Simulator and/or Xcode 4 to simulate a series of location changes?

Cheers.

回答1:

If you want to make a track, you can create a GPX file with waypoints like this:

<?xml version="1.0" encoding="UTF-8"?>
<gpx>
    <wpt lat="52.373" lon="4.871"></wpt>
    <wpt lat="52.374" lon="4.872"></wpt>
    <wpt lat="52.375" lon="4.871"></wpt>
</gpx>

Save it as track.gpx. Be careful of having no whitespace at the start.

When you Build and Run, there's a toolbar at the bottom of Xcode with a location arrow (similar to the one on iPhones). It's for "Simulate Location". Click there and load in your GPX file. When your app is running in the Simulator, the blue dot should move around. I don't think there's a way to control the timing, so it updates its location every half second or so.

The difference I can see in the Simulator menu is that you can't have a custom track, only a custom point. I don't know why the menus options are different, they'll probably add an option for a custom track in the Simulator at some point.



回答2:

FWIW, the monitoring of significant location changes is tied almost exclusively to cell tower triangulation and handoffs. The way this is handled in the simulator is probably much different than it would be on-device. Even region monitoring get the added bonus of Wifi and other apps using location to get more frequent updates.

I'm guessing this is just how the simulator works, even by passing in locations that are wildly far apart and should logically trigger, that just isn't how the updates are handled. There are no cell tower handoffs in the simulator, therefore no way to really test out the significant changes.



回答3:

To follow up on @Nevan-king answer as for why there are no updates when using startMonitoringSignificantLocationChanges

See Apple Note for func startMonitoringSignificantLocationChanges()

Apps can expect a notification as soon as the device moves 500 meters or more from its previous notification. It should not expect notifications more frequently than once every five minutes. If the device is able to retrieve data from the network, the location manager is much more likely to deliver notifications in a timely manner.