I am unable to call
-(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
On following IBAction
- (IBAction)button:(UIButton *)sender {
}
help me
-(IBAction)button:(UIButton *)sender {
[self mapView:yourMapViewObject didUpdateUserLocation:yourLactionObject];
}
You Can't explicitly call the delegate method of MKMapView.
It will get called when the location of the user gets updated.
Take a look at apple doc here
So, if you want to call the same action for MKMapView delegate and button selector, create new function which is called from
-(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
and also which is called from your button selector. You shouldn't call MKMapView delegate method.
Related
I have a mapView:regionDidChangeAnimated: delegate method that is called at the beginning of the app when MKMapView is loaded. How can I stop it from being called right away? I don't mind it being called when user drags the map, but just not when mapview is loaded.
- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated {
I am adding some functionality in
- (void)mapView:(MKMapView *)mapView regionWillChangeAnimated:(BOOL)animated
and
- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated
I want this to be called when the map region is changed.
How can i prevent these delegate methods from being called when device changes its orientation?
Add a property BOOL didRotate
Then add something like
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{
self.didRotate = YES;
}
and
- (void)mapView:(MKMapView *)mapView regionWillChangeAnimated:(BOOL)animated{
if(self.didRotate) return;
}
and
- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated{
if(self.didRotate) {
self.didRotate = NO;
return;
}
}
These delegate methods are called when the frame of the map view changes. The frame can change on rotation because of autolayout constraints or autoresizing masks. So one solution would be to keep the map view frame constant.
You could also try to unset the delegate while the interface orientation is changing.
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
_mapView.delegate = nil;
}
-(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
_mapView.delegate = self;
}
I have a mapViewController (in a Navigation Controller). When I open it for the first time, after viewDidLoad, - (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation gets called.
When I go back to the previous viewController and come again to the mapViewController, the didUpdateUserLocation delegate is not being called, hence my annotations are not getting shown.
Please help me in finding the solution to the problem. Thank you.
If you want the map view to continue to track the user’s location and update it periodically, you should set MapView showsUserLocation to YES (the default value is NO).
Try
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
...
[mapView setShowsUserLocation: YES];
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
...
[mapView setShowsUserLocation: NO];
}
Apple Doc: http://developer.apple.com/library/ios/#DOCUMENTATION/MapKit/Reference/MKMapView_Class/MKMapView/MKMapView.html
Call this method in - view will appear method of same class
[locationManager startUpdatingLocation];
From a detail view of an event I want to go to a MapViewController, zoom in to the annotation, and open it's callout.
Here is some of the relevant code:
#interface MapViewController : UIViewController<MKMapViewDelegate>
...
- (void) viewWillAppear:(BOOL)animated
{
[self displayAnnotations];
}
- (void) viewDidAppear:(BOOL)animated
{
...
// Zoom in to event
[map setRegion:region animated:YES];
}
- (void) mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated
{
...
regionAnimationEnded = YES;
[self selectAnnotation:a];
...
}
- (void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views
{
...
// check to see if the right view is in the array
...
annotationViewDidAppear = YES;
[self selectAnnotation:a];
...
}
- (void) selectAnnotation:(id<MKAnnotation>)annotation
{
if(annotationViewDidAppear && regionAnimationEnded)
{
if(!openedAnnotationFirstTime)
{
[map selectAnnotation:annotation animated:YES];
openedAnnotationFirstTime = YES;
}
}
}
This works on the ios 6 simulator, but on the ios 5.1 simulator (and on the device) the annotion view isn't visible as it says in the docs:
(void)mapView:(MKMapView *)mapView didAddAnnotationViews:(NSArray *)views
By the time this method is called, the specified views are already added to the map.
So it depends which finishes first: if the region change animation finishes last and the annotation view has appeared it works, otherwise it doesn't.
Any help would be appreciated.
Your selectAnnotation: method is being called twice. Try calling it only once, in mapView:regionDidChangeAnimated: delegate method.
I ended up using perform selector with delay on the actual [map selectAnnotation:annotation animated:YES];
It's a work-around but seems to work nicely.
i am loading a mapviewcontroller when the view map button is pressed from my mainviewcontroller:
- (IBAction)mapButton:(id)sender {
MapKitDisplayViewController *mapKitDisplayView = [[MapKitDisplayViewController alloc] init];
[self presentModalViewController:mapKitDisplayView animated:YES];
[mapKitDisplayView release]; mapKitDisplayView = nil;
}
On startup of this map view this method is called, which correctly zooms to the users location:
- (void)mapView:(MKMapView *)myMapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
myMapView.showsUserLocation = YES;
NSLog(#"didUpdateUserLocation = '%#'", userLocation);
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance([userLocation coordinate], 20000, 20000);
[myMapView setRegion:region animated:YES];
}
I have a done button on this screen that dismisses this view:
- (IBAction)mapDoneButton:(id)sender
{
[self dismissModalViewControllerAnimated:YES];
}
At this stage the user is taken back to the main view controller, however when i press the view map button again, the didUpdateUserLocation method is never called! Hence, the map view is zoomed out to the default one and wont zoom in as before.
How can i get it to be called again?
thanks
MKMapView object doesn't trigger an update on user location on its own. You need to activate the search by setting showsUserLocation = YES. This will then fetch the user's location and then the delegate method mapView:didUpdateUserLocation: is called. So set this value to YES on viewWillAppear: and once you've the user location you can set it to NO to stop tracking the user. If you don't do that, it periodically updates the user location and calls the delegate method. Without doing this, user location should be unreliable.
What i do is next:
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation{
[[NSNotificationCenter defaultCenter] postNotificationName:#"gotUserLocation" object:self];}
then in viewDidLoad check of user location is available. if it is not register view controller as reciever for that notification:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(zoomToLocation) name:#"gotUserLocation" object:nil];
if user location is available just call zoomToLocation method