Within a UICollectionViewCell, setRegion has no effect for a MKMapView - ios

I have set up a simple MKMapView (it is intended just to display a location on the map and nothing more) within one cell of a section of a CollectionView and am unable to set its region using setRegion. The MKMapViewDelegate is correctly set to the UICollectionViewCell as shown by breakpoints being hit within all the delegate methods.
I first tried to set the region in the View Controller's cellForItemAtIndexPath but I'm guessing this is too early, i.e. before the map is loaded so nothing happens and it loads a default location in the middle of the ocean.
I have tried passing the CLCoordinateLocation2D from the view controller to a CLCoordinateLocation2D in the cell during cellForItemAtIndexPath to set the region within the cell but where is the appropriate place to set the region? In a ViewController it should be done in ViewDidAppear rather than ViewDidLoad or ViewWillAppear so that the map is loaded but I cannot find an equivalent event for a collectionviewcell. awakeFromNib is clearly too soon. I have tried using various mapView delegates such as:
- (void)mapView:(MKMapView *)mapView1
regionWillChangeAnimated:(BOOL)animated
{
MKCoordinateSpan span = {.latitudeDelta = 0.0025, .longitudeDelta = 0.0025};
MKCoordinateRegion adjustedRegion = [mapView1 regionThatFits:MKCoordinateRegionMakeWithDistance(coords, 200, 200)];
[mapView1 setRegion:adjustedRegion animated:YES];
}
which runs but again does not change the region. I have tried
- (void)mapViewDidFinishLoadingMap:(MKMapView *)mapView and
- (void)mapViewWillStartRenderingMap:(MKMapView *)mapView
which seemed the best bets but a breakpoint before and after the setRegion line shows the region is never updated to the region I'm setting it to. The events all should be at a time that the map is fully loaded so why does setRegion simply have no effect??
Cell h file:
#interface BasicsCollectionViewCell : UICollectionViewCell
#property (strong, nonatomic) IBOutlet MKMapView *mapView;
#property (nonatomic) CLLocationCoordinate2D coords;
#end
Cell m file:
#interface BasicsCollectionViewCell() <MKMapViewDelegate>
#end
#implementation BasicsCollectionViewCell
#synthesize mapView;
#synthesize coords;
- (void)mapView:(MKMapView *)mapView1
regionWillChangeAnimated:(BOOL)animated
{
MKCoordinateSpan span = {.latitudeDelta = 0.0025, .longitudeDelta = 0.0025};
MKCoordinateRegion adjustedRegion = [mapView1 regionThatFits:MKCoordinateRegionMakeWithDistance(coords, 200, 200)];
[mapView1 setRegion:adjustedRegion animated:YES];
}
And the relevant part of cellForItemAtIndexPath:
cell0 = (BasicsCollectionViewCell*)[collectionView dequeueReusableCellWithReuseIdentifier:#"BasicsCell" forIndexPath:indexPath];
CLLocationCoordinate2D coord = {.latitude = self.latitude, .longitude = self.longitude};
[cell0 setCoords:coord];
cell0.mapView.delegate = cell0;
MKCoordinateSpan span = {.latitudeDelta = 0.0025, .longitudeDelta = 0.0025};
MKCoordinateRegion adjustedRegion = [cell0.mapView regionThatFits:MKCoordinateRegionMakeWithDistance(coord, 200, 200)];
[cell0.mapView setRegion:adjustedRegion animated:YES];
return cell0;
EDIT:
If I put breakpoints in the following code before and after the setRegion line at the asterixed points...
- (void)mapViewWillStartRenderingMap:(MKMapView *)mapView
{
MKCoordinateSpan span = {.latitudeDelta = 0.0025, .longitudeDelta = 0.0025};
MKCoordinateRegion adjustedRegion = [mapView regionThatFits:MKCoordinateRegionMakeWithDistance(coords, 200, 200)];
*****[mapView setRegion:adjustedRegion animated:YES];
*****}
then the debugger gives the following at the first breakpoint:
po self.mapView.region
{
(latitude = -41.508575000000008, longitude = 174.19921900000011)
(latitudeDelta = 13.733789001832008, longitudeDelta = 59.305518116231042)
}
And the same at the breakpoint immediately after:
po self.mapView.region
{
(latitude = -41.508575000000008, longitude = 174.19921900000011)
(latitudeDelta = 13.733789001832008, longitudeDelta = 59.305518116231042)
}
While at both breakpoints, the following is the (correct) value of adjustedRegion:
po adjustedRegion
{
(latitude = -0.13506928037132482, longitude = 51.518398284912109)
(latitudeDelta = 0.0018087389042361623, longitudeDelta = 0.0058458603824931288)
}
So it is simply that either setRegion is being ignored for some reason or it’s overridden by something else. Any ideas which?

I have faced similar one, where map region was not changed. So I manually called mapView delegate method
- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated;
in - (void) viewDidAppear:(BOOL)animated of view controller.
- (void) viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
// Manually called delegate method to forcefully set region.
[self mapView:self.mapView regionDidChangeAnimated:YES];
}
In your case call this method right after when you set map region. From your above code for example in cellForItemAtIndexPath method. For example:
cell0 = (BasicsCollectionViewCell*)[collectionView dequeueReusableCellWithReuseIdentifier:#"BasicsCell" forIndexPath:indexPath];
CLLocationCoordinate2D coord = {.latitude = self.latitude, .longitude = self.longitude};
[cell0 setCoords:coord];
cell0.mapView.delegate = cell0;
MKCoordinateSpan span = {.latitudeDelta = 0.0025, .longitudeDelta = 0.0025};
MKCoordinateRegion adjustedRegion = [cell0.mapView regionThatFits:MKCoordinateRegionMakeWithDistance(coord, 200, 200)];
[cell0.mapView setRegion:adjustedRegion animated:YES];
[cell0.mapView setRegion:adjustedRegion animated:YES];
// Force call of delegate method
[self mapView:cell0.mapView regionDidChangeAnimated:YES];
return cell0;
Note:
Don't forgot to override regionDidChangeAnimated method in view controller.
- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated {
}

Related

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

Incorrect user location shown on MKMapView after scaling the map programmatically

I have MKMapView which have UserTrackingMode = MKUserTrackingModeFollow,
and I have adding a circle overlay to show a region of the certain diameter at user location.
Also user can change the diameter of the region so I want to scale the map to insure whole region/circle is shown on that portion of the map.
The problem I have now is that scaling the map number of times by setting region results in incorrect user location annotation - it is moved from the correct location.
I cannot understand why is that happens, I see in the debugger that the mapView.userLocation property have correct coordinates.
But once new update is happens or I move the map manually - the annotation jumps to the correct place.
This is the code:
- (void)addCircleWithRadius:(double)radius andCoordinate:(CLLocationCoordinate2D)coordinate
{
_regionCircle = [MKCircle circleWithCenterCoordinate:coordinate radius:radius];
[_regionCircle setTitle:#"Region"];
MKCoordinateRegion region;
region.center.latitude = coordinate.latitude;
region.center.longitude = coordinate.longitude;
region.span.latitudeDelta = 0.00002 * _regionCircle.radius;
region.span.longitudeDelta = 0.00002 * _regionCircle.radius;
MKCoordinateRegion adjustedRegion = [self.mapView regionThatFits: region];
[self.mapView setRegion:adjustedRegion animated:TRUE];
_circleView = nil;
[self.mapView addOverlay:_regionCircle];
}
- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay
{
if(!_circleView)
{
_circleView = [[MKCircleView alloc] initWithCircle:overlay];
_circleView.strokeColor = [UIColor blueColor];;
_circleView.fillColor = [UIColor blueColor];
_circleView.alpha = 0.25;
_circleView.lineWidth = 2.0;
}
return _circleView;
}
- (IBAction)regionSliderValueChanged:(id)sender
{
[self updateRadiusCircle];
}
- (void) updateRadiusCircle
{
[self.mapView removeOverlays:self.mapView.overlays];
CLLocationCoordinate2D myCoordinate = {_currentLocation.coordinate.latitude, _currentLocation.coordinate.longitude};
[self addCircleWithRadius:self.radiusSlider.value andCoordinate:myCoordinate];
}
I have published the video on YouTube to better understand the issue.
https://www.youtube.com/watch?v=474gdjkGwJA

MKMapKit iOS 5. Can I set a desired delegate when I want?

I have a question about map and delegate. I am doing a exercise from a book and it says to me to use the method
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
//CLLocationCoordinate2D loc = [userLocation coordinate];
self.coord2D = [userLocation coordinate];
//MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(loc, 250, 250);
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(self.coord2D, 250, 250);
[worldView setRegion:region animated:YES];
}
Which means every time I move my phone from my location it will show myself on screen. I also create a button that calls this method. Therefore if I am scrolling my map I can see where I am.
The problem is: if I am in a car and want to scroll my map it will become a difficult task due this method will be call all the time I change my location.
Is there any other method or if I have an option to activate and deactivate a delegate?
Thanks in advance.
As far as I can tell it's quite simple. Just have a button for activating and deactivating it.
- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
//CLLocationCoordinate2D loc = [userLocation coordinate];
self.coord2D = [userLocation coordinate];
if (self.trackUserLocation) {
//MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(loc, 250, 250);
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(self.coord2D, 250, 250);
[worldView setRegion:region animated:YES];
}
}
The property:
#property (nonatomic, assign) BOOL trackUserLocation;
The button action:
self.trackUserLocation = !self.trackUserLocation;
You don't have to make it any harder than that.

