MKMapView: On startup user location -> zooming - iphone

I have a MKMapView and in the Map View "Show User Location" is set. The question if the app should use my location, I say yes. Then I see the blue bullet and I can zoom to the current location.
I read many other posts about this, but nothing solve the problem, that the user location won't zoom in automatically.
I want to have a zoom on startup if the user allows access the location, otherwise a defined coordinate should zoom in. (after, is the use allows the location, it can be updated, but should not set the center to the user location everytime I get updates on the location).
What are the steps to implement this behavior? I tried for example this: How do I zoom an MKMapView to the users current location without CLLocationManager? with the KVO but it does not work...
I hope someone has an idea?
Best Regards, Tim

Have you tried the delegate method mapView:didUpdateUserLocation:?
I used something like this in my code:
In the .h file:
#property (nonatomic, retain) CLLocation* initialLocation;
And in the .m file:
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
if ( !initialLocation )
{
self.initialLocation = userLocation.location;
MKCoordinateRegion region;
region.center = mapView.userLocation.coordinate;
region.span = MKCoordinateSpanMake(0.1, 0.1);
region = [mapView regionThatFits:region];
[mapView setRegion:region animated:YES];
}
}

you can do it like this in your viewDidLoad write this code
self.mapDetail.showsUserLocation = YES;
[self.mapDetail.userLocation addObserver:self
forKeyPath:#"location"
options:(NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld)
context:nil];
and this method will do the task
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
MKCoordinateRegion region;
region.center = self.mapDetail.userLocation.coordinate;
MKCoordinateSpan span;
span.latitudeDelta = 1; // Change these values to change the zoom
span.longitudeDelta = 1;
region.span = span;
[self.mapDetail setRegion:region animated:YES];
[self.mapDetail.userLocation removeObserver:self forKeyPath:#"location"];
}

Related

iOS6 iPhone App -- How to zoom to userLocation only once?

The below code is the method I've been using to zoom into the user's location, but then stop it from continuously re-zooming to it once the user moves the map. The problem with this method is that when it asks permission for userlocation data in the app, and I select yes, it doesn't do anything. I have to leave the page and return before it zooms correctly.
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{
if ([self.mapView showsUserLocation])
{
MKCoordinateRegion region;
region.center = self.mapView.userLocation.coordinate;
MKCoordinateSpan span;
span.latitudeDelta = .50; // Change these values to change the zoom
span.longitudeDelta = .50;
region.span = span;
[self.mapView setRegion:region animated:YES];
self.mapView.showsUserLocation = NO;}
}
The above method should be monitoring for user location changes and then set the location once it's found. I then set a condition in the if statement to make it stop -- otherwise it keeps moving back to the userlocation when you move the map around.
It sounds to me like self.mapView.showsUserLocation is set to FALSE when the popup appears because it doesn't know the user's location yet. To get around this problem, I would advise just using your own variable. Create a global boolean in this class and use it instead of self.mapView.showsUserLocation. Also, make sure that this function is, in fact, being called after the user agrees to the popup. Have you been NSLogging?

Zoom Level in MKMapView

I am working with map. I have a problem. I used following code to Zoom from the reference of this link in stackOverFlow
It's Easy to Zoom map.
But now,
I can't zoom in and out map. it means i cant change or find another place. it's only focus on current location. It's behave like an image Fix are. I can't understand what to do?
Please Help.
My Code as Follow.
- (void) viewDidLoad
{
[self.mapView.userLocation addObserver:self
forKeyPath:#"location"
options:(NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld)
context:nil];
}
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
MKCoordinateRegion region;
region.center = self.mapView.userLocation.coordinate;
MKCoordinateSpan span;
span.latitudeDelta = 1; // Change these values to change the zoom
span.longitudeDelta = 1;
region.span = span;
[self.mapView setRegion:region animated:YES];
}
I think the problem is that you are listening to user location changes (which most probably happening multiple times per second) and you are setting the map region to that region.
What you need to do is add a button on the map (like top left corner in Apple maps) which will toggle the map-mode to either free look or fixed to user location.
When the user press the button, you either remove/add the KVO. or toggle a boolean flag in your code. When the flag is true, you don't change map region. Something like:
#implementation YourController{
BOOL _followUserLocation;
}
- (IBAction) toggleMapMode:(id)sender{
_followUserLocation = !_followUserLocation;
}
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{
if(_followUserLocation){
MKCoordinateRegion region;
region.center = self.mapView.userLocation.coordinate;
MKCoordinateSpan span;
// retain the span so when the map is locked into user location they can still zoom
span.latitudeDelta = self.mapView.region.span.latitudeDelta;
span.longitudeDelta = self.mapView.region.span.longitudeDelta;
region.span = span;
[self.mapView setRegion:region animated:YES];
}
}
#end
Maybe you don't want all this and all you need is:
// retain the span so when the map is locked into user location they can still zoom
span.latitudeDelta = self.mapView.region.span.latitudeDelta;
span.longitudeDelta = self.mapView.region.span.longitudeDelta;

'Current location' BUTTON replica of the maps in iPhone

