苦苦寻找一种方法来使地图缩放并以注释引脚为中心. Pin下降,但地图加载海洋.代码如下.
- (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]; } }
第2个问题,非常相关:
在实现上述问题的答案后,我已经修改了我的代码.现在,我的坐标从前一个视图到我的MKMapView,所以我不必费心去做两次API调用,第二个是在MKMapView中.目前在我的ViewWillAppear中,我有以下内容,并且AGAIN遇到了一个问题,即视图不会居中并放大图钉:
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]; }
反馈非常感谢,因为我不知道还应该做些什么.引脚加载到正确的坐标上,只是没有居中/缩放…
解决方法
试试这个:
{ 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]; }