can't set zoom level on MKMapView - ios

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

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 :)

Display the map scale in mapkit

I am working with mapkit in Xcode 5.1 and am trying to display the map scale in regionDidChangeAnimated. I have no idea now to accomplish this though. I tried to look around and was unsuccessful. Any ideas?
EDIT:
-(void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated {
CGPoint nePoint = CGPointMake(self.mapView.bounds.origin.x + mapView.bounds.size.width, mapView.bounds.origin.y);
CGPoint swPoint = CGPointMake((self.mapView.bounds.origin.x), (mapView.bounds.origin.y + mapView.bounds.size.height));
CLLocationCoordinate2D neCoord;
neCoord = [self.mapView convertPoint:nePoint toCoordinateFromView:self.mapView];
CLLocationCoordinate2D swCoord;
swCoord = [self.mapView convertPoint:swPoint toCoordinateFromView:self.mapView];
CLLocationDistance distance = [neCoord distanceFromLocation:swCoord];
}
Any reason why I am getting an error with the last line, CLLocationDistance?
Use the methods that translate between the coordinates on the map and the points on the map view, such as convertPoint:toCoordinateFromView:. Use the edge points of your view for this.
Now you have the coordinates - you can calculate the distances between the points with CLLocation's distanceFromLocation:. You can make some assumptions about the width of your view based on the physical properties of, day an iPhone or iPad and calculate the scale.

MKMapView Region

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.

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.

Zooming MKMapview to bound annotations makes annotation right on edges of mapview

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

Resources