How to set zoom level to user location in MapView - ios

Im using MKMapView to get the users location and when the view loads it shows the whole world view. Is there anyway that I can set a zoom level so that the users don't have to keep zooming in always to get the city view or street view...It would be a great help. I've posted my code below for reference...
- (void)viewDidLoad
{
[super viewDidLoad];
[ self.mapView.delegate self];
[self.mapView setShowsUserLocation:YES];
// Do any additional setup after loading the view, typically from a nib.
}
-(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
CLLocationCoordinate2D loc = [userLocation coordinate];
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(loc, 500, 500);
[self.mapView setRegion:region animated:YES];
}
- (void)viewDidUnload {
[super viewDidUnload];
[_mapView setShowsUserLocation:NO];
}

The code you've posted should work for the most part except for this critical line in viewDidLoad:
[ self.mapView.delegate self];
Essentially, this line does nothing.
It is not setting the map view's delegate property (which is what I assume it is supposed to do).
What it is actually doing is calling self on the delegate property.
The map view's delegate is not getting set (it stays nil) and so the didUpdateUserLocation delegate method never gets called and so the map is not zooming to the user's location.
The line should be this:
[self.mapView setDelegate:self];
or even simpler:
self.mapView.delegate = self;
Note that in iOS 5 or later, you can just set userTrackingMode and the map view will automatically zoom to and follow the user's location so you don't have to do it manually.
Also note that since iOS 6, viewDidUnload is deprecated and is not even called by the OS. You probably want to move the disabling of showsUserLocation to viewWillDisappear (and move the enabling to viewWillAppear).

Try this May be Help full for you
MKCoordinateRegion startupRegion;
startupRegion.center = CLLocationCoordinate2DMake(26.0000, 75.00);
startupRegion.span = MKCoordinateSpanMake(0.5, 0.597129);
[mapViewObj setRegion:startupRegion animated:YES];
here 26.0000 and 75.00 is user location and MKCoordinateSpanMake is zoom level. It's increasing your zoom as you wish.

Try this, In ViewDidLoad
CLLocationCoordinate2D zoomLocation;
zoomLocation.latitude = 39.281516; // your latitude value
zoomLocation.longitude= -76.580806; // your longitude value
MKCoordinateRegion region;
MKCoordinateSpan span;
span.latitudeDelta=0.18; // change as per your zoom level
span.longitudeDelta=0.18;
region.span=span;
region.center= zoomLocation;
[self.mapView setRegion:region animated:TRUE];
[self.mapView regionThatFits:region];

