Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Yes, it is possible to have a separate location manager object and assign its value to the mapview (BTW, I'm using '=' below as list prefix to prevent the SO code-formatter from borking).</p> <p>= In your <code>UIViewController</code> maintain two separate properties: one to a <code>MKMapView</code> and one to a <code>CLLocationManager</code>. </p> <p>= Create a XIB file with the <code>MKMapView</code> and any other window chrome you want. Connect the outlets to the controller propeties. Make sure <code>MKMapView</code> does NOT follow user location.</p> <p>= Have the UIViewController implement the <code>CLLocationManagerDelegate</code> protocol--especially the <code>locationManager:didUpdateToLocation:fromLocation:</code> method which will be called whenever a new location value is available. We'll be setting the controller as the delegate for the location manager.</p> <p>= In the viewController's <code>loadView</code> method, load the NIB with the <code>MKMapView</code> in it. To give user feedback you may want to put up a <code>UIActivityIndicatorView</code> spinner and set it to <code>startAnimating</code>. Then you start with:</p> <pre><code>self.locationManager = [[CLLocationManager alloc] init]; self.locationManager.delegate = self; self.locationManager.desiredAccuracy = kCLLocationAccuracyKilometer; self.locationManager.distanceFilter = 10; // or whatever [self.locationManager startUpdatingLocation]; </code></pre> <p>= In <code>locationManager:didUpdateToLocation:fromLocation:</code> check to see if the event was updated since the last N seconds. Then tell the location manager to stop updating, the spinner to stop animating, and get the lat/long data and assign it to the map view along with a view span and region so it zooms and centers to the right place.</p> <p>= Now here's the tricky part: the blue marble 'throbber' is a feature of the mapview tracking user location. You'll have to momentarily 'fake it' until the real one kicks in (or just use a separate marker for the current location and maintain its position yourself). Personally I'd go with the blue marble that the user is familiar with. </p> <p>= To make it so it shows right at startup you will need to create a custom MKAnnotationView with just the blue marble graphic added at the location returned by the location manager. This means taking a snapshot of a Mapview with the location showing, then photoshopping just the blue marble out and using it as the image for a custom annotation view.</p> <p>= If you want it to actively follow the map, you can enable the userlocation tracking of the mapview and when it gets the actual data, you hide your previously set marker and let the mapview do the updating. The other option is to allow the existing location manager to continue receiving updates every second or so and update the position of the blue marble annotation yourself.</p> <p>= To let the mapview's own userLocation do the updating add to viewDidLoad:</p> <pre><code>[self.map.userLocation addObserver:self forKeyPath:@"location" options:(NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld) context:NULL]; self.map.showsUserLocation = YES; // starts updating user location </code></pre> <p>= Implement <code>observeValueForKeyPath</code>. It gets called when the <code>location</code> attribute of the mapview's <code>userlocation</code> has a value:</p> <pre><code>-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { if ([self.map isUserLocationVisible]) { [self.locationManager stopUpdatingLocation]; self.ownBlueMarble.hidden = YES; } // The current location is in self.map.userLocation.coordinate } </code></pre> <p>= To avoid the warm-up delay in showing current location, keep a reference to the viewController containing the map and the location manager so it doesn't go away (it's a bit of a memory hog but if you release it you'll have to wait again until MapView loads the tiles and is ready to go). </p> <p>= In <code>viewWillLoad</code> you can stuff the last known location into the custom bluemarble annotation and show it. Toggle on/off the userLocation tracking and when you get the notification the same hide-the-annotation-show-the-real-marble trick will work. The mapview's own location manager kicks in and when it has the data, you can make your annotation marker disappear.</p> <p>= You might want to implement the viewController's <code>viewWillDisappear</code> method and manually turn off <code>userLocation</code> tracking on the mapview so it's off by default the next time the view is brought up. You'll also want to get the last known <code>userLocation</code> and save it for the next get-go. That way, you can do all the positioning marker-juggling in the <code>viewWillAppear</code> method and not have to worry about userLocation interfering until you're ready for it.</p> <p>Good luck.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload