2012-01-17 26 views
0

我只是想知道如何將地圖視圖作爲用戶靜脈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); 
} 

回答

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

我唯一的問題是與你的示例代碼它給我和錯誤與.coordinate說最後一行的「任何成員命名座標CLLocationCoordinate2d「 – 2012-01-17 07:53:51

+2

糟糕,我的錯誤。 '這裏'本身就是一個座標,它會直接工作。 @ C.Johns感謝編輯代碼。 – sElanthiraiyan 2012-01-17 09:37:56

+0

所有的好,有時人們不回來檢查評論等等,所以我認爲我會這樣做,因爲有人使用這個問題來幫助他們可能有任何問題..再次感謝您的幫助。 – 2012-01-17 19:31:32

1

here.coordinate是不正確的,應該只是「這裏」

+0

歡呼聲這麼想,只要確保它不長或拉特或其他可以在這裏使用的東西。 – 2012-01-17 08:04:00