How to center a UIMapView in the middle of a MKOverlay - iphone

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...

Related

MapKit Polyline custom zoom?

I am trying to learn how to use a polyline to connect two points on a map in ios6. First off, I have read over every tutorial on this subject that a simple Google search turns up and can not get polylines to work for one reason. Every tutorial that I have seen always adds the polyline to the map and adjusts the zoom of the map to fit the whole line. How would I go about making and adding a polyline to a map in ios6 if I want the map to stay zoomed in at a constant distance and only show the end of the polyline if it is larger then the current view?
For example say I had a polyline that was a mile long and wanted the map to stay zoomed in at a constand distacne equivelent to:
MKCoordinateRegion userRegion = MKCoordinateRegionMakeWithDistance(self.currentLocation.coordinate, 1000, 1000);
[self.mainMap setRegion:[self.mainMap regionThatFits:userRegion] animated:YES];
How would I go about doing this? Please provide full code examples or a sample project that I could download!
MKMapPoint * malloc / assign:
MKMapPoint *newPoints = malloc((sizeof (MKMapPoint) * nbPoints));
newPoints[index] = varMKMapPoint;
free(newPoints);
MKPolyline must be initialize in your needed :
MKPolyline *polyline = [MKPolyline polylineWithPoints:newPoints count:nbPoints];
[self.mapView addOverlay:polyline];
to display your MKPolyline, you have to use viewForOverlay :
- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay
{
MKOverlayView* overlayView = [[MKOverlayView alloc] initWithOverlay:overlay];
if([overlay isKindOfClass:[MKPolyline class]]) {
MKPolylineView *backgroundView = [[MKPolylineView alloc] initWithPolyline:overlay];
backgroundView.fillColor = [UIColor blackColor];
backgroundView.strokeColor = backgroundView.fillColor;
backgroundView.lineWidth = 10;
backgroundView.lineCap = kCGLineCapSquare;
overlayView = backgroundView;
}
return overlayView;
}
To use this method, you have to convert your points to CLLocation, it will returns a MKCoordinateRegion that you will set to mapView :
- (MKCoordinateRegion)getCenterRegionFromPoints:(NSArray *)points
{
CLLocationCoordinate2D topLeftCoordinate;
topLeftCoordinate.latitude = -90;
topLeftCoordinate.longitude = 180;
CLLocationCoordinate2D bottomRightCoordinate;
bottomRightCoordinate.latitude = 90;
bottomRightCoordinate.longitude = -180;
for (CLLocation *location in points) {
topLeftCoordinate.longitude = fmin(topLeftCoordinate.longitude, location.coordinate.longitude);
topLeftCoordinate.latitude = fmax(topLeftCoordinate.latitude, location.coordinate.latitude);
bottomRightCoordinate.longitude = fmax(bottomRightCoordinate.longitude, location.coordinate.longitude);
bottomRightCoordinate.latitude = fmin(bottomRightCoordinate.latitude, location.coordinate.latitude);
}
MKCoordinateRegion region;
region.center.latitude = topLeftCoordinate.latitude - (topLeftCoordinate.latitude - bottomRightCoordinate.latitude) * 0.5;
region.center.longitude = topLeftCoordinate.longitude + (bottomRightCoordinate.longitude - topLeftCoordinate.longitude) * 0.5;
region.span.latitudeDelta = fabs(topLeftCoordinate.latitude - bottomRightCoordinate.latitude) * 1.2; //2
region.span.longitudeDelta = fabs(bottomRightCoordinate.longitude - topLeftCoordinate.longitude) * 1.2; //2
// NSLog(#"zoom lvl : %f, %f", region.span.latitudeDelta, region.span.latitudeDelta);
return region;
}
Hope this helps.

How to center map view while tracking user

I am just wondering how to center the map view as the user is vein tracked with CLLocationmanager and map kit
This is currently how I am tracking the user and updating the location etc.
- (void)viewDidLoad
{
// Initialize the TileOverlay with tiles in the application's bundle's resource directory.
// Any valid tiled image directory structure in there will do.
NSString *tileDirectory = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:#"Tiles"];
TileOverlay *overlay = [[TileOverlay alloc] initWithTileDirectory:tileDirectory];
[map addOverlay:overlay];
// zoom in by a factor of two from the rect that contains the bounds
// because MapKit always backs up to get to an integral zoom level so
// we need to go in one so that we don't end up backed out beyond the
// range of the TileOverlay.
MKMapRect visibleRect = [map mapRectThatFits:overlay.boundingMapRect];
visibleRect.size.width /= 2;
visibleRect.size.height /= 2;
visibleRect.origin.x += visibleRect.size.width / 2;
visibleRect.origin.y += visibleRect.size.height / 2;
map.visibleMapRect = visibleRect;
// map.showsUserLocation = YES;
//location tracking
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[locationManager startUpdatingLocation];
//Show the users location.. hopefully it works with tracking.
map.showsUserLocation = YES;
[overlay release]; // map is now keeping track of overlay
}
//OverLays Topographical map
- (MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id <MKOverlay>)overlay
{
TileOverlayView *view = [[TileOverlayView alloc] initWithOverlay:overlay];
view.tileAlpha = 0.6;
return [view autorelease];
}
//Tracks Users location and Prints out the Lat and Lon
-(void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{
CLLocationCoordinate2D here = newLocation.coordinate;
NSLog(#"%f %f ", here.latitude, here.longitude);
}
The below method focus a particular location in the map,
//Don't forget to add this method to your header also
-(void)focusLocationInMap:(CLLocationCoordinate2D)location
{
MKCoordinateRegion region;
MKCoordinateSpan span;
span.latitudeDelta=0.01;
span.longitudeDelta=0.01;
region.span=span;
region.center=location;
[self.yourMapView setRegion:region animated:TRUE];
[self.yourMapView regionThatFits:region];
}
You could use it anywhere by passing coordinates to it,
CLLocationCoordinate2D here = newLocation.coordinate;
[self focusLocationInMap:here];
here.coordinate is not correct, should be just 'here'

How to center the US location in iphone

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

Google logo does not show up in Google Map (IPad Application)

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.

iPhone SDK: How to center map around a particular point?

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.