Translate MapKit Region Button Action to Method - ios

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.

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!

MKMapView: annotation's callout not shown when setting region

I show several custom annotation views on an MKMapView, and when user taps one of them, I change the region to be centered on it:
- (void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view
{
if ([view.annotation isKindOfClass:[CustomAnnotation class]]) {
double miles = 2.5;
double scalingFactor = ABS(cos(2 * M_PI * lat / 360.0));
MKCoordinateSpan span;
span.latitudeDelta = miles/69.0;
span.longitudeDelta = miles/(scalingFactor * 69.0);
MKCoordinateRegion region;
region.span = span;
region.center = self.coordinates;
[self.mapView setRegion:region animated:YES];
}
}
I see that, when the annotation already was shown, more or less, in the center of the map, the callout is displayed, but it is not when I tap an annotation closer to the map's border and the region has to "move more". I tried to select the annotation after the region setting animation ends:
- (void)mapView:(MKMapView *)mapView regionDidChangeAnimated:(BOOL)animated
{
for (id annotation in self.mapView.annotations) {
if ([annotation isKindOfClass:[CustomAnnotation class]]) {
if (((CustomAnnotation *)annotation).id == self.selectedAnnotation.id) {
[self.mapView selectAnnotation:annotation animated:YES];
}
}
}
}
But the callout is neither shown. If I don't change programmatically the region when tapping, all callouts are shown. How could I solve this?
Thanks

How to give a location coordinate to plot a point in a map in ios using storyboard?

I am creating a map view in a view controller, using storyboard.
When I use the following code.
-(void) mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
CLLocationDistance distance = 1000;
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(userLocation.coordinate,
distance,
distance);
MKCoordinateRegion adjusted_region = [self.mapView regionThatFits:region];
[self.mapView setRegion:adjusted_region animated:YES];
}
A point is plotted in San Francisco, CA, United States. The
userLocation coordinates are the predefined value in MapKit.h framework.
Now I create a
-(void) mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
CLLocationDistance distance = 1000;
CLLocationCoordinate2D myCoordinate;
myCoordinate.latitude = 13.04016;
myCoordinate.longitude = 80.243044;
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(myCoordinate,
distance,
distance);
MKCoordinateRegion adjusted_region = [self.mapView regionThatFits:region];
[self.mapView setRegion:adjusted_region animated:YES];
}
Here, the region is displayed having the coordinate in the center. But, no point is plotted at the coordinate position.
How to plot a point or annotation in that coordinate location?
Try this code inside of didUpdateUserLocation method
MKPointAnnotation* annotation = [[MKPointAnnotation alloc] init];
CLLocationCoordinate2D myCoordinate;
myCoordinate.latitude=13.04016;
myCoordinate.longitude=80.243044;
annotation.coordinate = myCoordinate;
[self.mapView addAnnotation:annotation];
Add this code in didUpdateUserLocation
MKAnnotation *annotation = [[MKAnnotation alloc] initWithCoordinate:CLLocationCoordinate2DMake(latitude, longitude)];
[myMap addAnnotation:annotation];
Try This..
//MAP VIEW Point
MKCoordinateRegion myRegion;
//Center
CLLocationCoordinate2D center;
center.latitude=latitude;
center.longitude=longitude;
//Span
MKCoordinateSpan span;
span.latitudeDelta=THE_SPAN;
span.longitudeDelta=THE_SPAN;
myRegion.center=center;
myRegion.span=span;
//Set our mapView
[MapViewC setRegion:myRegion animated:YES];
//Annotation
//1.create coordinate for use with the annotation
CLLocationCoordinate2D wimbLocation;
wimbLocation.latitude=latitude;
wimbLocation.longitude=longitude;
Annotation * myAnnotation= [Annotation alloc];
myAnnotation.coordinate=wimbLocation;

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

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.

Resources