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
Related
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 am adding MKCircleView to the user annotation like so :
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
if (!_MapCentered) {
**_circle = [MKCircle circleWithCenterCoordinate:userLocation.coordinate radius:3000];
[_map_view addOverlay:_circle];**
_MapCentered = YES;
}
}
it will fire once and once the user location has traced, it works well but as you can see the diameter of the circle view is 3000 meters. so now i want the zoom level to fit the CircleView like so :
MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(mapView.userLocation.coordinate, 0.270, 0.270);
[_map_view setRegion:viewRegion animated:YES];
i have changed the delta degrees to other numbers but nothing is changed.
how can i manage this?
The distance parameters in the MKCoordinateRegionMakeWithDistance function are in meters (not degrees).
Also, the meters specify the full width and height so you have to use double the value of the circle's radius.
So it should be:
MKCoordinateRegion viewRegion =
MKCoordinateRegionMakeWithDistance
(mapView.userLocation.coordinate, 6000, 6000);
You could also just set the map view's visibleMapRect to the boundingMapRect of the circle overlay so you don't have to repeat the distance values:
mapView.visibleMapRect = _circle.boundingMapRect;
You need to set your span.
so set your span value in longitudeDelta & latitudeDelta
yourRegion.span.longitudeDelta = 0.004; // set required zoom value
yourRegion.span.latitudeDelta = 0.004; // set required zoom value
For Google's zoom level i use this category for MKMapView
Otherwise use Anna's solution
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.
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 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).