Map Issue In iOS 6 - ios

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.

Related

Odd behavior from MPMapView

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!

MKMapView setRegion not working

I'm trying to set a custom starting region in an iOS app using MKMapView's setRegion. The app runs fine, and a map appears, but no matter what I try I can't get the region to change. I've tried many tutorials and solutions, but none are working. Here's my code:
-(void)viewDidAppear:(BOOL)animated{
MKCoordinateRegion region;
MKCoordinateSpan span;
span.latitudeDelta = 0.001;
span.longitudeDelta = 0.001;
region.span = span;
region.center.latitude = 100;
region.center.longitude = 100;
[mapView setRegion:(region) animated:(TRUE)];
}
I have the MKMapView and Core Location frameworks added to the projects properly, I import MapKit.h, and I declare mapView, so I don't know why it's not working.
Thank you,
Jacob
It sounds like the map view's IBOutlet is not connected to the map view control in the xib/storyboard.
If it's not connected, the mapView variable will be nil and calling methods on it will do nothing.
In the xib/storyboard, right-click on View Controller and connect the mapView outlet to the map view control.
Additionally, although not necessary for just setting the region, also connect the map view's delegate outlet to the View Controller. This will be required if you later implement any map view delegate methods. If this is not done, the delegate methods won't get called.
Another separate point:
In your first code example, you are setting the region's center to 100, 100. Please note that this is an invalid coordinate. If the map view was actually connected, setting the center to this would have caused a crash with "Invalid Region". Latitude must be from -90 to +90 (longitude must be from -180 to +180).
By the way, the new coordinate you're trying in the code posted in the comment (26, 80) is in India. Since you're setting the span to a relatively small value, you'll need to zoom out a lot to see this.
However it was set as answered, I will let here an example of what worked for me, when I had no clue why setRegion was not working at all. In my case the problem was the I've initiated the MKMapView without a frame, e.g.:
override func viewDidLoad() {
...
self.mapView = MKMapView() // Wrong!
...
self.mapView.setRegion(...) // Does not work!
}
It looks like that the whole region thing is calculated with regards the the initial (CGRect). It worked for me by doing:
override func viewDidLoad() {
...
self.mapView = MKMapView(frame: CGRect(0, 108, self.mapView.bounds.width, self.mapView.bounds.height))
self.mapView.setRegion(...) // Oh, it works!
}
This problem was described here too.

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.

MQMapView setRegion: causes app to crash

I'm trying to center my mapView on the user's location, but an exception is thrown that's caught by the AppDelegate before the mapView or view controller are even loaded.
mapView is an MQMapView
userLocation is assigned earlier from mapView.userLocation.location.coordinate
MQCoordinateSpan userSpan = MQCoordinateSpanMake(1000, 1000);
MQCoordinateRegion userRegion = MQCoordinateRegionMake(userLocation, userSpan);
[mapView setRegion:userRegion animated:true];
As far as I can make out from the MapQuest developer guide I'm calling setRegion correctly. Any idea what might be causing the exception?
I was having crashes with my iPhone4S and console revealed nan values for region. After trying about 7 different solutions from SO and various suggestions from Apple DTS, I solved it by eliminating the regionThatFits call. I simply used:
CLLocationDistance visibleDistance = 100000; // 100 kilometers
MKCoordinateRegion adjustedRegion = MKCoordinateRegionMakeWithDistance(zoomLocation, visibleDistance, visibleDistance);
[_mapView setRegion:adjustedRegion animated:YES];
Apparently there is a problem with that regionThatFits method.

MapView in iOS6 won't show certain zoom levels at latitude > 75 north

This code sets a default zoom level centered around a specified location in viewDidLoad.
The code works fine in previous versions of iOS:
CLLocationDistance visibleDistance = 100000; // 100 kilometers
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(location, visibleDistance, visibleDistance);
MKCoordinateRegion adjustedRegion = [mapView regionThatFits:region];
.
.
.
[mapView setRegion:adjustedRegion animated:NO];
However, in iOS6 for locations with latitude above ~ 75 (>75.1) the app crashes with the following message:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason:
'Invalid Region <center:nan, nan span:nan, nan>'
I found that for the given zoom level mapView can't set a proper MKCoordinateRegion internally. [mapView regionThatFits:region] returns all values as nan. If I use the region variable directly, it just shows the default map (the whole world).
After some testing I found that by adjusting the visibleDistance I can get the code to work properly. The magic distance seems to be slightly above 20 kilometers (somewhere between 22 and 23 km for a series of latitudes and latitudeDelta values).
This happens only on northern latitudes (-80 works just fine).
The maps work at any location after the initial positioning. It looks like Apple changed the way visible map regions are initialized. I'm using a higher zoom level for the affected region as a workaround. Is there any other way to make it work properly?
CLLocationDistance visibleDistance = 100000; // 100 kilometers
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(location, visibleDistance, visibleDistance);
MKCoordinateRegion adjustedRegion = [mapView regionThatFits:region];
.
.
.
[mapView setRegion:adjustedRegion animated:NO];
It will work..
CLLocationCoordinate2D southwest, northeast;
southwest.latitude = 34.172684;
southwest.longitude = -118.604794;
northeast.latitude = 34.236144;
northeast.longitude = -118.500938;
BSForwardGeocoderCoordinateBounds *bounds = [BSForwardGeocoderCoordinateBounds boundsWithSouthWest:southwest northEast:northeast];
try this....
I was having crashes with my iPhone4S and console revealed nan values for region. After trying about 7 different solutions from SO and various suggestions from Apple DTS, I solved it by eliminating the regionThatFits call. I simply used:
CLLocationDistance visibleDistance = 100000; // 100 kilometers
MKCoordinateRegion adjustedRegion = MKCoordinateRegionMakeWithDistance(zoomLocation, visibleDistance, visibleDistance);
[_mapView setRegion:adjustedRegion animated:YES];
Apparently there is a problem with that regionThatFits method.
I found a version of this code on a Chinese website and it seems to work for me. He is only bypassing sizeThatFits when the NAN is returned, thus only adjusting if necessary, and if this bug gets fixed by Apple (assuming it is a bug) then it won't be an issue at all.
MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(coordinate, mapSizeMeters, mapSizeMeters);
MKCoordinateRegion adjustedRegion = [mapView regionThatFits:viewRegion];
if (isnan(adjustedRegion.center.latitude)) {
// iOS 6 will result in nan. 2012-10-15
adjustedRegion.center.latitude = viewRegion.center.latitude;
adjustedRegion.center.longitude = viewRegion.center.longitude;
adjustedRegion.span.latitudeDelta = 0;
adjustedRegion.span.longitudeDelta = 0;
}
[mapView setRegion:adjustedRegion animated:YES];

Resources