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.
Related
I need to create ViewController with MKMapView. When it opens, there are should appear current location and slowly zooming out for 10 seconds (or less, it doesn't matter actually). And when animation completed application should perform segue to the next ViewController. Any ideas?
There is no "direct" method for setting a zoom level. You can, however, simulate this by setting the distance, which causes the image to resize.
CLLocationCoordinate2D noLocation;
MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(noLocation, 500, 500);
MKCoordinateRegion adjustedRegion = [self.mapView regionThatFits:viewRegion];
[self.mapView setRegion:adjustedRegion animated:YES];
self.mapView.showsUserLocation = YES;
You can either loop this, or simply set it twice, first to a BIG area and then the zoomed in area, and let the MapView's default animation do its thing.
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.
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.
I'm new to iOS map-kit, even though i have loaded mapview in my app using the following code
CLLocationCoordinate2D zoomLocation;
zoomLocation.latitude = 39.281516;
zoomLocation.longitude= -76.580806;
// 2
MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(zoomLocation, 0.5*METERS_PER_MILE, 0.5*METERS_PER_MILE);
// 3
MKCoordinateRegion adjustedRegion = [_mapView regionThatFits:viewRegion];
// 4
[_mapView setRegion:adjustedRegion animated:YES];
I want to allow user only to view a certain region not the whole world in the map, For example i take america, i want to allow user to view only america in the map, where he can't able to view any other places. What have to do to achieve this.. Needs your help.
You need to set the zoom level to a particular country or a particular region..... i think you must have a look at this.... that helped me when i was trapped in such kind of confusion... i hope that might help you too....
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];