How to set MKMapView activityType? - ios

CLLocationManager has a property called activityType where we can set the type of activity that is associated with the location updates.
In my app, I display an MKMapView with user location on it so I do not use CLLocationManager. Since we can't access MKMapView's location manager, how do we set the MKMapView's activityType?
Thanks.

You can't and that's probably intentional. MKMapView uses CoreLocation to determine your location when you set showsUserLocation = NO, which I think is what you're doing. Per the description for that property:
... Setting this property to YES causes the map view to use the Core Location framework to find the current location and try to display it on the map. As long as this property is YES, the map view continues to track the user’s location and update it periodically. ...
If you want an activityType or desireAccuracy level of granularity you have to use the CoreLocation API classes. Namely CLLocationManager.
As an aside, I would assume that Apple's MKMapView is using the significant change location API to manage your location on the map (and possibly AGPS) to conserve battery life, but I could be wrong.
Anyway, I would just create an instance of CLLocationManager and set its activityType.

Related

Updating geofences with low accuracy in background, update position on map at high accuracy, does not work as intended

I'm writing an app which use the location for two purposes:
1) It shows the user's location on a map (with navigation-like accuracy) in a location manager instance and delegate, contained in a viewcontroller
2) In the background, even when closed, it gets the nearest 20 points of interest (list stored in userdefaults) and activates a geofence for these, so the user is noticed when getting nearby. This is done by lowest accuracy possible (3km) to save power, and set up in another location manager instance, initiated when the app is first opened.
The problem is, that the CLLocationmanager object seems to be static so when I call it from two different places, it's the same instance that's returned, and the accuracy, intervals etc, seems to be the same, since both delegates are called simultaneously all the time.
Therefore, the geofences setup method is called all the time, when the user just move a little bit, which I want the map to reflect.
Yes, there's only one location manager. So reconfigure it when you go into the background and when you come back into the foreground. It may be convenient to switch its delegate to another object at those times as well (so that each delegate object can focus on one problem).
the CLLocationmanager object seems to be static
Yup! CLLocationmanager object is a Singleton so different objects cannot be created. To Solve your problem you need check you application status, which can be done using :
[[UIApplication sharedApplication] applicationState] == UIApplicationStateBackground
Once you know the application state you can configure CLLocationmanager's object properties accordingly. Hope it helps!

CLLocationManager with MKMapView avoiding multiple instances

I have a question regarding how to properly set up my app to deal with multiple views needing location information. I have a two views. One a normal UIViewController that has a table view, the other, a UIViewController that houses an MKMapView. Now, both of these views need location information. As it stands, I have a singleton that implements a CLLocationManager to get all the user location information for the app. However, I was thinking about some other possibilities because sending a NSNotification every time the location is updated to change the center of my map seems like not the preferred approach as opposed to using the tracking methods in MKMapView. So, I am thinking about a couple options.
When the MKMapView appears, pause the singleton CLLocationManager and then, when it disappears start it up again. This way, only one location manager is updating at a time and I could still use the user tracking on the MKMapView. However, there will still be two objects that collect location data which seems like bad coding.
Use only the singleton CLLocationManager and update the center of the map based on that by posting notifications in the app.
As it stands, I am leaning toward option 1. However, I am very interested in what the preferred approach to a MKMapView and another view that needs location information in regards to how to update location throughout the app and was hoping someone could fill me in on what the proper approach would be. Thanks

Google map sdk for IOS 8 does not update location

I'm running on IOS8 simulator and counter this problem:
GMSMapView is able to load map but it didn't ask for Location's permission, thus it could not update current user's location. (I did set a custom location, using GPX file)
However, GMSMapView still works fine on IOS 7.1, 7.0 ...
My code is simple, init mapView, add it to controller's view and using KVO myLocation to observer location's change.
What should I do now ?
Google Maps SDK v1.8.1 doesn't request the location authorization that is needed in iOS8, so you need to do this in your application.
Use the CLLocationManager's requestWhenInUseAuthorization method, and in the authorization changed delegate, enable location updates in the map view when authorization can be given.
Also, don't forget to add the correct keys in the info.plist to explain to your users why you need their location, as without this the call to requestWhenInUseAuthorization don't do anything.

MKMapView Loads before LocationManager has got user location - cannot set Region to center on user location

I have an iOS app where Im loading a MKMapView as the app starts. I want to set the center of the "MKCoordinateRegion" to be the user latitude/longitude. However, the CLLocation Manager instance does not update the user location until after the MKMapView has loaded.
For now I am hard coding the map center coordinates into the app. But I was wondering if anyone can suggest a better way to handle this situation.
I can think of 2 approaches but Im not a fan of either of them:
1) Stall launching the MKMapView using an activityindicator
2) Launch the MKMapview with the hard coded location and then as soon as the user location is available animate the mapview region to center on that location
Any suggestions?
Store the last known lat/long when you go to background or when app is closed.
On start center map view to last known position.
As soon as location manager sends an updated location, update the map center.
Set the userTrackingMode property of the mapview to MKUserTrackingModeFollow. You don't need a location manager for this, mapView does it for you. When you later want to relocate the map, set the property to MKUserTrackingModeNone, and relocate your map.
Do what Apple does. Start really far zoomed out, maybe put a message on screen "Scanning for location", then when the location information is available zoom in.

How does MKMapView's showsUserLocation work?

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.

Resources