How do I reset an MKMapView back to the world view zoom level?
The map rect for the world is stored as a constant named MKMapRectWorld.
MKCoordinateRegion worldRegion = MKCoordinateRegionForMapRect(MKMapRectWorld);
map.region = worldRegion;
One other way is to set the zoom level of the map. Although the MapKit framework does not support zoom levels as Google Maps API does, you can use this category extension written by Troy Brant.
Set the center coordinate to 0, 0 with zoom level 0 to get the same result.
[map setCenterCoordinate:CLLocationCoordinate2DMake(0, 0) zoomLevel:0 animated:0];
Try this:
MKCoordinateRegion region = MKCoordinateRegionMake(mapView.centerCoordinate, MKCoordinateSpanMake(180, 360));
[mapView setRegion:region animated:YES];
This will set a new region for the map view using the current center coordinate, and the maximum possible span (180 degrees of latitude, 360 degrees of longitude).
For those hoping to do this in Swift:
let region = MKCoordinateRegion(.world)
mapView.setRegion(region, animated: true)
MKMapView can not zoom out to show the whole world. No matter what you are using, MKMapRectWorld, zoom level 0, or MKCoordinateSpanMake(180, 360).
Related
Google Maps has had this for a while (double tap then hold and slide finger up or down to zoom in or out) but Apple is just adding it to their Apple Maps app in iOS 11.
It does not seem like they are going to add this to MKMapView, at least not in the current betas.
If Apple does not give MKMapView this functionality how would I do this using gestures? Has anyone else done this?
This will help you to zoom out to world-view.
// step.1 create co-ordianate-span as follows
MKCoordinateSpan span = {.latitudeDelta = 180, .longitudeDelta = 360};
// step.2 crate region as follows
MKCoordinateRegion region = MKCoordinateRegionMake(CLLocationCoordinate2DMake(0.0000f, 0.0000f), span);
// step.3 zoom using region to map as follows
[self.mapView setRegion:region animated:YES];
I have a MKPinAnnotationView that is always in the center of the map. When panning and zooming
the pin gives me the center coordinates (lat/long) of the map.
Currently when you zoom in, it just zooms into wherever your directing the map to zoom into.
I'd really like to lock the zoom onto the pin.
Any ideas on how I'd achieve this?
Assuming by MKPinAnnotation you mean MKPinAnnotationView, you can access the annotation's coordinate and use that to create a region and subsequently set the region of the mapView to that region centered on the coordinate:
MKCoordinateRegion region = MKCoordinateRegionMake(pin.annotation.coordinate, MKCoordinateSpanMake(.05, .05));
[self.mapView setRegion:region animated:YES];
I have a question regarding setting the region on my MKMapView.
I need to set the mapview to display a specific region when my view first loads.
The north east and south west latitude and longitude of this region is:
North East Coordinate Lat:59.623724 Long:2.911587
South West Coordinate Lat:49.004833 Long:-11.361825
Further to this, I would like to 'lock' the mapview to this region. Ideally the lock will be transparent, i.e: the coordinates above represent the maximum extent of the MKMapView. However if it is simply a case of checking the northeast and southwest coordinates within
- (void)mapView:(MKMapView *)aMapView regionDidChangeAnimated:(BOOL)animated
and resetting the view if they exceed my maximum range, that would be acceptable to me also.
Many thanks for any pointers on this matter.
EDIT:
Regarding the first part of my question, I have figured out I can set the initial region on the MKMapView using the following code:
CLLocationCoordinate2D neCoord;
neCoord.latitude = 59.787643;
neCoord.longitude = 3.025857;
CLLocationCoordinate2D swCoord;
swCoord.latitude = 49.394171;
swCoord.longitude = -11.036642;
MKCoordinateRegion region;
region.center.latitude = neCoord.latitude - (neCoord.latitude - swCoord.latitude) * 0.5;
region.center.longitude = neCoord.longitude + (swCoord.longitude - neCoord.longitude) * 0.5;
region.span.latitudeDelta = fabs(neCoord.latitude - swCoord.latitude); // Add a little extra space on the sides
region.span.longitudeDelta = fabs(swCoord.longitude - neCoord.longitude); // Add a little extra space on the sides
region = [self.mapView regionThatFits:region];
[self.mapView setRegion:region animated:YES];
First, you'll need to make sure you set the region on the map view after the view has been displayed. If you set it before the map has loaded, it probably won't center on that region. Once you've done that, just set self.mapView.zoomEnabled = NO; and self.mapView.scrollEnabled = NO; and it will prevent the user from moving the map around.
If you want to lock the maximum bounds the user can view but still allow scrolling and zooming, you will have to use -mapView:regionDidChangeAnimated: and 'bump' the user back inside your bounds if they leave it. Note that the user experience for this will probably suck - they'll pan around, let go, and then the map will suddenly move back to the region you defined. You could try using -mapView:regionWillChangeAnimated: and modify the map region if they left your boundaries, that could be a little less jarring.
Hi have MKMapView and I make it to zoom depending on the annotations added to mapview, but sometimes I see map zoomed to some level in which annotations fall on the edges and half visible. Below is the code i'm using to set the map region.
MKPolygon *poly = [MKPolygon polygonWithCoordinates:points count:annotationCount];
MKCoordinateRegion region=MKCoordinateRegionForMapRect([poly boundingMapRect]);
Please provide some solution, Thanks.
So your region is too small, have you considered making it bigger?
Your MKCoordinateRegion has a CLLocationCoordinate2D (center) and a MKCoordinateSpan (span). That MKCoordinateSpan has a latitudeDelta (consider this the height) and a longitudeDelta (consider this the width). What you want to do is a make a slightly larger region. So my first guess is
region.span.latitudeDelta = region.span.latitudeDelta * 1.01;
Then set your mapview to that region
I have an app which is providing a zoomable MKMapView to the user. I want to be able to store the user's preferred coordinates and zoom level for when the map view is first displayed.
Currently, in viewDidLoad, I am providing a default set of coordinates and zoom level for the initial map presentation:
zoomLocation.latitude = 55.50;
zoomLocation.longitude = -5.50;
// specify size of region to display
MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(zoomLocation, 340.0*METERS_PER_MILE, 340.0*METERS_PER_MILE);
// auto adjust region to fit screen
MKCoordinateRegion adjustedRegion = [mapView regionThatFits:viewRegion];
// display the new region
[mapView setRegion:adjustedRegion animated:YES];
What I am trying to do is, when the user scrolls and zooms the map to their preferred default view, they can hit the "Set Default" button and store the required properties to implemented in future whenever the view loads.
To store the coordinates of the user's selected view, I have this:
// gets coordinates of currently viewed map image
CGPoint pointCentrePoint = CGPointMake(mapView.frame.size.width/2, mapView.frame.size.height/2);
centrePoint = [mapView convertPoint:pointCentrePoint toCoordinateFromView:mapView];
NSLog(#"LAT: %f LON: %f", centrePoint.latitude, centrePoint.longitude);
What I'm struggling with is how to store the user's selected MKCoordinateRegion, or zoom level. Is there a way I can access this property for the current view, so it can be reused in future when the view loads?
Unlike the JavaScript Google Maps api, the MKMapView doesn't have a readily available "zoom level" property (it isn't really necessary for your purpose).
Work with the center and span values in the region property.
This question gives an example of how to save/load the region to NSUserDefaults.