0

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);
}
C.Johns
  • 10,185
  • 20
  • 102
  • 156

2 Answers2

4

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];
C.Johns
  • 10,185
  • 20
  • 102
  • 156
sElanthiraiyan
  • 6,000
  • 1
  • 31
  • 38
  • The only issue I have is with the last line of your example code its giving me and error with .coordinate saying "No member named coordinate in CLLocationCoordinate2d" – C.Johns Jan 17 '12 at 07:53
  • 2
    Oops, my mistake. 'here' itself is a coordinate and it will work directly. @C.Johns thanks for editing the code. – sElanthiraiyan Jan 17 '12 at 09:37
  • all good, sometimes people don't come back to check up on comments etc so thought i would do it incase someone uses this question to help with any problems they might have.. Again thanks for the help. – C.Johns Jan 17 '12 at 19:31
1

here.coordinate is not correct, should be just 'here'

henghonglee
  • 1,732
  • 3
  • 20
  • 29
  • cheers thought so just making sure it wasn't long or lat or something else that you can use with here. – C.Johns Jan 17 '12 at 08:04