Ios 如何为MKMapKitView编码,当用户单击pin时,它会显示另一个容器视图?
我想要ios应用程序的这种类型的输出。我不知道怎么做。如果有人知道怎么做,请告诉我 编辑 我已经根据答案更新了我的代码,但仍然不起作用。我无法理解UIView的显示方式以及它的大小?Ios 如何为MKMapKitView编码,当用户单击pin时,它会显示另一个容器视图?,ios,objective-c,iphone,mkmapview,Ios,Objective C,Iphone,Mkmapview,我想要ios应用程序的这种类型的输出。我不知道怎么做。如果有人知道怎么做,请告诉我 编辑 我已经根据答案更新了我的代码,但仍然不起作用。我无法理解UIView的显示方式以及它的大小? #导入 @接口MyView:UIView @结束 #导入“MyView.h” @实现MyView -(id)initWithFrame:(CGRect)帧 { self=[super initWithFrame:frame]; 如果(自我) { //初始化代码 //初始化所有UIView组件 UILabel*la
#导入
@接口MyView:UIView
@结束
#导入“MyView.h”
@实现MyView
-(id)initWithFrame:(CGRect)帧
{
self=[super initWithFrame:frame];
如果(自我)
{
//初始化代码
//初始化所有UIView组件
UILabel*label1=[[UILabel alloc]initWithFrame:CGRectMake(20,30,200,44)];
label1.text=@“我是标签1”;
[self addSubview:label1];//将label1添加到自定义视图中
UILabel*label2=[[UILabel-alloc]initWithFrame:CGRectMake(20,80200,44)];
label2.text=@“我是标签2”;
[self addSubview:label2];//将label2添加到自定义视图中
}
回归自我;
}
=================================================
#进口
#导入“MyView.h”
@接口MyCustomView:MKAnnotationView
-(UIView*)hitTest:(CGPoint)point with event:(UIEvent*)event;
-(BOOL)pointInside:(CGPoint)pointwithevent:(UIEvent*)event;
-(void)设置showCustomCallout:(BOOL)showCustomCallout动画:(BOOL)动画;
-(无效)设置showCustomCallout:(BOOL)showCustomCallout
;
@属性(强,非原子)MyView*Callout视图;
@结束
#导入“MyCustomView.h”
@实现MyCustomView
-(无效)设置showCustomCallout:(BOOL)showCustomCallout
{
[自设置showCustomCallout:showCustomCallout动画:否];
}
-(无效)设置showCustomCallout:(BOOL)showCustomCallout动画:(BOOL)动画
{
//如果(showCustomCallout==showCustomCallout)返回;
showCustomCallout=showCustomCallout;
void(^animationBlock)(void)=零;
无效(^completionBlock)(BOOL finished)=无;
如果(showCustomCallout)
{
self.calloutView.alpha=0.0f;
动画块=^{
self.calloutView.alpha=1.0f;
[自添加子视图:自添加视图];
};
}否则{
animationBlock=^{self.calloutView.alpha=0.0f;};
completionBlock=^(BOOL finished){[self.calloutView removeFromSuperview];};
}
如果(动画){
[UIView animateWithDuration:0.2f动画:animationBlock完成:completionBlock];
}否则{
动画块();
完成区块(是);
}
}
-(UIView*)hitTest:(CGPoint)point with event:(UIEvent*)event
{
UIView*view=[super-hitTest:pointwithevent:event];
if([view iskindof类:\u callout view.class]){
return nil;//todo:向映射协议添加一个新的委托方法以处理调用点击
}否则{
返回视图;
}
}
-(BOOL)pointInside:(CGPoint)pointwithevent:(UIEvent*)event
{
CGRect rect=自边界;
BOOL-isInside=CGRectContainsPoint(rect,point);
如果(!isInside)
{
for(UIView*self.subview中的视图)
{
isInside=CGRectContainsPoint(view.frame,point);
如果(isInside)
打破
}
}
返回isInside;
}
=================================================
#导入“ViewController.h”
@界面视图控制器()
{
CLLocationManager*locationManager;
}
-(无效)viewDidLoad{
[超级视图下载];
//加载视图后,通常从nib执行任何其他设置。
locationManager=[[CLLocationManager alloc]init];
locationManager.delegate=self;
locationManager.distanceFilter=KCLDistanceFilterOne;//无论何时移动
locationManager.desiredAccuracy=KCallocationAccuracyBest;
[locationManager startUpdatingLocation];
[locationManager RequestWhenUseAuthorization];//添加此行
[locationManager startUpdatingLocation];
_mapView.showsUserLocation=是;
_mapView.delegate=self;
CLLocationCoordinate2D注释坐标;
注释坐标纬度=23.041261;
注释坐标经度=72.513892;
_mapView.region=MKCoordinateRegionMakeWithDistance(注释Coord,800800);
//MKCoordinateRegion adjustedRegion=[地图视图区域匹配:MKCoordinateRegion与距离匹配(注释Coord,800800)];
MKPointAnnotation*annotationPoint=[[MKPointAnnotation alloc]init];
annotationPoint.coordinate=annotationCoord;
annotationPoint.title=@“我在这里”;
annotationPoint.subtitle=@“微软总部”;
}
-(MKAnnotationView*)地图视图:(MKMapView*)地图视图1视图用于注释:(id)注释
{
MKPinAnnotationView*annView=[[MKPinAnnotationView alloc]initWithAnnotation:annotation重用标识符:@“pin”];
annView.pinColor=MKPinAnnotationColorGreen;
返回视图;
}
-(void)mapView:(MKMapView*)mapView注释视图:(MKAnnotationView*)视图
{
[((MyCustomView*)视图)设置ShowCustomCallout:否动画:是];
}
-(无效)地图视图:(MKMapView*)地图视图未选择注释视图:(MKAnnotationView*)视图
{
MyCustomView*注释视图=[[MyCustomView alloc]init];
[注释视图设置ShowCustomCallout:是动画:是];
}
要在mapview上创建自定义详图索引,您必须创建一个自定义视图。您必须创建一个包含自定义视图的类,该视图将位于图钉上。下面是github上的示例代码
请检查。您必须创建customview,或者您可以使用第三方控件。此处是尼斯教程:
#import <UIKit/UIKit.h>
@interface MyView : UIView
@end
#import "MyView.h"
@implementation MyView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
// Initialization code
// initilize all your UIView components
UILabel *label1 = [[UILabel alloc]initWithFrame:CGRectMake(20,30, 200, 44)];
label1.text = @"i am label 1";
[self addSubview:label1]; //add label1 to your custom view
UILabel *label2 = [[UILabel alloc]initWithFrame:CGRectMake(20,80, 200, 44)];
label2.text = @"i am label 2";
[self addSubview:label2]; //add label2 to your custom view
}
return self;
}
=================================================
#import <MapKit/MapKit.h>
#import "MyView.h"
@interface MyCustomView : MKAnnotationView
- (UIView*)hitTest:(CGPoint)point withEvent:(UIEvent*)event;
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent*)event;
- (void)setShowCustomCallout:(BOOL)showCustomCallout animated:(BOOL)animated;
- (void)setShowCustomCallout:(BOOL)showCustomCallout
;
@property(strong, nonatomic) MyView *calloutView ;
@end
#import "MyCustomView.h"
@implementation MyCustomView
- (void)setShowCustomCallout:(BOOL)showCustomCallout
{
[self setShowCustomCallout:showCustomCallout animated:NO];
}
- (void)setShowCustomCallout:(BOOL)showCustomCallout animated:(BOOL)animated
{
//if (showCustomCallout == showCustomCallout) return;
showCustomCallout = showCustomCallout;
void (^animationBlock)(void) = nil;
void (^completionBlock)(BOOL finished) = nil;
if (showCustomCallout)
{
self.calloutView.alpha = 0.0f;
animationBlock = ^{
self.calloutView.alpha = 1.0f;
[self addSubview:self.calloutView];
};
} else {
animationBlock = ^{ self.calloutView.alpha = 0.0f; };
completionBlock = ^(BOOL finished) { [self.calloutView removeFromSuperview]; };
}
if (animated) {
[UIView animateWithDuration:0.2f animations:animationBlock completion:completionBlock];
} else {
animationBlock();
completionBlock(YES);
}
}
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
UIView *view = [super hitTest:point withEvent:event];
if ([view isKindOfClass:_calloutView.class]) {
return nil; // todo: add a new delegate method to the map protocol to handle callout taps
} else {
return view;
}
}
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent*)event
{
CGRect rect = self.bounds;
BOOL isInside = CGRectContainsPoint(rect, point);
if(!isInside)
{
for (UIView *view in self.subviews)
{
isInside = CGRectContainsPoint(view.frame, point);
if(isInside)
break;
}
}
return isInside;
}
=================================================
#import "ViewController.h"
@interface ViewController ()
{
CLLocationManager *locationManager;
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.distanceFilter = kCLDistanceFilterNone; //whenever we move
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[locationManager startUpdatingLocation];
[locationManager requestWhenInUseAuthorization]; // Add This Line
[locationManager startUpdatingLocation];
_mapView.showsUserLocation = YES;
_mapView.delegate = self;
CLLocationCoordinate2D annotationCoord;
annotationCoord.latitude = 23.041261;
annotationCoord.longitude = 72.513892;
_mapView.region = MKCoordinateRegionMakeWithDistance(annotationCoord, 800, 800);
// MKCoordinateRegion adjustedRegion = [_mapView regionThatFits:MKCoordinateRegionMakeWithDistance(annotationCoord, 800, 800)];
MKPointAnnotation *annotationPoint = [[MKPointAnnotation alloc] init];
annotationPoint.coordinate = annotationCoord;
annotationPoint.title = @"I am here";
annotationPoint.subtitle = @"Microsoft's headquarters";
}
- (MKAnnotationView *)mapView:(MKMapView *)mapView1 viewForAnnotation:(id <MKAnnotation>)annotation
{
MKPinAnnotationView *annView=[[MKPinAnnotationView alloc]initWithAnnotation:annotation reuseIdentifier:@"pin"];
annView.pinColor = MKPinAnnotationColorGreen;
return annView;
}
- (void)mapView:(MKMapView *)mapView didDeselectAnnotationView:(MKAnnotationView *)view
{
[((MyCustomView *)view) setShowCustomCallout:NO animated:YES];
}
-(void)mapView:(MKMapView *)mapView didSelectAnnotationView:(MKAnnotationView *)view
{
MyCustomView *annotationView = [[MyCustomView alloc]init];
[annotationView setShowCustomCallout:YES animated:YES];
}