xcode 尝试在 Pin (MKMapView) 上居中地图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14866812/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Trying to Center Map on Pin (MKMapView)
提问by Greg
Struggling to find a way to make map zoom and center on annotation pin. Pin drops, but map loads ocean. Code is below.
正在努力寻找一种方法来使地图缩放并以注释图钉为中心。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];
}
}
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:
在实现上述问题的答案后,我修改了我的代码。现在,我的坐标从前一个视图传到了我的 MKMapView,这样我就不必费心进行两次 API 调用,第二次是在 MKMapView 中。目前在我的 ViewWillAppear 中,我有以下内容,并且再次遇到视图无法居中和放大图钉的问题:
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...
非常感谢反馈,因为我不知道我还应该做什么。引脚加载到正确的坐标上,只是不居中/缩放...
回答by dthien
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];
}
回答by Ravi Karadbhajane
- (void)zoomMapViewToFitAnnotationsWithExtraZoomToAdjust:(double)extraZoom
{
if ([self.annotations count] == 0) return;
int i = 0;
MKMapPoint points[[self.annotations count]];
for (id<MKAnnotation> annotation in [self annotations])
{
points[i++] = MKMapPointForCoordinate(annotation.coordinate);
}
MKPolygon *poly = [MKPolygon polygonWithPoints:points count:i];
MKCoordinateRegion r = MKCoordinateRegionForMapRect([poly boundingMapRect]);
r.span.latitudeDelta += extraZoom;
r.span.longitudeDelta += extraZoom;
[self setRegion: r animated:YES];
}