How to set-up zoom level in iOS? - ios

I am using the code given below to set a region in map view:
CLLocationCoordinate2D loc = (CLLocationCoordinate2D )[self geoCodeUsingAddress:searchBar.text];
CLLocationDistance visibleDistance = 100000;
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(loc, visibleDistance, visibleDistance);
MKCoordinateRegion adjustedRegion = [self.mapView regionThatFits:region];
[self.mapView setRegion:adjustedRegion animated:YES];
[self.mapView setCenterCoordinate:loc];
[self.mapView setZoomEnabled:YES];
It shows the entire map in a small area. How can increment the zoom level?

You can also take a help of span value for MapView. Animate to that small area and span map to zoom for required value.
Use below code:
CLLocationCoordinate2D loc = (CLLocationCoordinate2D )[self geoCodeUsingAddress:searchBar.text];
[UIView animateWithDuration:1.5 animations:^{
MKCoordinateRegion region;
region.center.latitude = loc.latitude;
region.center.longitude = loc.longitude;
region.span.latitudeDelta = 0.15f; // Decrement value to zoom
region.span.longitudeDelta = 0.15f; // Decrement value to zoom
[self.mapView setRegion:region animated:YES];
} completion:^(BOOL finished) { }];

Change the visibleDistance to a smaller number. Experiment until you find something you like.

Finally I have solved the issue by removing
[self.mapView setCenterCoordinate:loc];
I worked well when i removed the above.

Try this,
CLLocationCoordinate2D loc = (CLLocationCoordinate2D )[self geoCodeUsingAddress:searchBar.text];
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(loc, visibleDistance, visibleDistance);
region.span.latitudeDelta = 0.15f;
region.span.longitudeDelta = 0.15f;
[self.mapView setRegion:region animated:YES];
[self.mapView setZoomEnabled:YES];

Related

Map View - Current Location after moving the map view

Loaded Map view on the screen.
If we move the map from left and right
i need to get the current location on the screen immediatly when i click a button
i used the following code to get the current location initially
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation{
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(userLocation.coordinate, 800, 800);
[self.mapView setRegion:[self.mapView regionThatFits:region] animated:YES];
// Add an annotation
MKPointAnnotation *point = [[MKPointAnnotation alloc] init];
point.coordinate = userLocation.coordinate;
point.title = #"Where am I?";
point.subtitle = #"I'm here!!!";
[self.mapView addAnnotation:point];
}
by clicking the button call below method.
- (void)gotoLocation
{
float spanX = 0.05;
float spanY = 0.05;
MKCoordinateRegion region;
region.center.latitude = mapView.userLocation.coordinate.latitude;
region.center.longitude = mapView.userLocation.coordinate.longitude;
region.span.latitudeDelta = spanX;
region.span.longitudeDelta = spanY;
[mapView setRegion:region animated:YES];
}

How to zoom out on load for mkmapview in iOS 7