set MKCoordinateRegion and change region span according to your requirement
MKCoordinateRegion region;
CLLocation* currentLocation = (CLLocation*)from ; // current location
if(currentLocation.coordinate.latitude > maxLat)
maxLat = currentLocation.coordinate.latitude;
if(currentLocation.coordinate.latitude < minLat)
minLat = currentLocation.coordinate.latitude;
if(currentLocation.coordinate.longitude > maxLon)
maxLon = currentLocation.coordinate.longitude;
if(currentLocation.coordinate.longitude < minLon)
minLon = currentLocation.coordinate.longitude;
region.center.latitude = (maxLat + minLat) / 2;
region.center.longitude = (maxLon + minLon) / 2;
region.span.latitudeDelta = 0.4; // change as per required zoom level
region.span.longitudeDelta = 0.4; // change as per required zoom level
// input that region and set map area as below 30000
_mapView.region =MKCoordinateRegionMakeWithDistance(currentLocation.coordinate,30000, 30000);
[_mapView setRegion:region animated:YES];
[_mapView regionThatFits:region];
[_mapView reloadInputViews];
// center your map at Current location
_mapView.centerCoordinate = _mapView.userLocation.location.coordinate;
// To get current location use CLLocationManagerDelegate
#interface GetCurrentLocation : NSObject<CLLocationManagerDelegate>
#property(nonatomic,strong)CLLocationManager *_locationManager;
-(void)getLocation;
#end
-(void)getLocation{
_locationManager = [[CLLocationManager alloc] init];
[_locationManager setDelegate:self];
[_locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
_locationManager.distanceFilter = kCLDistanceFilterNone;
[_locationManager startUpdatingLocation];
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation// you can get current location here
{
CLLocationCoordinate2D coord;
coord.latitude = newLocation.coordinate.latitude;
coord.longitude = newLocation.coordinate.longitude;
}
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
// NSLog(#"locationManager:%# didFailWithError:%#", manager, error);
}

-(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
MKCoordinateRegion mapRegion;
mapRegion.center = mapView.userLocation.coordinate;
mapRegion.span.latitudeDelta = 0.3;
mapRegion.span.longitudeDelta = 0.3;
[mapView setRegion:mapRegion animated: 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];
}

MKMapView frozen after setting region

I have a MKMapView and want to limit the zoom-out-level.
My approach is the following using the regionWillChangeAnimated-Method:
- (void)mapView:(MKMapView *)mapView regionWillChangeAnimated:(BOOL)animated {
//Maximum zoom-out-level
MKCoordinateSpan maxSpan;
maxSpan.latitudeDelta = 0.2;
maxSpan.longitudeDelta = 0.2;
if (self.mapView.region.span.latitudeDelta > maxSpan.latitudeDelta ) {
CLLocation *location = [self.locationManager location];
CLLocationCoordinate2D coordinate = [location coordinate];
MKCoordinateRegion region;
region.center = coordinate;
region.span = maxSpan;
[self.mapView setRegion:region animated:YES];
}
}
When I zoom out of the maximum span, the map-region is reset correctly to the maximum.
Unfortunately, after I call setRegion:, the mapView freezes and I can't zoom or interact anymore with the mapView. The app doesn't crash though.
Does anyone know what's wrong in my code? Is there maybe a better way to limit the zoom-out-level?
Thanks a lot in advance!

setcentercoordinate not allowing to zoom in and out in ios 7

The same method is working fine in ios 6
I am using didUpdateToLocation method and its implemented as follows:-
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)Location fromLocation:(CLLocation *)oldLocation
{
if(appDelegate->mapView )
{
if(Span)
{
[appDelegate->mapView setRegion:MKCoordinateRegionMake(Location.coordinate, MKCoordinateSpanMake(0.01f, 0.01f)) animated:YES];
Span = NO;
}
else
{
[appDelegate->mapView setCenterCoordinate:Location.coordinate animated:YES];
}
}
}
Please suggest what should be done so that the user's location should also be tracked and user should also be able to zoom in and out on the map.The map should also center itself around the current GPS location.
i use this to zoom the the users location when the map is loaded.
- (void)mapViewDidFinishLoadingMap:(MKMapView *)mapView {
NSLog(#"mapViewDidFinishLoadingMap");
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 2 * NSEC_PER_SEC), dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
[self zoomMapView:mapView ToUserLocation:mapView.userLocation.coordinate];
}); }
-(void)zoomMapView:(MKMapView*)mapView ToUserLocation:(CLLocationCoordinate2D)coordinate {
if (!isZoomed) {
isZoomed=TRUE;
MKCoordinateRegion region = mapView.region;
MKCoordinateSpan span;
region.center = coordinate;
span.latitudeDelta = 0.02;
span.longitudeDelta = 0.02;
region.span=span;
[mapView setMapType:MKMapTypeStandard];
[mapView setZoomEnabled:YES];
[mapView setScrollEnabled:YES];
[mapView setShowsUserLocation:YES];
[mapView setRegion:region animated:YES];
} }
you can call the -(void)zoomMapView:(MKMapView*)mapView ToUserLocation:(CLLocationCoordinate2D)coordinate method whenever you want to zoom the the users current location.

adding a drop pin button

