Odd behavior from MPMapView - ios

I have a getLocation button in my app that gets the user's current location, the button calls the following method:
-(void)setOriginalRegion {
float spanX = 0.217;
float spanY = 0.217;
MKCoordinateRegion region;
region.center.latitude = self.mapView.userLocation.coordinate.latitude;
region.center.longitude = self.mapView.userLocation.coordinate.longitude;
region.span.latitudeDelta = spanX;
region.span.longitudeDelta = spanY;
[self.mapView setRegion:region animated:YES];
}
That works just fine, and when tapped I get my location and a 15mile radius (as set by the delta's)...
To make sure my map loads up in the current user's location, I have added the following in my viewWillAppear method:
[self setOriginalRegion];
But that is starting the map somewhere in the middle of the ocean (approx 100 miles off the coast of Ghana.... This of course is not default, because by default it loads up in with North America. What am I doing wrong here?

As pointed out by #AndreMunis, viewWillAppear is too soon to know user's location, moved my method call to viewDidAppear and code works like a charm!

Related

How to control Over Zoom level in MKMapView in iOS

Please check the below Image. How to Avoid this type of zoom level in iPhone
Now getting Result:
Expected Result:
I don't think there is a way of telling if satellite images would be available at zoom level for given region. Better approach is to set your MKMapView to MKMapTypeStandard and give the user the option of switching to MKMapTypeSatellite in this case you will always have a map details to display on initial presentation of your MKMapView object
You can set the zoom by using MKCoordinateRegion and setting its span latitude & longitude delta as:
MKCoordinateRegion region;
region.center.latitude = {desired lat};
region.center.longitude = {desired lng};
region.span.latitudeDelta = 1;
region.span.longitudeDelta = 1;
region = [mapView regionThatFits:region];
[mapView setRegion:region animated:TRUE];
Vary the span.longitudeDelta and span.latitudeDelta to get the required zoom.

Change the position of Map point without changing zoom level or span

I am using following codes
MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(CLLocationCoordinate2DMake(annView.annotation.coordinate.latitude, annView.annotation.coordinate.longitude - .04), Some KM, Some KM);
MKCoordinateRegion adjustedRegion = [self.mapView regionThatFits:viewRegion];
[self.mapView setRegion:adjustedRegion animated:YES];
Otherway around is
MKCoordinateRegionMake(CLLocationCoordinate2DMake(annView.annotation.coordinate.latitude, annView.annotation.coordinate.longitude - .04), ADD SPAN HERE)
Both of these makes the map zoom. How is it possible that I change the Region without any zoom.
Get the current region and just change the center point.
// get current region
MKCoordinateRegion region = self.mapView.region;
// Update the center
region.center = CLLocationCoordinate2DMake(annView.annotation.coordinate.latitude, annView.annotation.coordinate.longitude - .04);
// apply the new region
self.mapView.region = region;
If you have a coordinate to set, use the MKMapView -setCenterCoordinate:animated: method. Animating the position change gives the user a clue about what's happened.
CLLocationCoordinate2D coordinate = annView.annotation.coordinate;
[myMapView setCenterCoordinate:coordinate animated:YES];
Also, no need to make a new coordinate, just use the one already in the annotation view.

How to zoom into user current location?

I wanna zoom into the user current location when the app starts in the MapKit.
This is my code (in the viewDidLoad function):
Locate=[[CLLocationManager alloc]init];
Locate.delegate=self;
Locate.desiredAccuracy=kCLLocationAccuracyBestForNavigation;
Locate.distanceFilter=kCLDistanceFilterNone;
[Locate startUpdatingLocation];
[super viewDidLoad];
//Region
MKCoordinateRegion myRegion;
//Center
CLLocationCoordinate2D center;
center.latitude=Locate.location.coordinate.latitude;
center.longitude=Locate.location.coordinate.longitude;
//Span
MKCoordinateSpan span;
span.latitudeDelta=0.3f;
span.longitudeDelta=0.3f;
//Set Region
myRegion.center=center;
myRegion.span=span;
[_MyMap setRegion:myRegion animated:YES];
Also I implemented the didUpdateLocation function with the same code as previous.
The problem is that when the user location is changing (when the user is moving) the screen makes zoom at him but I can't move the screen, if I try to move it return to the user location immediately, so I can't see the whole map.
For Zooming the map you have to change the region values means center and span.once see this one Mapview Zoom
in my case i have used this one for moving the map when i click on particular button.
MKCoordinateSpan span = MKCoordinateSpanMake(30.5982f,0.0001f);
CLLocationCoordinate2D coordinate = {36, 90};
MKCoordinateRegion region = {coordinate, span};
MKCoordinateRegion regionThatFits = [self.mapView regionThatFits:region];
[self.mapView setRegion:regionThatFits animated:YES];
I solved this issue by adding touchesMoved function and I stopped updating the Location in this function.
Solved.

Map Issue In iOS 6

In iOS 5 this worked fine, and I was under the impression that everything from MKMapKit would continue working in much the same way. However, instead of zooming to my location and adding annotations that I have set up, it just shows North America. I am leaving the annotations out of the code snippet I have, and just putting in the initial code setup.
[mapView setMapType:MKMapTypeSatellite];
[mapView setZoomEnabled:YES];
[mapView setScrollEnabled:YES];
mapView.delegate = self;
MKCoordinateRegion region = { {0.0, 0.0 }, { 0.0, 0.0 } };
region.center.latitude = 32.385325 ;
region.center.longitude = -86.217442;
region.span.longitudeDelta = 0.005f;
region.span.latitudeDelta = 0.005f;
[mapView setRegion:region animated:YES];
Any thoughts on why it is not zooming to where I have it set?
I personally don't suspect an iOS 6 v iOS 5 issue. This code works fine for me in iOS 6. So the problem rests elsewhere.
In short, a problem with the mapView variable seems far more likely. Have you checked to see that mapView is not nil? There are all sorts of simple candidate issues:
Perhaps you manually declared class instance variable for a property (which you should not do ... simply define your property and let the compiler synthesize your instance variable for you, precisely to avoid this sort of possible confusion); or
Perhaps there was a failure to link up the IBOutlet, etc.

How can I get a finer grained span in my MKMapView

I'm using an MKMapView and I'm initializing like this:
homeLocation.latitude = 42.033126;
homeLocation.longitude = -70.168621;
[self.mapView setCenterCoordinate:homeLocation animated:NO];
MKCoordinateRegion viewRegion;
viewRegion.center = homeLocation;
viewRegion.span = MKCoordinateSpanMake(1.7, 1.7);
[self.mapView setRegion:[self.mapView regionThatFits:viewRegion] animated:NO];
and here is my app
That's fine, except for my app I need to have it zoomed out a tiny bit more. But when I try this:
viewRegion.span = MKCoordinateSpanMake(1.8, 1.8); // use span of 1.8, not 1.7
I get this:
It is zoomed way out. If I look at the MKMapView's region the latitudeDelta i 3.54981 and the longitudeDelta is 3.51562.
How can I set my span to a value between 1.7 and 3.5? I'd love to hit around 2.25 for this particular application.
NOTE: the mapview pinch zooms just fine to whatever span I want.
MKCoordinateSpanMake uses degrees, and 1 degree is about 69 miles. If you want a more fine grained setting, use MKCoordinateRegionMakeWithDistance instead.
CLLocationCoordinate2D homeLocation;
homeLocation.latitude = 42.033126;
homeLocation.longitude = -70.168621;
[self.mapView setCenterCoordinate:homeLocation animated:NO];
MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance( homeLocation, 200000, 200000);
// 200000 meter is approximately 1.8 degree
[self.mapView setRegion:[self.mapView regionThatFits:viewRegion] animated:NO];
Have you tried on the device? Sometimes I have more control on the zoom level on the device than on the simulator.

Resources