如何在跟踪用户时居中地图视图

时间:2012-01-17 07:13:16

标签: iphone ios mapkit cllocationmanager

我只是想知道如何使用CLLocationmanager和地图套件对用户进行静脉跟踪来居中地图视图

目前我正在跟踪用户并更新位置等。

- (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);
}

2 个答案:

答案 0 :(得分:4)

以下方法将特定位置聚焦在地图中,

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

您可以通过将坐标传递给它来在任何地方使用它,

CLLocationCoordinate2D here =  newLocation.coordinate;
[self focusLocationInMap:here];

答案 1 :(得分:1)

here.coordinate不正确,应该只是'这里'