I have an MKMapView in my app and have a button that turns on showsUserLocation for the map, while the map is finding the location I display a spinner so the user knows its working. I know when to stop the spinner by adding NSKeyValueObserver to the map's userLocation property. However if the user has Location Services disabled then the spinner just keeps on going forever but nothing happens, is there any way to know when location services are disabled while using an MKMapView?
Use locationServicesEnabled, a boolean property of a CLLocationManager.
The MapKit support for automatically showing the user's location is pretty poor. The app I'm writing shows the user's location, but I use CLLocationManager directly and add my own annotation to the map.
Note that locationServicesEnabled is new in 4.0, so won't work if your deployment target is lower.
Edit: Nevermind -- the class method [CLLocationManager locationServicesEnabled] is new in 4.0. But there is a now deprecated instance property .locationServicesEnabled for earlier SDK's.
Related
Anyone know how to show the "blue pulsating dot" for the user devices location on watchOS InterfaceMap element?
In iOS this can be done to MKMapView by calling mapView.showsUserLocation = true. InterfaceMap for watchOS doesn't seem to be supporting this and I was wondering if there is some other way to do this. Of course this could be done manually by placing an annotation on the user location every time the location is updated, but it seems a bit clunky and it would be great to have the default blue marker for the user location.
Thank you
You cannot do this on watchOS. WKInterfaceMap can only be used to show a static piece of the map at the moment. WKInterfaceMap shows a noninteractive map for a specific location. You can change the location shown on the map dynamically, but there are no built-in methods to update it automatically to show the user location in contrast to what MKMapView can achieve.
I am new to iPhone development, i am creating a location based app. i have searched lot related to location and came to knew that use one object ( singleton pattern ) of CLLocationManager through out app, in my app i have to update user's location to server using web service,
UPDATE
as discussion with Umer i came to knew that i can use one object in appdelegate of CLLocationManager and implement delegate methods in that, and update user location to server in app delegate ,
So, is it good to do in appDelegate ??
please help.
Your locationservices will run both in foreground and background, but to allow it to run in background (when the app is open and minimized), you need to declare a special background mode in your projects "Plist" file, if I'm not wrong.
The method didUpdateToLocation will give you updates every time there is a significant movement, which you can define and adjust through various settings, such as CLLocationAccuracy (locationManager.desiredAccuracy and locationManager.distanceFilter).
Depending on what desired accuracy you ask, battery main drain quicker or not.
I don't understand what you mean with "web service". These are not web-related, they're native functionalities of iOS.
You can just call the delegate method of locationManager StartUpdatingLocation when user moves to significant distance & also call the web-service for updating User's Location.
Both Task are done in Background mode.
UPDATED ANSWER
SOURCE CODE
My map view should work in 2 modes: with enabled Location Services and with disabled. If user disables locations services, application must give him the opportunity to manually set his current location. Also application should display blue pin that looks like MKUserLocation default pin. But the problem is userLocation property is readonly. And according to apple's documentation about MKUserLocation
You do not create instances of this class directly.
Can someone help me to solve this?
You should provide your own blue pin. System blue pin is considered to be used as read only and if you'll try to use it in some other way that will certainly lead to problems. So if you need some custom current location pin behavior you should implement your own pin for sure. Use CLLocationManager to update location if location services are enabled.
how do I display the user location in a mapView while not running GPS to update the location (at all)? Will
mapView.userTrackingMode = kCLLocationAccuracyNearestTenMeters;
do the job or will GPS still burn battery in the background?
Thanks :)
I'm going to attempt to answer your question as best as i can given the limited information.
The GPS has 3 main modes that go from low power to high power, low accuracy to high accuracy.
Location Tracking off
Background location updates (uses cell towers and wifi)
GPS location updates
An MKMapView will show a blue dot for the user location if you set showsUserLocation to YES. The MKMapView will use all available location methods to find the users location as accurately as possible and keep updating it while this is set to YES.
The tracking in MKMapView, a mode which keeps the users location centred on screen, moves the visible map region as the user moves and is available in iOS5. you are given three MKUserTrackingModes to choose from. From the docs:
MKUserTrackingModeNone: The map does not follow the user location.
MKUserTrackingModeFollow: The map follows the user location.
MKUserTrackingModeFollowWithHeading: The map follows the user location
and rotates when the heading changes.
So setting it to kCLLocationAccuracyNearestTenMeters isn't going to work as it is not an available option in this context.
Will showing the location burn battery in the background? It depends on what you mean by background. When the user taps the home button the MKMapView is forced to stop using the GPS by the system. While the app is running and the MKMapView is alive and has the showsUserLocation property set to YES it will keep using any available method, including the GPS, to update the location. You have a couple of strategies to reduce this.
1) presume that the user needs to see their location updated while the map is on screen. In which case keep showsUserLocation set to YES until the map is moved off screen, or the device is locked, then set it to NO
2) Presume the user only needs to see a static marker of their location from a particular point in time, like when the app is opened. In this case you need to make a CLLocationManager object, ask it to startUpdatingLocation, filter the delegate messages for the accuracy you want, and then turn off location updates (stopUpdatingLocation). You can then add an MKAnnotation to the MKMapView to show the user their location.
As you can see number 2 is more work, yet very power efficient. Number 1 is easy, but will use more battery while the map is visible. It is up to you to decide which behaviour the user expects and to implement your app accordingly.
Has any one successfully created user location pin that has a ripple like effect that 4.x (or higher) mapkit gives when we use mkMakkit.showUserLoaction = YES;
If any one has done it please guide me how to do it or past sample code that does that effect.
The user location in the mapkit is read-only property. Has anyone able to move the pin from user actual location. That is in my senario when the user location is not available for cases like iTouch user can select his location, I don't want to show user at Infinite Loop,CA but the location he prefers to choose.