setcentercoordinate not allowing to zoom in and out in ios 7 - ios

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.

Related

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!

How to make a MKMapView come in from the top

I have a normal MKMap view with a basic MKAnnoation what i want to accomplish is that my annotation is in the UK and the MKCoordinateSpan is 0.05 but i want the MKMapView start from a view of the whole UK and zoom in to the annotation.
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
/* set here the region from where map will be zooming; */
[self.mapView setRegion:MKCoordinateRegionForMapRect(MKMapRectWorld) animated:NO];
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
/* set here your end point of zooming */
CLLocationCoordinate2D zoomLocation;
zoomLocation.latitude = 39.281516;
zoomLocation.longitude= -76.580806;
[self.mapView setCenterCoordinate:zoomLocation animated:YES];
CLLocationDistance locationDistance = [self locationDistance:0.5];
MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(zoomLocation, locationDistance, locationDistance);
[self.mapView setRegion:viewRegion animated:YES];
}
- (CLLocationDistance )locationDistance:(CGFloat )value {
return 1609.344 *value;
}

Translate MapKit Region Button Action to Method

Apologies in advance for what must surely be so simple.
I want my map to zoom to a programmatically defined region surrounding the user's location. I've found tons of code where this is done via a button action, but I need it done automatically when the map loads. I'll post what I've tried here. Probably missing something very obvious. The meat of these both work as button actions, so I assume my issue is with my method code (first line of each).
Method 1: Using MKCoordinateSpanMake
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
float spanX = 0.00725;
float spanY = 0.00725;
MKCoordinateRegion region;
region.center.latitude = self.mapView.userLocation.coordinate.latitude;
region.center.longitude = self.mapView.userLocation.coordinate.longitude;
region.span = MKCoordinateSpanMake(spanX, spanY);
[self.mapView setRegion:region animated:YES];
}
Method 2: Using MKCoordinateRegionMakeWithDistance
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
MKUserLocation *userLocation = _mapView.userLocation;
MKCoordinateRegion region =
MKCoordinateRegionMakeWithDistance (
userLocation.location.coordinate, 3000, 3000);
[_mapView setRegion:region animated:YES];
}
As always, thanks so much for your time and advice.
Move to viewDidLoad:
- (void)viewDidLoad
{
MKUserLocation *userLocation = _mapView.userLocation;
MKCoordinateRegion region =[_mapView regionThatFits:MKCoordinateRegionMakeWithDistance (userLocation.location.coordinate, 3000, 3000)];
[_mapView setRegion:region animated:YES];
}
Hope that will help.

How to set zoom level to user location in MapView

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];
}

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