I have my mapView set to a default location every time the view is accessed. Sometimes when you access the view, it will be at the correct spot, although sometimes when you access the view, it will be at the mapView's default location, over the ocean south of Africa. How can I make sure it is in the right spot when the view is accessed.
Here is my code:
-(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];
}
This is what loads the annotations:
-(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;
}
There is only one point in the code you have above that set the maps the map location (
setRegion
). If your map is being set to a different location it isn't in the code you have provided.