每次访问视图时,我都会将 mapView 设置为默认位置。有时当您访问视图时,它会位于正确的位置,尽管有时当您访问视图时,它会位于 mapView 的默认位置,即非洲南部海洋上空。访问视图时如何确保它位于正确的位置。
这是我的代码:
-(void)viewDidLoad {
[super viewDidLoad];
self.mapView.delegate = self;
MKCoordinateRegion region = { {0.0, 0.0 }, { 0.0, 0.0 } };
region.center.latitude = 45.442424;
region.center.longitude = -122.78;
region.span.latitudeDelta = 0.60;
region.span.longitudeDelta = 0.60;
[mapView setRegion:region animated:YES];
}
这是加载注释的内容:
-(void)initXML {
NSURL *theURL = [NSURL URLWithString:@"http://www.wccca.com/PITS/"];
NSData *data = [[NSData alloc] initWithContentsOfURL:theURL];
xpathParser = [[TFHpple alloc] initWithHTMLData:data];
NSArray *elements = [xpathParser searchWithXPathQuery:@"//input[@id='hidXMLID']//@value"];
if (elements.count >= 1) {
TFHppleElement *element = [elements objectAtIndex:0];
TFHppleElement *child = [element.children objectAtIndex:0];
NSString *idValue = [child content];
NSString *idwithxml = [idValue stringByAppendingFormat:@".xml"];
NSString *url = @"http://www.wccca.com/PITS/xml/fire_data_";
NSString *finalurl = [url stringByAppendingString:idwithxml];
xmlParser = [[XMLParser alloc] loadXMLByURL:finalurl];
if (xmlParser.calls.count == 0) {
[self noCallsMessage];
}
else {
[self wcccaAnn];
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]; }
}
}
}
MKAnnotationView:
-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation {
if ([annotation isKindOfClass:[MKUserLocation class]])
return nil;
MKPinAnnotationView *MyPin=[[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"current"];
MyPin.pinColor = MKPinAnnotationColorRed;
MyPin.draggable = NO;
MyPin.highlighted = YES;
MyPin.animatesDrop= YES;
MyPin.canShowCallout = YES;
return MyPin;
}