gpt4 book ai didi

iphone - iOS 用户位置不断恢复

转载 作者:行者123 更新时间:2023-12-01 19:17:29 32 4
gpt4 key购买 nike

我有一张 map ,当按下按钮时,它会显示用户的位置并放大。但是,一旦按下该按钮,您就无法在 map 上滚动,它会一直快速回到用户的位置。我需要添加/更改什么?谢谢。

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

最佳答案

getlocation您设置的方法showsUserLocationYES并开始使用 addObserver 观察用户位置的变化.

因此,每当用户位置发生变化(设备移动或获得更新位置时),observeValueForKeyPath方法触发。在该方法中,您将 map 的区域重置为以用户位置为中心。

在用户平移或缩放其他位置后,如果发生另一个用户位置更改,则该方法将再次触发并且 map 将返回到用户位置。

在您的情况下,最简单的解决方法是在您第一次缩放到用户位置后立即停止观察。在 observeValueForKeyPath方法,在 setRegion 之后行,添加:

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

请注意,从 iOS 4.0 开始,您不需要使用 KVO(所有 addObserver 的东西)来观察用户位置的变化。相反,您可以使用 didUpdateUserLocation 委托(delegate)方法。

从 iOS 5.0 开始,缩放到用户位置的另一个选项是设置 map View 的 userTrackingMode MKUserTrackingModeFollowMKUserTrackingModeFollowWithHeading .这样,您不必手动设置区域。 map 将缩放到用户并跟随用户,但仍让用户自由平移或缩放。

关于iphone - iOS 用户位置不断恢复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12206646/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com