I used this code for iPhone iOS 5.0 device. And everything was OK:
self.mapView = [[[MKMapView alloc]init] autorelease];
mapView.delegate = self;
mapView.frame = CGRectMake(0, 0, 320, 414);
[myView addSubview:mapView];
MKCoordinateRegion zoomRegion = MKCoordinateRegionForMapRect(MKMapRectWorld);
MKCoordinateRegion adjustedRegion = [mapView regionThatFits:zoomRegion];
[mapView setRegion:adjustedRegion animated:NO];
Then I tried it on iPhone 6.0, and it crashes at the string [mapView setRegion:adjustedRegion animated:NO];
*** Terminating app due to uncaught exception 'NSInvalidArgumentException',
reason: 'Invalid Region <center:+0.00000000, +0.00000000 span:+176.06470039, +360.00004292>'
What should I change in my code?
I think you have to set latitude span and longitude span before setRegion like this,,,,
MKCoordinateRegion zoomRegion = MKCoordinateRegionForMapRect(MKMapRectWorld);
MKCoordinateRegion adjustedRegion;
adjustedRegion.center = self.mapView.userLocation.coordinate;
adjustedRegion.span.latitudeDelta = 0.01;
adjustedRegion.span.longitudeDelta = 0.01;
adjustedRegion = [mapView regionThatFits:zoomRegion];
[mapView setRegion:adjustedRegion animated:NO];
EDIT
In your case [mapView regionThatFits:zoomRegion]; it create issues.. remove it :
MKCoordinateRegion zoomRegion = MKCoordinateRegionForMapRect(MKMapRectWorld);
// MKCoordinateRegion adjustedRegion = [self.mapView regionThatFits:zoomRegion];
[self.mapView setRegion:zoomRegion animated:NO];
Related
This takes me days..I could not make it work.
I write a method that places a pin on map as below:
- (void) SetMaps:(NSString *)Lats :(NSString *)lons;
{
if([upLo isEqualToString:#"Y"])
{
NSLog(#"setting maps:%#,%#",Lats,lons);
[mapView setMapType:MKMapTypeStandard];
[mapView setZoomEnabled:YES];
[mapView setScrollEnabled:YES];
[mapView setMapType:MKMapTypeStandard];
[mapView setZoomEnabled:YES];
[mapView setScrollEnabled:YES];
MKCoordinateRegion region = { {0.0, 0.0 }, { 0.0, 0.0 } };
region.center.latitude = [Lats doubleValue] ;
region.center.longitude = [lons doubleValue];
region.span.longitudeDelta = 0.01f;
region.span.latitudeDelta = 0.01f;
[mapView setRegion:region animated:YES];
[mapView setDelegate:self];
MKPointAnnotation *point = [[MKPointAnnotation alloc] init];
point.coordinate = region.center;
[self.mapView addAnnotation:point];
}
}
This method runs well if I call it in secondViewController.m in a tab bar controll application.
But, I want to call it from appdelegate.m.
So in appdelegate.m, I put
secondViewController *Gper=[[secondViewController alloc]init];
[Gper SetMaps:LAT:LON];
[Gper release];
from this NSLog(#"setting maps:%#,%#",Lats,lons); I can see the lat and lons values are correct into this method.
However, the map doesnt change to this location.
Whatelse should I do to make it showing new location?
Thanks for any help.
I feel you should learn (Objective-)C memory management and Cocoa conventions better before doing anything further. + alloc returns a new instance of your view controller class, and not the one that is currently displayed.
And begin your class names with a capital letter. And your method names with a lowercase one.
How to center the location's center in us map in map Kit view
I am sending my code here
mapView .scrollEnabled = YES;
appDelegate=(AppDelegate *)[[UIApplication sharedApplication]delegate];
dataStore = [[DataStore alloc] init];
utility = [[Utility alloc] init];
self = [super initWithFrame:CGRectMake(0, 28, mapView.frame.size.width, mapView.frame.size.height)];
//[self setBackgroundColor:[UIColor whiteColor]];
[self setMapView:mapView];
MKCoordinateRegion region;
region.center.latitude = 41.603854;
region.center.longitude = -92.930628;
region.span.longitudeDelta = 47.00;
region.span.latitudeDelta = 10.00;
You need to set the region once you set it up.
region.center.latitude = 41.603854;
region.center.longitude = -92.930628;
region.span.longitudeDelta = 47.00;
region.span.latitudeDelta = 10.00;
[mapView setRegion:region animated:YES];
I have a IPad application in which I add MKMapView programmatically without using the NIB.
{
CGRect r = CGRectMake(0, 0, 768, 980);
//CGRect r = CGRectMake(0, 0, 1004, 1004);
mapView = [[MKMapView alloc] initWithFrame:r];
mapView.showsUserLocation = FALSE;
mapView.mapType = MKMapTypeStandard;
//mapView.mapType = MKMapTypeHybrid;
//mapView.mapType = MKMapTypeSatellite;
mapView.delegate = self;
/*Region and Zoom*/
// one degree = 69 miles
// get the distance
// .01 = 1 mile square
//float distance = controller.itemDistance;
float distance = 1.0;
distance = distance/100.0;
MKCoordinateRegion region;
MKCoordinateSpan span;
span.latitudeDelta = distance;
span.longitudeDelta = distance;
CLLocationCoordinate2D location = mapView.userLocation.coordinate;
location.latitude = currLocation.coordinate.latitude;
location.longitude = currLocation.coordinate.longitude;
region.span = span;
region.center = location;
[mapView setRegion:region animated:TRUE];
[mapView regionThatFits:region];
[self addSubview:mapView];
[self addAnnotation];
}
The problem that I am facing is that the GOOGLE Logo Does not show up at the bottom of the MAP and also not showing the copyright notice. Due to this the application has been rejected from App Store. Can anyone help to add the logo on the MAP ?
Thanks in advance
may be your mapview is bigger than your view that you want to add in. Log frame height and width of view, and see if it is smaller than mapView.
Also consider to add [self setClipToBounds:NO]; and see mapView is flooding.
My mapview is not zooming to my user location. Please can you point out the error:
- (void)viewDidLoad {
[super viewDidLoad];
mapView = [[MKMapView alloc] init];
mapView.showsUserLocation = YES;
mapView.mapType = MKMapTypeHybrid;
mapView.delegate = self;
[self performSelector:#selector(startupZoom) withObject:nil afterDelay:1.25];
}
- (void)startupZoom {
MKCoordinateRegion region;
MKCoordinateSpan span;
span.latitudeDelta=0.2;
span.longitudeDelta=0.2;
CLLocationCoordinate2D location=mapView.userLocation.coordinate;
location.latitude=mapView.userLocation.coordinate.latitude;
location.longitude=mapView.userLocation.coordinate.longitude;
region.span=span;
region.center=location;
[mapView setRegion:region animated:TRUE];
[mapView regionThatFits:region];
NSLog(#"%f, %f", mapView.userLocation.coordinate.latitude, mapView.userLocation.coordinate.longitude);
}
The mapView is being created but its frame is not set and it's not being added as a subview of the view controller's view.
Change the alloc+init line to (change the dimensions as needed):
mapView = [[MKMapView alloc] initWithFrame:CGRectMake(0, 0, 320, 300)];
Then before the performSelector line, add this:
[self.view addSubview:mapView];
Note however that it's better to use the MKMapView's didUpdateUserLocation delegate method to zoom to the user's location instead of assuming the user location will be available after some arbitrary, fixed interval like 1.25 seconds.
For example, remove the performSelector line and implement didUpdateUserLocation:
- (void)mapView:(MKMapView *)mapView
didUpdateUserLocation:(MKUserLocation *)userLocation
{
[self startupZoom];
}
Only possible problem using the didUpdateUserLocation method is that it will be called every time the user's location changes so if you only want to zoom once on startup, you'll need to add a BOOL flag ivar and check/set it accordingly.
New to MapKit. Having problems centering map around a specified point. Here is the code. Not sure why this is not working. We are expecting to see a map centered around Cincinnati, OH. What we are seeing is the default google map of the world.
Any help appreciated.
/ Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
CLLocationCoordinate2D mapCoords[2];
mapCoords[0].latitude = 39.144057;
mapCoords[0].latitude = -84.505484;
mapCoords[1].latitude = 39.142984;
mapCoords[1].latitude = -84.502534;
MKCoordinateSpan span;
span.latitudeDelta = 0.2;
span.longitudeDelta = 0.2;
MKCoordinateRegion region;
region.center = mapCoords[0];
region.span = span;
[mapView setRegion:region animated:YES];
}
Change:
mapCoords[0].latitude = 39.144057;
mapCoords[0].latitude = -84.505484;
mapCoords[1].latitude = 39.142984;
mapCoords[1].latitude = -84.502534;
To:
mapCoords[0].latitude = 39.144057;
mapCoords[0].longitude = -84.505484;
mapCoords[1].latitude = 39.142984;
mapCoords[1].longitude = -84.502534;
I do this in my code and it works fine:
MKCoordinateRegion region;
MKCoordinateSpan span;
span.latitudeDelta=0.2;
span.longitudeDelta=0.2;
CLLocationCoordinate2D location;
location.latitude = 39.144057;
location.longitude = -84.505484;
region.span=span;
region.center=location;
[mv setRegion:region animated:TRUE];
[mv regionThatFits:region];
Want to improve this post? Provide detailed answers to this question, including citations and an explanation of why your answer is correct. Answers without enough detail may be edited or deleted.
The problem was a combination of the typos above and IB wiring problem.