MKCircle is not updating radius but it's translating

I've to draw an MKCicle into an MKMapView. Then I've to re-draw it when user, through a slider, change the radius. I remove it and I re-create it, re-adding it to the map.
But instead of do what I'm expecting, I see the MKCircle translating over the map, maintaining the same size.
Here's my code:
- (MKOverlayView *)mapView:(MKMapView *)map viewForOverlay:(id)overlay
{
MKOverlayView* overlayView = nil;
if(overlay == self.circle)
{
//if we have not yet created an overlay view for this overlay, create it now.
if(nil == self.circleView)
{
self.circleView = [[[MKCircleView alloc] initWithCircle:self.circle] autorelease];
self.circleView.fillColor = [UIColor blueColor];
self.circleView.strokeColor = [UIColor blueColor];
self.circleView.alpha = 50;
self.circleView.lineWidth = 2;
}
overlayView = self.circleView;
}
return overlayView;
}
-(void)drawPolygonWithLocation
{
[self.mapView removeOverlay: self.circle];
MKCoordinateRegion region;
region.center.latitude = self.geofenceLocation.latitude;
region.center.longitude = self.geofenceLocation.longitude;
region.span.latitudeDelta = 0.005;
region.span.longitudeDelta = 0.005;
MKCoordinateRegion adjustedRegion = [self.mapView regionThatFits: region];
[self.mapView setRegion:adjustedRegion animated:TRUE];
self.radius = (double)(slRadius.value);
NSLog(#"Raggio: %f", self.radius);
NSLog(#"Lat: %f, Lon: %f", region.center.latitude, region.center.longitude);
self.circle = [MKCircle circleWithCenterCoordinate:self.geofenceLocation.coordinate radius: self.radius];
NSLog(#"CIRCLE: radius %f Lat: %f, Lon: %f", self.circle.radius, self.circle.coordinate.latitude, self.circle.coordinate.longitude);
[self.mapView addOverlay:self.circle];
}
-(IBAction)updateRadius:(id)sender
{
[self drawPolygonWithLocation];
}
The NSLog is writing into the console right values, the center doesn't change and the radius changes according to the user input.
But, again, the MKCircle translates going on the north-west.
Thanks in advance,
Samuel Rabini
Fixed.
I just add
self.circleView = nil;
before the
[self.mapView addOverlay:self.circle];
in this way it works fine.
Samuel

MKMapView freezes on User Interaction

I have an application where I show a small map with one annotation, the map is in a objectDetailScreen that I created, this screen is updated whenever a new object is set, also showing a new map. The map as a annotation for the object.
Whenever I try to move the view, tap the annotations pin or zoom, the map freezes for a while and I have no clue why it does that. The app is iPad only (iPad 2, iOS 4.3.5). Here is the code that sets the map:
- (void) setObject:(AchmeaObject *)_object
{
if(kaart != nil)
{
[kaart removeFromSuperview];
kaart = nil;
}
kaart = [[MKMapView alloc] initWithFrame:CGRectMake(340, 380, 400,300)];
[kaart setDelegate: self];
[kaart setUserInteractionEnabled:YES];
CLLocationCoordinate2D coordinate;
coordinate.latitude = [_object.latitude doubleValue];
coordinate.longitude = [_object.longitude doubleValue];
double miles = 2;
double scalingFactor = ABS( cos(2 * M_PI * coordinate.latitude /360.0) );
MKCoordinateSpan span;
span.latitudeDelta = miles/69.0;
span.longitudeDelta = miles/(scalingFactor*69.0);
MKCoordinateRegion region;
region.span = span;
region.center = coordinate;
[kaart setRegion: region animated:YES];
ObjectAnnotation *sa = [[ObjectAnnotation alloc] initWithName: _object.plaats Address: _object.adres Coordinate:coordinate];
NSArray *anotations = [NSArray arrayWithObject: sa];
[kaart addAnnotations:anotations];
[self.view addSubview:kaart];
}
I have no idea why it happens, but when it first shows it takes a few seconds to respond to any user interaction, and after every interaction it needs at least a few more seconds, until after a few times freezing completely.
ObjectAnnotation.m
#import "ObjectAnnotation.h"
#implementation ObjectAnnotation
#synthesize coordinate = _coordinate;
- (id) initWithName: (NSString *) _name Address: (NSString *) _address Coordinate: (CLLocationCoordinate2D) _coord{
self = [super init];
name = [_name retain];
address = [_address retain];
_coordinate = _coord;
return self;
}
- (NSString *)title {
return name;
}
- (NSString *)subtitle {
return address;
}
- (void)dealloc
{
[name release];
name = nil;
[address release];
address = nil;
[super dealloc];
}
#end
I suspect this is related to re-adding new map views. Add a map view in the xib or very early in the view controller's life cycle and keep it hidden, and then show it and update its position (with setRegion:animated:). Creating a new map view each time is both expensive and unnecessary; if you're doing it because you want to get rid of some state (annotations, the map's region), learn how to reset that state in the existing map view instead.
It could also be related to your ObjectAnnotation, to which you do not provide the source code.
I suffered the same problem. After profiling, I find out tint property could be causing the map refresh delay. So I set default tint at storyboard and problem disappeared.

Resources