Objective c MKMapView缩放用户位置和注释
我希望我的MKMapView将“用户位置”和注释缩放到尽可能近的位置。 地图上已经显示了这两种情况,但已缩小到全国范围。 我该怎么做 我不知道为什么注释和Pin没有设置动画 我的代码:Objective c MKMapView缩放用户位置和注释,objective-c,xcode,Objective C,Xcode,我希望我的MKMapView将“用户位置”和注释缩放到尽可能近的位置。 地图上已经显示了这两种情况,但已缩小到全国范围。 我该怎么做 我不知道为什么注释和Pin没有设置动画 我的代码: //Coords User CLLocationCoordinate2D user_location; user_location.latitude = mapView.userLocation.location.coordinate.latitude; user_location.longitude = map
//Coords User
CLLocationCoordinate2D user_location;
user_location.latitude = mapView.userLocation.location.coordinate.latitude;
user_location.longitude = mapView.userLocation.location.coordinate.longitude;
//Coords Annotation
CLLocationCoordinate2D club_location;
club_location.latitude = [self.clubInfo.latitude doubleValue];
club_location.longitude = [self.clubInfo.longitude doubleValue];
MKMapPoint userPoint = MKMapPointForCoordinate(user_location);
MKMapPoint annotationPoint = MKMapPointForCoordinate(club_location);
MKMapRect userRect = MKMapRectMake(userPoint.x, userPoint.y, 0, 0);
MKMapRect annotationRect = MKMapRectMake(annotationPoint.x, annotationPoint.y, 0, 0);
MKMapRect unionRect = MKMapRectUnion(userRect, annotationRect);
MKMapRect unionRectThatFits = [mapView mapRectThatFits:unionRect];
[mapView setVisibleMapRect:unionRectThatFits animated:YES];
// Add an annotation
MKPointAnnotation *annotation = [[MKPointAnnotation alloc] init];
annotation.coordinate = club_location;
annotation.title = self.clubInfo.clubName;
annotation.subtitle = @"Pin!";
[mapView addAnnotation:annotation]; // This was missing
[mapView selectAnnotation:annotation animated:YES];
日志:
使用这4个类-它就像一个魅力
//MapViewController.h file
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
@interface MapViewController : UIViewController <CLLocationManagerDelegate> {
IBOutlet UIButton *buttonBack;
IBOutlet MKMapView *mapView;
IBOutlet UILabel *labelTitle;
NSString *lon,*lat,*nickName;
BOOL isFirstLaunch;
}
@property(nonatomic,retain) NSString *lon,*lat,*nickName;
@property(nonatomic,retain) IBOutlet UIButton *buttonBack;
@property(nonatomic,retain) IBOutlet UILabel *labelTitle;
@property(nonatomic,retain) IBOutlet MKMapView *mapView;
-(IBAction)buttonBackAction:(UIButton *)_btn;
-(void)zoomToFitMapAnnotations;
@end
//anotation类-Annotation.h
#import <MapKit/MapKit.h>
@interface Annotation : NSObject <MKAnnotation>{
CLLocationCoordinate2D theCoordinate;
NSString *theTitle;
NSString *restId;
NSString *theSubtitle;
}
@property(nonatomic,retain) NSString *restId;
@property(nonatomic,retain) NSString *theTitle;
@property(nonatomic,retain) NSString *theSubtitle;
@property CLLocationCoordinate2D theCoordinate;
@end
如果我理解得很好,您希望这两个注释能够以最大可能的缩放显示。我发现这个解决方案不需要任何计算
// You have coordinates
CLLocationCoordinate2D user = ...;
CLLocationCoordinate2D annotation = ...;
// Make map points
MKMapPoint userPoint = MKMapPointForCoordinate(user);
MKMapPoint annotationPoint = MKMapPointForCoordinate(annotation);
// Make map rects with 0 size
MKMapRect userRect = MKMapRectMake(userPoint.x, userPoint.y, 0, 0);
MKMapRect annotationRect = MKMapRectMake(annotationPoint.x, annotationPoint.y, 0, 0);
// Make union of those two rects
MKMapRect unionRect = MKMapRectUnion(userRect, annotationRect);
// You have the smallest possible rect containing both locations
MKMapRect unionRectThatFits = [mapView mapRectThatFits:unionRect];
[mapView setVisibleMapRect:unionRectThatFits animated:YES];
CoreLocation和MapKit结构是地狱。对于稍后来到这里的任何人来说:实现这一点的最短方法是(iOS7+): 假设您有@property
MKMapView*mapView
,以及MKPointAnnotation*theAnnotation
,请调用:
[self.mapView showAnnotations:@[theAnnotation, self.mapView.userLocation] animated:NO];
现在,如果调用得太早(即,self.mapView.userLocation
尚未设置),可能会导致一些问题。如果是这样的话,您可以调用视图将出现:
[self.mapView.userLocation addObserver:self forKeyPath:@"location" options:0 context:NULL];
然后,实施:
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
if ([keyPath isEqualToString:@"location"]) {
[self.mapView showAnnotations:@[theAnnotation, self.mapView.userLocation] animated:NO];
}
}
这样你就可以确定一切都安排好了。不要忘记在ViewWillEnglish删除观察者:
[self.mapView.userLocation removeObserver:self forKeyPath:@"location"];
你好,iMartin我不知道你为什么否决了它,但我用它在iTunes中为我的pickanddate应用程序编写代码,以显示两个用户的位置,最大可能的缩放效果,而且效果很好!还要检查其中的ZoomTofitPannotations方法!所以,请不要否决投票,直到你检查和测试它!我不是说它不起作用,但这不是一个很好的答案。一个人必须通读所有的代码并找出有用的信息。在我看来,即使你也不知道哪一部分是相关的。一旦你通过删除不相关的代码来改进你的答案,我将向上投票;)如何获取用户坐标?像这样mapView.userLocation.location.Coordinate
我尝试过,但没有成功。请查看我的第一篇帖子,我更新了它。你能,请,记录所有的MKMapRect
值吗?使用MKStringFromMapRect
。还要记录finalmapView.visibleMapRect
。嗯,rect值是正确的。但您需要在选择注释之前添加注释。这就是为什么你什么都没看到。调用-addAnnotation:
,然后选择-selectAnnotation:animated:
。将图像替换为新图像,这样我们可以看到pin的位置。@iMartin image updated您的用户位置与俱乐部位置完全不同。我认为您在用户位置不可用时调用此方法。它指向世界的中心。尝试将pin添加到用户位置,您将看到差异。
[self.mapView.userLocation addObserver:self forKeyPath:@"location" options:0 context:NULL];
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
if ([keyPath isEqualToString:@"location"]) {
[self.mapView showAnnotations:@[theAnnotation, self.mapView.userLocation] animated:NO];
}
}
[self.mapView.userLocation removeObserver:self forKeyPath:@"location"];