【问题标题】:How to center map view while tracking user如何在跟踪用户时将地图视图居中
【发布时间】:2012-01-17 07:13:16
【问题描述】:

我只是想知道如何在使用 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);
}

【问题讨论】:

    标签: iphone ios mapkit cllocationmanager


    【解决方案1】:

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

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

    【讨论】:

    • 我唯一的问题是你的示例代码的最后一行给了我和错误 .coordinate 说“在 CLLocationCoordinate2d 中没有名为坐标的成员”
    • 糟糕,我的错误。 'here' 本身就是一个坐标,它将直接起作用。 @C.Johns 感谢您编辑代码。
    • 一切都好,有时人们不会回来检查 cmets 等,所以我想我会这样做,以防有人使用这个问题来帮助解决他们可能遇到的任何问题。再次感谢您的帮助.
    【解决方案2】:

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

    【讨论】:

    • 干杯认为所以只要确保它不是长或纬度或其他你可以在这里使用的东西。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-06-27
    • 1970-01-01
    • 1970-01-01
    • 2011-06-01
    • 2021-12-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多