xcode – 尝试在Pin上居中地图(MKMapView)

前端之家收集整理的这篇文章主要介绍了xcode – 尝试在Pin上居中地图(MKMapView)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
苦苦寻找一种方法来使地图缩放并以注释引脚为中心. 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];
}

猜你在找的iOS相关文章