- (void)mapView:(MKMapView *)mv didAddAnnotationViews:(NSArray *)views {
for(MKAnnotationView *annotationView in views) {
if(annotationView.annotation == mv.userLocation) {
MKCoordinateRegion region;
MKCoordinateSpan span;
span.latitudeDelta=0.002;
span.longitudeDelta=0.002;
CLLocationCoordinate2D location=mv.userLocation.coordinate;
region.span=span;
region.center=location;
[mv setRegion:region animated:TRUE];
[mv regionThatFits:region];
}
}
Hello all. I have tried searching other posts and websites. In essence I want to duplicate the 'Current location' BUTTON.
Using the above code, I was able to zoom into the users current location. This however is not flawless, sometimes the GPS updates a little late or thinks I am somewhere else, zooms to that location but then the blue dot will shift somewhere off screen to where I really am.
I was wondering is there a way I can add a 'Button', exactly like maps on the iphone. It doesn't need to gain a new zoom etc, simply move to the new updated locaiton. My main source of code would be almost replica to here.
I appreciate any help.
Create a custom annotation with the same graphic. This object must implement MKAnnotationView, and you have to return the custom view for it in the method mapView:viewForAnnotation: of the MKMapViewDelegate. If you want the exact graphic, use the project UIKit-Artwork-Extractor to get it.
Save a reference to the annotation view, subscribe to core location updates, and update the position calling something like:
- (void) animateView:(UIView *)view toPosition:(NSValue*)value
{
CGPoint newCenter = [value CGPointValue];
[UIView animateWithDuration:0.5
animations:^{
view.center = newCenter;
}
completion:^(BOOL finished){ }];
}
Then set the center of the map on the annotation:
- (void)centerMap:(CLLocation *)location {
MKCoordinateRegion region = { { 0.0f, 0.0f }, { 0.0f, 0.0f } };
region.center = location.coordinate;
region.span.longitudeDelta = 0.15f;
region.span.latitudeDelta = 0.15f;
[self.mapView setRegion:region animated:YES];
}

iphone zoom to user location on mapkit

I'm using map kit and showing user's location using "showsUserLocation"
I"m using following code to zoom to user's location, but not zooming. Its zooming to some location in Africa, though the user location on map is showing correct.
MKCoordinateRegion newRegion;
MKUserLocation* usrLocation = mapView.userLocation;
newRegion.center.latitude = usrLocation.location.coordinate.latitude;
newRegion.center.longitude = usrLocation.location.coordinate.longitude;
newRegion.span.latitudeDelta = 20.0;
newRegion.span.longitudeDelta = 28.0;
[self.mapView setRegion:newRegion animated:YES];
Why is user's location correctly showing and not zooming properly. Can some one correct me please?
mapView.userLocation is set to a location off the coast of Africa (0,0) when first instantiated. As a result, I do something like the following, which causes the zoom to occur when the annotation appears. Of course, this is just an example:
- (void)mapView:(MKMapView *)mv didAddAnnotationViews:(NSArray *)views {
for(MKAnnotationView *annotationView in views) {
if(annotationView.annotation == mv.userLocation) {
MKCoordinateRegion region;
MKCoordinateSpan span;
span.latitudeDelta=0.1;
span.longitudeDelta=0.1;
CLLocationCoordinate2D location=mv.userLocation.coordinate;
region.span=span;
region.center=location;
[mv setRegion:region animated:TRUE];
[mv regionThatFits:region];
}
}
}
you can try:
mapView.userTrackingMode=YES;
mapView.userTrackingMode=NO;
UserTrackingMode will zoom to user Location.
Have you verified that the location returned by mapView.userLocation isn't at 0, 0?

Zoom on userLocation

how can I zoom the map on my userLocation automatically in my app?
I have the following code to zomm in the map but i must zoom on the userLocation and the following code zooms always to africa?
MKCoordinateRegion zoomIn = mapView.region;
zoomIn.span.latitudeDelta *= 0.2;
zoomIn.span.longitudeDelta *= 0.2;
zoomIn.center.latitude = mapView.userLocation.location.coordinate.latitude;
zoomIn.center.longitude = mapView.userLocation.location.coordinate.longitude;
[mapView setRegion:zoomIn animated:YES];
OK i solved the problem, with the following delegate Method:
-(void)mapView:(MKMapView *)mv didAddAnnotationViews:(NSArray *)views{
for(MKAnnotationView *annotationView in views) {
if(annotationView.annotation == mv.userLocation){
MKCoordinateRegion region;
MKCoordinateSpan span;
span.latitudeDelta=0.9;
span.longitudeDelta=0.9;
CLLocationCoordinate2D location =mv.userLocation.coordinate;
location = mv.userLocation.location.coordinate;
region.span = span;
region.center = location;
[mv setRegion:region animated:TRUE];
[mv regionThatFits:region];
}
}
}
thanks alot mate, this helped me greatly. Really wanted to vote but cant because of my rep level.
I also found that using:
mapView.userLocation.location.coordinate.latitude;
always zoomed me to africa, and never centered me onto my current location.
But by using the delegate method:
-(void)mapView:(MKMapView *)myMapView didAddAnnotationViews:(NSArray *)views {
I was able to solve this successfully. What I discovered is that when using mapView.userLocation.location.coordinate.latitude; in the viewDidLoad method wasn't triggering the correct user coords on startup of the view, hence it was displaying the wrong coords.