How to control Over Zoom level in MKMapView in iOS - 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.

Related

How to set the zoom level of the MKMap view making MKCircle(varying radius) visible?

My map view contains one circle which covers many pinpoints with one center point.Whenever the user clicks a button the map should be zoomed in such a level the circle is visible.The map should not be zoomed more or less it should show exact circle.When user logs out then circle will have new radius in next login.How to calculate the appropriate zoom level.
My code:
-(void)showCircle{
//calculate new radius
long radius=[self calculateRadius];
MKCircle *circle= [[MKCircle alloc]init];
circle = [MKCircle circleWithCenterCoordinate:CLLocationCoordinate2DMake([groupLat floatValue], [groupLon floatValue]) radius:radius];
[myMapView addOverlay:circle];
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(CLLocationCoordinate2DMake([groupLat floatValue], [groupLon floatValue]), 800, 800);
MKCoordinateSpan span;
//calculate zoom level
double radius=[circle radius]);
double rad = radius + radius / 2;
double scale = rad / 500;
zoomLevel=(16 - log(scale) / log(2));
region.span.latitudeDelta =zoomLevel;
region.span.longitudeDelta =zoomLevel;
[myMapView setRegion:region animated:YES];
}
Thank you in advance!
You can try : mapRectThatFits(:) or mapRectThatFits(:edgePadding:)
Something like this, maybe, to zoom the map according to your circle :
myMapView.visibleMapRect = [myMapView mapRectThatFits:circle.boundingMapRect];
Hope this helps you :)

Different views in Apple maps and MKMapview for same place

Hi I have integrated MKMapView map in my iOS application. Apple maps show different view for one place and MKMapview shows different view for same place. How can I get clear view with zoom level like Apple maps.
This is the Apple maps view screenshot link
This is the MKMapView map screenshot link
it's look like the same place. But issue it you map has less zoom level.
Reduce you span value to make it more zoom. So you will get the same result. Try to solve by error and trial. you can check the below code to set span value of the map.
MKCoordinateSpan span;
span.latitudeDelta = 0.4;
span.longitudeDelta = 0.4;
MKCoordinateRegion region;
region.center = newLocation.coordinate;
region.span = span;
[mapView setRegion:region animated:NO];

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 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.

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