How does MKMapView's showsUserLocation work? - ios

As the mac desktop's simulator don't have location service, I can't try by myself and I choose to ask here.
the google map app can do continuous locationing for your device, as you move you can see the blue spot moving on the map.
But how can we develop app that does it?
Is it just
(1)enable the device location service in setting.
(2)add the codes: mapView.showsUserLocation = YES;
sufficient to do this? If yes, can we know how frequent does it update the location?
the similar query also raised on CLLocationManager class and its delegate.
how startUpdatingLocation method updates the device's location? and how frequent is it?
And, does startUpdatingLocation call the locationManager: didUpdateToLocation?
how does the former call the latter and what to implement in the latter?

Both CLLocationManager and MKMapView will use the AGPS of the iPhone.
AGPS means assisted GPS, which works by first giving you app the last know coordinates then using triangulation the hext coordinates, then it will start getting some real GPS coordinates.
It will keep tracking until you call the stopUpdatingLocation on CLLocationManager. There is no interval you will just be notified when ever a new set of coordinates is received.
Be aware that continues track of GPS will drain you battery.
I suggest you read the CLLocationManager documentation.

Related

Region monitoring when device is off

I have a client requirement to monitor a region (say MyHome). To alert the user when he leaves MyHome and reaches back to the location. I can handle it pretty easily using the region crossing delegate methods:
locationManager:didEnterRegion:
locationManager:didExitRegion:
My question is, what will be the scenario if my device is off when I am at MyHome location. I leave the location MyHome, move to a different place and switch on my mobile. Will I get the locationManager:didExitRegion: delegate method fired when I launch the app back and thus will be able to notify that I am away from the region. OR will I have to do anything else to get this possible?
First of all monitoring region is not made for such small area to monitor. You may not get actual results. Now, the answer of your question, yes. It is possible. When you will start your device, you will have delegates method to get hit.

CoreMotion updates in background state

With the M7 chip in the latest iOS devices one can get programmatically notified as the user goes from stationary to running, walking, etc using CMMotionActivityManager. Stava and Runkeeper have both used this to auto-pause GPS polling (shut off the GPS antenna) when it detects the user isn't moving via the M7, and then re-enable GPS updates once they are moving again. It is able to do this while the app is in the background state, which is the key here.
The issue I run into while duplicating this functionality is that if I turn off GPS updates while my app is in the background I stop receiving activity updates, and can no longer detect when the user moves again via the M7 to turn the GPS back on.
If I leave the GPS running the whole time I'll continue to get movement updates from Core Motion the entire time the app is in the background.
I'm assuming they aren't playing white-noise or some other cheap trick to stay active. How did they go about this?
RunKeeper actually does use the audio trick to stay awake. If you open up the app package and check their Info.plist you will see that it registers for the background audio mode. This is how they pull off periodic audio notifications of your distance, speed, and pace. It is also how they stay awake during your run while minimizing battery drain.
If you noticed that the Location Services icon (the triangle in the status bar) disappears completely while using RunKeeper then they definitely are not using any type of location tracking to accomplish background execution. Even activating geo-fences and significant location change monitoring would cause the Location Services icon to appear.
They also aren't using the M7 to stay awake because it doesn't work that way. An update from the M7-related CoreMotion APIs will not wake up your app from sleep. When they app does wake up they would be able to query the Motion Activity and Step history and maybe try to compute something, but I doubt it would be all that accurate.
Finally you should note that the Auto-pause APIs were introduced in iOS 6 before the release of the iPhone 5s and M7 chip. They are orthogonal concepts.
have you considered experimenting with
application:performFetchWithCompletionHandler:
in the app delegate? You can't control how often it is called, but depending on the app, it can be every ~15 minutes. You can then launch the CMMotionActivityManager from there to query M7 results.
It's not entirely clear what functionality you are trying to replicate, but the M7 chip records all of the activity, regardless of whether your app is running. So you can simply query in the background and update step totals or activity type totals.
What I noticed when you turn off GPS, app will not execute any code in background for iOS 7, app looks like in inactive state. So better while moving to background use startMonitoringSignificantLocationChanges and also get updates from your location manager. Means simultenoulsy use both service startUpdatingLocation on user state change and startMonitoringSignificantLocationChanges in Background.
So when user Turn on GPS, while you used startMonitoringSignificantLocationChanges your app will receive
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
Better check here in background itself what wrong with CoreMotion Framework. And try to restart it.
Because wihtout M7 chip device I am able to read Accelerometer reading in such case.
If your location manager is working in Active mode, to enable background mode you need to do this three steps:
Check that [Target / Capabilities / Background Modes / Location updates] is enabled.
[locationManager requestAlwaysAuthorization];
locationManager.allowsBackgroundLocationUpdates = YES;
First, check if you have set up the background behave of your app.
Go to target - capabilities section and check Background mode for location updates.

ios background location services arrow disappears when not moving

I built this app which is tracking the users position even when running in background. I use the CLLocationManagers method
startUpdatingLocation
I set the CLLocationManager to
desiredAccuracy=kCLLocationAccuracyNearestTenMeters
distanceFilter=250
Everything works fine in foreground and in background. But when I don't move the device for a longer period (hours) I can see that the location arrow disappears. When I switch back to my app I can see that the arrow comes back immediately and that the app did NOT crash. Is this a "feature" of the LocationManager I don't know? Did the LocationManager went into some kind of "standby"? This should NOT be an issue with the app running not in foreground since I can travel with it for an hour and it gives me a perfect track.
Yes, this is a feature. As given in the documentation location manager object manages power aggressively by turning off hardware when it is not needed. Turning off GPS hardware can lead to significant power savings.

Region Monitoring and App Closed

I have a problem with my App based in Region Monitoring in iOS;
Suppose that I have a Monitored Region with 300 meters radius, and my location is 350 meters off the center of that region (but I am into a car moving get closer to my region).
If I close my App in that moment the GPS is turned Off instantly and the method "didEnterRegion" is never called. This problem doesn't happen if my location is farther to my region monitored (for example 500 meters away)
Is possible to fix this? I tried with "Background modes" setting the "Required location services" in background, but this make to use the GPS instead "Region Monitoring" and the GPS never stops.
The problem with Region Monitoring is that this function never works when the user is already "inside the region", then this causes a lot of problems for the in time notification.
First, you don't need background mode for region monitoring to work, region monitoring will continue to function even if you Sleep your device(pressing the top button). Plus Apple can reject your app if you don't use BG mode according to how they need it.
For your issue, if you are already inside and you are not getting a didEnterRegion for whatever reasons, you should use a backup plan. In your CLRegion class you can call containsCoordinate to see if you are inside the region.
You did not get a didEnterRegion call probably for some reason your add region gets reinitialized. I.e. you have re-added your region.

CLLocationManager default coordinate values

I'm learning to develop iOS apps and I try them on simulater only. Now, in my app I use CLLocationManager to get my current location coordinates. But when I run the app no matter where I am I get the same results for all the coordinate values(altitude,longitude,latitude). They do not change even if I move to another location. What I wonder is how does CLLocationManager get values? If it does not work on simulater then where do the values that never change come from? According to the coordinates I guess it's a default value that shows London if no coordinates are available.
Try Resetting the iOS Simulator, erasing all applications and settings.
And when you run CLLocationManager using application, it will probably ask for permission to use location, something like what you would see in you device.
I've been using iOS Simulator, and it gives location of my computer for the simulator all the time. I guess it's affected by your internet connection and IP address. Since many computers don't come with GPS, it's the only way to know the location of the computer.
The simulator always gives the location of Apple headquarters at Cupertino. It does not use computer's gps or location.
Simulator may use skyhook to check your location which is bounded to WiFi hotspot (if it was added with it's coordinates) and if you move in radius of WiFi coverage than your location won't be changed at all.
On the menu bar, go to Debug, then select Location, Custom Location and then you can input coordinates to simulate from. The Simulator will not always provide you the same feedback as a device would.

Resources