2013-01-03 51 views
0

我試圖顯示一個簡單地捕獲用戶經度和緯度座標的地圖,然後放大地圖上的用戶。我可以捕捉用戶的座標,但我無法顯示放大用戶位置的地圖,也不能將地圖放在用戶的中心。不幸的是,我只能查看從遠處顯示用戶的地圖。我的相關代碼如下:無法在iOS中的MapView中縮放用戶位置並集中於其中

我在.h文件中聲明瞭CLLocationCoordinate2D類型的userLoc。然後我在我的.m文件如下:

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation 
{ 

    NSLog(@"didUpdateToLocation: %@", newLocation); 
    CLLocation *currentLocation = newLocation; 
    userLoc = newLocation.coordinate; 
    [locationManager stopUpdatingLocation]; 
    [self showCurrentLocation]; 



    if (currentLocation != nil) { 
     userLongitude = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.longitude]; 
     userLatitude = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.latitude]; 
    } 


    NSLog(@"User Latitude is %@", userLatitude); 
    NSLog(@"User Longitude is %@", userLongitude); 
} 

然後我有一個顯示實際的地圖下面的方法:

-(void) showCurrentLocation { 

    mapView = [[MKMapView alloc] initWithFrame:CGRectMake(0, 55, 320, 425)]; 
    MKCoordinateRegion region = mapView.region; 
    MKCoordinateSpan span; 

    region.center = userLoc; 
    span.latitudeDelta = 0.02; 
    span.longitudeDelta = 0.02; 
    region.span=span; 

    [mapView setMapType:MKMapTypeStandard]; 
    [mapView setZoomEnabled:YES]; 
    [mapView setScrollEnabled:YES]; 
    [mapView setShowsUserLocation:YES]; 

    [self.view addSubview:mapView]; 


} 

正如我所說的,我可以查看地圖,顯示用戶的位置,但地圖既不會放大,也不會將地圖對準用戶。任何人都能看到爲什麼

回答

0
-(void) showCurrentLocation { 

    mapView = [[MKMapView alloc] initWithFrame:CGRectMake(0, 55, 320, 425)]; 
    MKCoordinateRegion region = mapView.region; 
    MKCoordinateSpan span; 

    region.center = userLoc; 
    span.latitudeDelta = 0.02; 
    span.longitudeDelta = 0.02; 
    region.span=span; 

    [mapView setMapType:MKMapTypeStandard]; 
    [mapView setZoomEnabled:YES]; 
    [mapView setScrollEnabled:YES]; 
    [mapView setShowsUserLocation:YES]; 
    [mapView setRegion:region animated:YES]; 
    [self.view addSubview:mapView]; 


} 
+0

非常感謝您的解決方案。我沒有意識到我那麼近:-) – syedfa

+0

沒問題!有時候最渺茫的東西很難看清,而另一雙眼睛則有幫助 –

相關問題