This code I have works for iOS 6 but doesn't seam to have an effect on iOS 7. How can I get it to work for iOS 7 when setting the span doesn't have an effect?
CLLocationCoordinate2D ctrpoint = self.location.coordinate;
MapPoint *mp = [[MapPoint alloc] initWithCoordinate:ctrpoint title:[NSString stringWithFormat:#"%#: %#",NSLocalizedString(#"Pin Name:", #"Pin Name:"),self.pin.name] subtitle:self.pin.cell];
[self.mapView addAnnotation:mp];
MKCoordinateSpan span;
span.latitudeDelta = 0.2;
span.longitudeDelta = 0.2;
MKCoordinateRegion region;
region.span = span;
region.center = self.location.coordinate;
[self.mapView setRegion:region animated:YES];
the proper way is to use the constant provided:
- (MKCoordinateRegion)regionForWorld {
return MKCoordinateRegionForMapRect(MKMapRectWorld);
}
so
- (void)zoomToWorldAnimated:(BOOL)animated {
MKCoordinateRegion region = [self regionForWorld];
[self.mapView setRegion:region animated:animated];
}
Assuming that self.mapView is already synthesised I would do something like this:
MKCoordinateRegion region = [self.mapView regionThatFits:MKCoordinateRegionMakeWithDistance([self.location.coordinate, 800, 800)];
region.span.latitudeDelta = 0.2;
region.span.longitudeDelta = 0.2;
[self.mapView setRegion:region animated:YES];
Please note that the 800 values in MKCoordinateRegionMakeWithDistance are "CLLocationDistance latitudinalMeters" and "CLLocationDistance longitudinalMeters". You might have to change these depending on your map size and the zoom level you want.
For the map point I would do something like this:
MKPointAnnotation *mapPoint = [[MKPointAnnotation alloc] init];
[mapPoint setCoordinate:self.location.coordinate];
[mapPoint setTitle:#"Your title"];
[mapPoint setSubtitle:#"My subtitle"];
[mapView addAnnotation:mapPoint];
I have tested this in both iOS6 and iOS7 and can assure you it works just fine.

Map view not showing exact location needs to zoom out

I am new to ios and working on map view i have showned position on map view (Annotation)using lat, long. map view is showing location, but need to do pinching to get exact location.(zoom with two fingures) I am using following code but it not showing exact location for exact location i need to use zoom then is showing exact location but i want to display it without touching to map view??
zoomLocation.latitude = latmpa.doubleValue;
zoomLocation.longitude = logmpa.doubleValue;
annotationPoint = [[MKPointAnnotation alloc] init];
annotationPoint.coordinate = zoomLocation;
annotationPoint.title = #"masjid....";
[mapView selectAnnotation:annotationPoint animated:YES];
[mapView addAnnotation:annotationPoint];
mapView.centerCoordinate = annotationPoint.coordinate;
You need to work out the region you want the map to be looking at and then use [mapView setRegion:newRegion animated:YES] ref.
An MKCoordinateRegion is made of a CLLocationCoordinate2D which you've already made and a MKCoordinateSpan. Here's an example of making a span
MKCoordinateSpan span;
span.latitudeDelta = 1.5;
span.longitudeDelta = 1.0;
MKCoordinateRegion newRegion;
newRegion.center = zoomLocation;
newRegion.span = span;
[mapView setRegion:newRegion animated:YES];

MKMapView Setting Initial Zoom level

I use this code in viewWillLayoutSubviews to set the initial region of my map.
CLLocationCoordinate2D startCoord = CLLocationCoordinate2DMake(13.747266, 100.526804);
MKCoordinateRegion adjustedRegion = [self.mapView regionThatFits:MKCoordinateRegionMakeWithDistance(startCoord, 800, 800)];
[self.mapView setRegion:adjustedRegion animated:YES];
NSLog(#"%f",adjustedRegion.span.latitudeDelta);
However, the initial zoom level doesn't work. The coordinate is correct, but it always zoom in to probably the max level. I check the span of the region and got 0.0. How do I fix this.
You need to set your span.So give your span value here.
adjustedRegion.span.longitudeDelta = 0.005;
adjustedRegion.span.latitudeDelta = 0.005;
Custom span setting:
region.span.longitudeDelta = 0.04;
region.span.latitudeDelta = 0.04;
else programmatically:
region.span.longitudeDelta = geoMapView.region.span.latitudeDelta;
region.span.latitudeDelta = geoMapView.region.span.latitudeDelta;

how can I limit the map area to only one country in iOS?

I am making an app for iOS using mapkit. I want to limit the boundaries of the map only to a specific region/country. Is there a way to do this?
There's no way to tell the map not to scroll out of a certain area. The only way I could think to do it would be to stop the user from scrolling when you hit one of your fences. The example below is written without testing or compiling at all so you may need to tweek it yourself but hopefully it'll get you started..
ViewController.h
CLLocationCoordinate2D myNorthEast, mySouthWest;
ViewController.m
-(void)viewDidLoad{
myNorthEast = CLLocationCoordinate2DMake(lat1,lon1);
mySouthWest = CLLocationCoordinate2DMake(lat2,lon2);
[super viewDidLoad];
}
-(void)mapView:(MKMapView *)mapView regionWillChangeAnimated:(BOOL)animated{
/*
/ Check if the map region is going to change outside your fence.
/ If so, programmatically set it back to the edge of your fence.
*/
if(!animated){
return; // Don't want to get stuck in a loop after you set your region below.
}
MKCoordinateRegion region = [mapView region];
// You will need to get the NE and SW points of the new region to compare
// First we need to calculate the corners of the map so we get the points
CGPoint nePoint = CGPointMake(mapView.bounds.origin.x + mapView.bounds.size.width, mapView.bounds.origin.y);
CGPoint swPoint = CGPointMake(mapView.bounds.origin.x, bounds.origin.y + mapView.bounds.size.height);
// Then transform those point into lat,lng values
CLLocationCoordinate2D neCoord;
neCoord = [mapView convertPoint:nePoint toCoordinateFromView:mapView];
CLLocationCoordinate2D swCoord;
swCoord = [mapView convertPoint:swPoint toCoordinateFromView:mapView];
/*
You will need to mess around with the lat/lon & sign of the new center calculation for the other cases.
*/
if(neCoord.latitude > myNorthEast.latitude){
MKCoordinateRegion newRegion;
newRegion.span = region.span;
CLLocationCoordinate2D newCenter;
newCenter.longitude = region.center.longitude;
newCenter.latitude = myNorthEast.latitude - region.span.latitudeDelta;
newRegion.center = newCenter;
[mapView setRegion:newRegion animated:NO];
}else if(neCoord.longitude < myNorthEast.longitude){
}else if(swCoord.latitude < mySouthWest.latitude){
}else if(swCoord.longitude > mySouthWest.longitude){
}
}
Part of this comes from this answer:
Getting the bounds of an MKMapvIew
Here is a further specification of the previous answer. Note that this will be different for different parts of the world:
if(neCoord.latitude > myNorthEast.latitude){
MKCoordinateRegion newRegion;
newRegion.span = region.span;
CLLocationCoordinate2D newCenter;
newCenter.longitude = region.center.longitude;
newCenter.latitude = myNorthEast.latitude - region.span.latitudeDelta / 2;
newRegion.center = newCenter;
[mapView setRegion:newRegion animated:NO];
} else if(swCoord.latitude < mySouthWest.latitude){
MKCoordinateRegion newRegion;
newRegion.span = region.span;
CLLocationCoordinate2D newCenter;
newCenter.longitude = region.center.longitude;
newCenter.latitude = mySouthWest.latitude + region.span.latitudeDelta / 2;
newRegion.center = newCenter;
[mapView setRegion:newRegion animated:NO];
} else if(neCoord.longitude > myNorthEast.longitude){
MKCoordinateRegion newRegion;
newRegion.span = region.span;
CLLocationCoordinate2D newCenter;
newCenter.longitude = myNorthEast.longitude - region.span.longitudeDelta / 2;
newCenter.latitude = region.center.latitude;
newRegion.center = newCenter;
[mapView setRegion:newRegion animated:NO];
} else if(swCoord.longitude < mySouthWest.longitude){
MKCoordinateRegion newRegion;
newRegion.span = region.span;
CLLocationCoordinate2D newCenter;
newCenter.longitude = mySouthWest.longitude + region.span.longitudeDelta / 2;
newCenter.latitude = region.center.latitude;
newRegion.center = newCenter;
[mapView setRegion:newRegion animated:NO];
}

Resources