2012-08-30 36 views
1

我有一個地圖,當按下按鈕時,它會顯示用戶的位置並放大。但是,一旦按下該按鈕,就不能在地圖上滾動,它會一直對齊用戶的位置。我需要添加/更改什麼?謝謝。iOS用戶位置保持對齊

// Set action for when the refreh button is pressed 

-(IBAction)refreshMapView:(id)sender { 

[self refreshMap]; 
} 

// Set action for when the show user location button is pressed 

-(IBAction)getlocation { 

mapView.showsUserLocation = YES; 
[self.mapView.userLocation addObserver:self forKeyPath:@"location" options:(NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld) context:nil]; 

} 

// Display segment button to change map views 

-(IBAction)changeSeg:(id)sender { 

if (segment.selectedSegmentIndex == 0) { 
    mapView.mapType = MKMapTypeStandard; 
} 
if (segment.selectedSegmentIndex == 1) { 
    mapView.mapType = MKMapTypeSatellite; 
} 
if (segment.selectedSegmentIndex == 2) { 
    mapView.mapType = MKMapTypeHybrid; 
} 
} 

// Listen to change in the userLocation 

-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context 
{ 

MKCoordinateRegion region; 
region.center = self.mapView.userLocation.coordinate; 

MKCoordinateSpan span; 
span.latitudeDelta = 0.2; // Change these values to change the zoom 
span.longitudeDelta = 0.2; 
region.span = span; 

[self.mapView setRegion:region animated:YES]; 
} 

// Load everying when map tab is accessed 

-(void)viewDidLoad { 

// Load mapView from MKMapKit 

[super viewDidLoad]; 

mapView = [[MKMapView alloc] initWithFrame:self.view.bounds]; 
[self.view insertSubview:mapView atIndex:0]; 

[mapView setMapType:MKMapTypeStandard]; 
[mapView setZoomEnabled:YES]; 
[mapView setScrollEnabled:YES]; 
[mapView setDelegate:self]; 

// Display WCCCA HQ annotation pin. 

MKCoordinateRegion WCCCA = { {0.0, 0.0} , {0.0, 0.0} }; 
WCCCA.center.latitude = 45.53540820864449; 
WCCCA.center.longitude = -122.86178648471832; 
WCCCA.span.longitudeDelta = 0.02f; 
WCCCA.span.latitudeDelta = 0.02f; 
[mapView setRegion:WCCCA animated:YES]; 

Annotation *ann1 = [[Annotation alloc] init]; 
ann1.title = @"WCCCA"; 
ann1.subtitle = @"Washington County Consolidated Communications Agency"; 
ann1.coordinate = WCCCA.center; 
[mapView addAnnotation: ann1]; 

// Display call annotations based on xml parser data. 

NSArray *callsArray = [xmlParser calls]; 

for (JointCAD *call in callsArray) { 
    NSString *callnumber = [call.callnumber stringByAppendingFormat:@". "]; 
    NSString *callandnumber = [callnumber stringByAppendingString:call.currentCallType]; 
    Annotation *ann = [[Annotation alloc] init]; 
    ann.title = callandnumber; 
    ann.subtitle = [call location]; 
    ann.coordinate = CLLocationCoordinate2DMake([call.latitude doubleValue], [call.longitude doubleValue]); 
    [mapView addAnnotation:ann]; } 

} 

// Annotation details. 

-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation { 

// Set user location annotation to blue point instead of red pin 

if ([annotation isKindOfClass:[MKUserLocation class]]) 
    return nil; 

MKPinAnnotationView *MyPin=[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"current"]; 
MyPin.pinColor = MKPinAnnotationColorRed; 

UIButton *advertButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; 
[advertButton addTarget:self action:@selector(button:) forControlEvents:UIControlEventTouchUpInside]; 

// MyPin.rightCalloutAccessoryView = advertButton; 
MyPin.draggable = NO; 
MyPin.highlighted = YES; 
MyPin.animatesDrop= TRUE; 
MyPin.canShowCallout = YES; 

return MyPin; 
} 

// Set default view when accessing Maps tab. 

- (void)viewWillAppear:(BOOL)animated { 

[super viewWillAppear:animated]; 

CLLocationCoordinate2D coord = {.latitude = 45.422424, .longitude = -122.76}; 
MKCoordinateSpan span = {.latitudeDelta = 0.60, .longitudeDelta = 0.60}; 
MKCoordinateRegion region = {coord, span}; 
[mapView setRegion:region]; 
} 

@end 

回答

3

getlocation方法設置showsUserLocationYES,並開始觀察更改使用addObserver用戶位置。

因此,無論何時用戶位置發生變化(無論是在設備移動還是獲取更新位置時),都會觸發該方法。在該方法中,您將重置地圖的區域以居中用戶位置。

用戶在其他位置平移或縮放後,如果發生另一個用戶位置更改,該方法將再次觸發,地圖將返回到用戶位置。

您的案例中最簡單的修復方法是在第一次縮放到用戶位置後停止觀察。在observeValueForKeyPath法,setRegion行後,添加:

[self.mapView.userLocation removeObserver:self forKeyPath:@"location"]; 


注意,因爲iOS的4.0,你不需要使用KVO(所有的addObserver東西)來觀察用戶位置的變化。相反,您可以使用didUpdateUserLocation委託方法。

從iOS 5.0開始,縮放到用戶位置的另一個選項是將地圖視圖的userTrackingMode設置爲MKUserTrackingModeFollowMKUserTrackingModeFollowWithHeading。這樣,您不必手動設置區域。地圖會縮放並跟隨用戶,但仍然讓用戶隨意平移或縮放。