Struggling to find a way to make map zoom and center on annotation pin. Pin drops, but map loads ocean. Code is below.
- (void) connectionDidFinishLoading:(NSURLConnection *)connection
{
[self setString];
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseData options:0 error:nil];
NSDictionary *location = [dic objectForKey:@"location"];
NSDictionary *coordinate = [location objectForKey:@"coordinate"];
NSString *lat = [coordinate objectForKey:@"latitude"];
NSString *lon = [coordinate objectForKey:@"longitude"];
for (NSDictionary *diction in coordinate)
{
[array addObject:lat];
[array addObject:lon];
}
{
CLLocationCoordinate2D track;
track.latitude = [lat doubleValue];
track.longitude = [lon doubleValue];
MapViewAnnotation *newAnnotation = [[MapViewAnnotation alloc] initWithTitle:@"Title of Place Here" andCoordinate:track];
[self.mapView addAnnotation:newAnnotation];
}
}
2ND QUESTION, VERY RELATED:
After implementing the answer to the above question, I have since modified my code. Now, I have my coordinates coming to my MKMapView from the previous view, so that I don't have to bother making an API call twice, the second being IN the MKMapView. Currently in my ViewWillAppear I have the following, and AGAIN am experiencing a problem where the view will not center and zoom on the pin:
if ([self.stringToDisplay isEqualToString: @"Firehouse Gallery"])
{
UIImage *img = [UIImage imageNamed:@"firehouse.jpg"];
[imageView setImage:img];
CLLocationCoordinate2D track;
track.latitude = [lat doubleValue];
track.longitude = [lon doubleValue];
MKCoordinateRegion region;
MKCoordinateSpan span;
span.latitudeDelta = 0.01;
span.longitudeDelta = 0.01;
region.span = span;
region.center = track;
MapViewAnnotation *newAnnotation = [[MapViewAnnotation alloc] initWithTitle:@"Firehouse Gallery" andCoordinate:track];
[self.mapView addAnnotation:newAnnotation];
[self.mapView setRegion:region animated:TRUE];
[self.mapView regionThatFits:region];
}
Feedback is DEEPLY appreciated, as I can't tell what else I should do. The pin loads on the correct coordinates, just doesn't center/zoom...
Try this:
{
CLLocationCoordinate2D track;
track.latitude = [lat doubleValue];
track.longitude = [lon doubleValue];
MKCoordinateRegion region;
MKCoordinateSpan span;
span.latitudeDelta = 0.01;
span.longitudeDelta = 0.01;
region.span = span;
region.center = track;
MapViewAnnotation *newAnnotation = [[MapViewAnnotation alloc] initWithTitle:@"Title of Place Here" andCoordinate:track];
[self.mapView addAnnotation:newAnnotation];
[self.mapView setRegion:region animated:TRUE];
[self.mapView regionThatFits:region];
}