简体   繁体   中英

MKMapView setRegion: causes app to crash in iOS SDK

I want to zoom MKMapView on custom coordinates. So I using following code.

CLLocationCoordinate2D myCurrentLocationCordinate = CLLocationCoordinate2DMake(latitude, longtitude);
[myMapView setCenterCoordinate:myCurrentLocationCordinate animated:YES];
MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(myCurrentLocationCordinate, 500, 500);
MKCoordinateRegion adjustedRegion = [myMapView regionThatFits:viewRegion];
[myMapView setRegion:adjustedRegion animated:YES];

But my app is randomly crashing on setRegion: Method. And getting this log when it is crashing :-

adjustedRegion description: (MKCoordinateRegion) adjustedRegion = {
center = (latitude = 23.066432958888399, longitude = 72.531898498535213) span = (latitudeDelta = 0.0051205743267423998, longitudeDelta = 0.0048793707173899747) }

Also sometimes getting following error :-

* Terminating app due to uncaught exception 'NSGenericException', reason: '(null) must implement title when canShowCallout is YES on corresponding view >' * First throw call stack: (0x18e8b91b8 0x18d2f055c 0x19ab76244 0x19abb2c74 0x19ab74d44 0x19ab8db58 0x19477c7dc 0x19abb041c 0x19abb0290 0x19abb1588 0x1001ae010 0x1001ad318 0x101265258 0x101265218 0x101275080 0x101274b78 0x18d94b2a0 0x18d94ad8c) libc++abi.dylib: terminating with uncaught exception of type NSException

I am sure not why it is crashing randomly. Let me know if any other proper solution for zooming the MKMapView on custom coordinates.

Any help would be appreciated.

I also got the same issue, but solved using this

 CLLocationCoordinate2D startCoord = CLLocationCoordinate2DMake(latitudeAPI, longtitudeAPI);
   [myMapView setRegion:MKCoordinateRegionMakeWithDistance(myCurrentLocationCordinate, 500.0, 500.0) animated:NO];

There is no need to use this line

MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(myCurrentLocationCordinate, 500, 500);
MKCoordinateRegion adjustedRegion = [myMapView regionThatFits:viewRegion];

Hope it works and thanks to @Firoze Lafeer to help me to solve this.

I handle my mapView zoom this way. Could you try it this way?

CLLocationCoordinate2D zoomLocation = CLLocationCoordinate2DMake(40.741168, 30.333555);
MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(zoomLocation, 0.5* 1609.344, 0.5* 1609.344);

[self.myMapview setRegion:viewRegion animated:YES];
[self.myMapview setDelegate:self];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM