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];
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.
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];
Im Having MapView In which I added some 5 Annotations in the Areas of Chennai.. I want to Zoom Out the Chennai as soon as the MapView Begins Loading so that Annotations will be Visible Clearly.
In viewDidLoad:
CLLocationCoordinate2D location6;
location6.latitude=12.9758;
location6.longitude=80.2205;
MapAnnotation *ann6=[[MapAnnotation alloc]initWithTitle:#"Chennai-Velacherry" andCoordinate:location6];
[mapView addAnnotation:ann6];
CLLocationCoordinate2D centerlocation;
centerlocation.longitude=13.0810;
centerlocation.longitude=80.2740;
[mapView setCenterCoordinate:centerlocation animated:NO];
[self.view addSubview:mapView];
use this,
MKCoordinateRegion region = { {0.0, 0.0 }, { 0.0, 0.0 } };
region.center.latitude = [currentPlace.latitude doubleValue];
region.center.longitude = [currentPlace.longitude doubleValue];
region.span.longitudeDelta = 0.03f;
region.span.latitudeDelta = 0.03f;
[mapView setRegion:region animated:YES];
[mapView setDelegate:self];
mapView.zoomEnabled = YES;
mapView.scrollEnabled = NO;
these will vary your zoom level,
region.span.longitudeDelta = 0.03f;
region.span.latitudeDelta = 0.03f;
You cannot have a predefined set of zoom levels. Instead, you can set the visible area of a MKMapView using MKCoordinateRegion. Sample code available here.
You can do that like this:
Create a function like :
-(void)zoomToFitMapAnnotations:(MKMapView*)mv {
CLLocationCoordinate2D topLeftCoord;
topLeftCoord.latitude = -90;
topLeftCoord.longitude = 180;
CLLocationCoordinate2D bottomRightCoord;
bottomRightCoord.latitude = 90;
bottomRightCoord.longitude = -180;
for(MKAnnotations* annotation in mv.annotations)
{
topLeftCoord.longitude = fmin(topLeftCoord.longitude, annotation.coordinate.longitude);
topLeftCoord.latitude = fmax(topLeftCoord.latitude, annotation.coordinate.latitude);
bottomRightCoord.longitude = fmax(bottomRightCoord.longitude, annotation.coordinate.longitude);
bottomRightCoord.latitude = fmin(bottomRightCoord.latitude, annotation.coordinate.latitude);
}
MKCoordinateRegion region;
region.center.latitude = topLeftCoord.latitude - (topLeftCoord.latitude - bottomRightCoord.latitude) * 0.5;
region.center.longitude = topLeftCoord.longitude + (bottomRightCoord.longitude - topLeftCoord.longitude) * 0.5;
region.span.latitudeDelta = fabs(topLeftCoord.latitude - bottomRightCoord.latitude) * 1.1; // Add a little extra space on the sides
region.span.longitudeDelta = fabs(bottomRightCoord.longitude - topLeftCoord.longitude) * 1.1; // Add a little extra space on the sides
region = [mv regionThatFits:region];
[mv setRegion:region animated:YES];
}
And call this function in viewDidLoad after all the pins are plotted
Try this
#define kMAPSPAN 1600
MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(zoomLocation, 1*kMAPSPAN, 1*kMAPSPAN);
MKCoordinateRegion adjustedRegion = [mapView regionThatFits:viewRegion];
[_mapView setRegion:adjustedRegion animated:YES];
mapView is your MKMapView object and kMAPSPAN can be set according to your need and zoomLocation is your coordinate2D location for chennai
I am using the following code (based on the Apple WWDC 2010 TileMap example) to load an UIMapView with a MKOverlay image (this could is in the viewDidLoad method):
NSString *tileDirectory = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:#"Tiles"];
TileOverlay *overlay = [[TileOverlay alloc] initWithTileDirectory:tileDirectory];
[map addOverlay:overlay];
Next I was to center the UIMapView map in the center of this overlay. At the moment I am doing this my hard-coding the latitude and longitude as follows:
double myLatitude = ...;
double myLongitude = ...;
CLLocation *location = [[CLLocation alloc] initWithLatitude:myLatitude longitude:myLongitude];
MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(location.coordinate, 4000, 4000);
MKCoordinateRegion adjustedRegion = [map regionThatFits:viewRegion];
[map setRegion:adjustedRegion animated:YES];
This works fine. I have tried to calculate the center of the overlay as follows:
double x, y;
x = MKMapRectGetMidX([overlay boundingMapRect]);
y = MKMapRectGetMidY([overlay boundingMapRect]);
CLLocationCoordinate2D coord = MKCoordinateForMapPoint(MKMapPointMake(x, y));
MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(coord, 4000, 4000);
MKCoordinateRegion adjustedRegion = [map regionThatFits:viewRegion];
[map setRegion:adjustedRegion animated:YES];
But this does not work. The MKCoordinateForMapPoint returns a latitude/longitude outside the overlay. I don't understand why.
Can anyone suggest a solution?
Thanks in advance...
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.