I'm creating a button that will place a pin on users current location. But when ever I press it, instead is is placed in the middle of the global map, around Africa, I believe this is because in the original tutorial where I got this from, they created a void method with "didUpdateUserLocation" but I can figure a way to add this to the IBAction , here's what am working with. Has anyone ever had trouble with this?
This is the zooming in code but notice the didUpdateUserLocation down there vvvv
-(void)mapView: (MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
MKCoordinate region = MKCoordinateRegionMakeWithDistance(userLocation.coordinate,
800,800);
[self.mapView setRegion:[self.mapView regionThatFits:region] animated:NO];
}
`
Now here is the IBAction that when tapped, drops pin around Africa or the center of the global map. I figured it's because it is missing the didUpdateUserLocation
-(IBAction)addAnnotation;
{
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(userLocation.coordinate,
800,800);
[self.mapView setRegion:[self.mapView regionThatFits:region] animated:NO];
MKPointAnnotation *point = [[MKPointAnnotation Alloc] init];
point.coordinate = myLocation.coordinate;
point.title = #"Test";
point.subtitle = #"Text";
[self.mapView addAnnotation:point];
}
So if anybody would know how to, what i figure is the solution, didUpdateUserLocation to the IBAction, please let me know, appreciate it!
Try this...
- (void)mapView:(MKMapView *)aMapView didUpdateUserLocation:(MKUserLocation *)aUserLocation
{
MKCoordinateRegion region;
CLLocationCoordinate2D location;
location.latitude = aUserLocation.coordinate.latitude;
location.longitude = aUserLocation.coordinate.longitude;
region.span = aMapView.region.span;
region.center = location;
[self.objMapView setRegion:region animated:FALSE];
showCurrentLoc = FALSE;
}
-(IBAction)addAnnotation;
{
MKCoordinateRegion region;
region.span = self.mapView.region.span;
region.center = location;
[self.mapView setRegion:region animated:FALSE];
MKPointAnnotation *point = [[MKPointAnnotation Alloc] init];
point.coordinate = self.mapView.userLocation.coordinate;
point.title = #"Test";
point.subtitle = #"Text";
[self.mapView addAnnotation:point];
}

Zoom automatically returns to its initial zoomlevel

My objective is to make a map in which the user location is shown with annotation and zoom, everything is ok, the location is there with a red annotation and a pretty zoom, for that I have a view which is called PositionActuelleViewController, here is my code :
PositionActuelleViewController.h :
#interface PositionActuelleViewController : UIViewController<MKMapViewDelegate,CLLocationManagerDelegate> {
MKMapView *mapView;
MKReverseGeocoder *geoCoder;
MKPlacemark *mPlacemark;
CLLocationCoordinate2D location;
}
#property (nonatomic,retain)IBOutlet MKMapView *mapView;
#end
PositionActuelleViewController.m :
- (void)viewDidLoad {
[super viewDidLoad];
[mapView setShowsUserLocation:TRUE];
[mapView setMapType:MKMapTypeStandard];
[mapView setDelegate:self];
[self.view insertSubview:mapView atIndex:0];
CLLocationManager *locationManager=[[CLLocationManager alloc] init];
[locationManager setDelegate:self];
[locationManager setDesiredAccuracy:kCLLocationAccuracyNearestTenMeters];
[locationManager startUpdatingLocation];
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{
location = newLocation.coordinate;
MKCoordinateRegion region;
region.center = location;
MKCoordinateSpan span;
span.latitudeDelta = .005;
span.longitudeDelta = .005;
region.span = span;
[mapView setRegion:region animated:TRUE];
}
My only problem is that the zoom in is always enabled even if the user zoom out the map, it zoomed in automatically. How can I fix this?
If you only want to zoom in once, you can add a boolean ivar called didZoomToUserLocation for example.
In viewDidLoad, initialize it to NO before the call to startUpdatingLocation:
didZoomToUserLocation = NO;
[locationManager startUpdatingLocation];
Then in didUpdateToLocation, change the code like this:
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{
location = newLocation.coordinate;
if (didZoomToUserLocation)
return;
didZoomToUserLocation = YES;
MKCoordinateRegion region;
region.center = location;
MKCoordinateSpan span;
span.latitudeDelta = .005;
span.longitudeDelta = .005;
region.span = span;
[mapView setRegion:region animated:TRUE];
}
Note that this will also stop following the user on the map (but the location ivar will still be updated).
If you want to keep following the user but zoom in only the first time, then do this instead:
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{
location = newLocation.coordinate;
if (didZoomToUserLocation)
{
//just re-center map on user's location without changing zoom...
[mapView setCenterCoordinate:newLocation.coordinate animated:YES];
}
else
{
didZoomToUserLocation = YES;
MKCoordinateRegion region;
region.center = location;
MKCoordinateSpan span;
span.latitudeDelta = .005;
span.longitudeDelta = .005;
region.span = span;
[mapView setRegion:region animated:TRUE];
}
}
Also, in your viewDidLoad, you don't need to call insertSubview on the mapView if it's created in IB.
Your code in didUpdateToLocation sets a span of a fixed value. Every time that delegate call is invoked it will set the zoom based on those .005 spans.
If you simply put a breakpoint or log in the function you'll see it generally gets called quite frequently.

Resources