我需要放大一个通过NSNotification传递的MKMapItem,并按如下方式处理:
-(void)zoomToAnnotation:(NSNotification *)notification {
// Zoom into the passed MKMapItem
MKMapItem *passedMapItem = notification.object;
// self.previewMapView is the what I called my mapView
}
更新
-(void)zoomToAnnotation:(NSNotification *)notification {
// Zoom into the passed MKMapItem
NSLog(@"beginCam");
MKMapItem *passedMapItem = notification.object;
MKMapCamera *mapCamera = [[MKMapCamera alloc] init];
mapCamera.centerCoordinate = [[passedMapItem placemark] coordinate];
[self.previewMapView setCamera:mapCamera animated:YES];
NSLog(@"endCam");
}
更新2:
如何使用MKMapView放大单个MKMapItem?
谢谢!
答案 0 :(得分:0)
(从聊天消息中复制,稍微快点n'脏):
所以,我们有点偏僻。虽然我通过以下方式开展工作:
-(void)zoomToAnnotation:(NSNotification *)notification {
MKMapItem *passedMapItem = (MKMapItem *)notification.object;
CLLocationCoordinate2D coordinateOfInterest = passedMapItem.placemark.coordinate;
MKCoordinateRegion region = MKCoordinateRegionMake(coordinateOfInterest, MKCoordinateSpanMake(.1, .1));
[self.previewMapView setRegion:region
animated:YES];
[self.previewMapView addAnnotation:passedMapItem.placemark];
}
//in my implementation, I just have a random button call this
-(void) makeNotification{
CLLocationCoordinate2D coordinateOfInterest = self.locationManager.location.coordinate;
MKPlacemark * placeMark = [[MKPlacemark alloc] initWithCoordinate:coordinateOfInterest
addressDictionary:nil];
MKMapItem * mapItem = [[MKMapItem alloc] initWithPlacemark:placeMark];
[[NSNotificationCenter defaultCenter] postNotificationName:@"mapPoint"
object:mapItem ];
}
所以我猜最大的收获是MKCamera对象并不打算用来改变地图视图的视图,至少在我们尝试的方式。最终,在这种情况下,调用setRegion:animated:
最终会放大某个区域。我也实现了setCenterCoordinate:animated:
,但我看了它,因为它似乎没有影响我们希望实现的最终结果。
此外,位置数据需要通过CLLocationManager
提供,在iOS中需要两个(轻微记录的)操作:
NSLocationWhenInUseUsageDescription
和NSLocationAlwaysUsageDescription
以及字符串说明。 (见this link for further info)requestAlwaysAuthorization
和requestWhenInUseAuthorization
无论如何,我发现谷歌地图API在这方面更容易使用,所以我可能会回去使用它。但是很好地解决这个问题。
祝其他人好运!