Guest User

Untitled

a guest
Mar 18th, 2018
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.05 KB | None | 0 0
  1. let ann = MKPointAnnotation()
  2. self.ann.coordinate = annLoc
  3. self.ann.title = "Customize me"
  4. self.ann.subtitle = "???"
  5. self.mapView.addAnnotation(ann)
  6.  
  7. func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
  8. if annotation is MKUserLocation {
  9. return nil
  10. }
  11.  
  12. let identifier = "MyCustomAnnotation"
  13.  
  14. var annotationView = mapView.dequeueReusableAnnotationViewWithIdentifier(identifier)
  15. if annotationView == nil {
  16. annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: identifier)
  17. annotationView?.canShowCallout = true
  18. } else {
  19. annotationView!.annotation = annotation
  20. }
  21.  
  22. configureDetailView(annotationView!)
  23.  
  24. return annotationView
  25. }
  26.  
  27. func configureDetailView(annotationView: MKAnnotationView) {
  28. let width = 300
  29. let height = 200
  30.  
  31. let snapshotView = UIView()
  32. let views = ["snapshotView": snapshotView]
  33. snapshotView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:[snapshotView(300)]", options: [], metrics: nil, views: views))
  34. snapshotView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:[snapshotView(200)]", options: [], metrics: nil, views: views))
  35.  
  36. let options = MKMapSnapshotOptions()
  37. options.size = CGSize(width: width, height: height)
  38. options.mapType = .SatelliteFlyover
  39. options.camera = MKMapCamera(lookingAtCenterCoordinate: annotationView.annotation!.coordinate, fromDistance: 250, pitch: 65, heading: 0)
  40.  
  41. let snapshotter = MKMapSnapshotter(options: options)
  42. snapshotter.startWithCompletionHandler { snapshot, error in
  43. if snapshot != nil {
  44. let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: width, height: height))
  45. imageView.image = snapshot!.image
  46. snapshotView.addSubview(imageView)
  47. }
  48. }
  49.  
  50. annotationView.detailCalloutAccessoryView = snapshotView
  51. }
  52.  
  53. func mapView(mapView: MKMapView, didSelectAnnotationView view: MKAnnotationView) {
  54. let calloutView = ...
  55. calloutView.translatesAutoresizingMaskIntoConstraints = false
  56. calloutView.backgroundColor = UIColor.lightGrayColor()
  57. view.addSubview(calloutView)
  58.  
  59. NSLayoutConstraint.activateConstraints([
  60. calloutView.bottomAnchor.constraintEqualToAnchor(view.topAnchor, constant: 0),
  61. calloutView.widthAnchor.constraintEqualToConstant(60),
  62. calloutView.heightAnchor.constraintEqualToConstant(30),
  63. calloutView.centerXAnchor.constraintEqualToAnchor(view.centerXAnchor, constant: view.calloutOffset.x)
  64. ])
  65. }
  66.  
  67. @interface CustomeAnnotationView : MKAnnotationView
  68. @property (strong, nonatomic) UIButton *buttonCustomeCallOut;
  69. - (void)setSelected:(BOOL)selected animated:(BOOL)animated;
  70. @end
  71.  
  72. @implementation CustomeAnnotationView
  73.  
  74. -(id)initWithFrame:(CGRect)frame
  75. {
  76. self = [super initWithFrame:frame];
  77. if (self) {
  78. // Initialization code
  79. }
  80. return self;
  81. }
  82.  
  83.  
  84. - (void)setSelected:(BOOL)selected animated:(BOOL)animated{
  85.  
  86. [super setSelected:selected animated:animated];
  87.  
  88. if(selected)
  89. {
  90.  
  91.  
  92.  
  93. self.buttonCustomeCallOut = [UIButton buttonWithType:UIButtonTypeCustom];//iconShare//iconShareBlue
  94.  
  95. [self.buttonCustomeCallOut addTarget:self action:@selector(buttonHandlerCallOut:) forControlEvents:UIControlEventTouchDown];
  96. [self.buttonCustomeCallOut setBackgroundColor:[UIColor blueColor]];
  97.  
  98. [self.buttonCustomeCallOut setFrame:CGRectMake(-40,-80, 100, 100)];
  99.  
  100.  
  101.  
  102. [self addSubview:self.buttonCustomeCallOut];
  103.  
  104. [self.buttonCustomeCallOut setUserInteractionEnabled:YES];
  105. }
  106. else
  107. {
  108. //Remove your custom view...
  109. [self.buttonCustomeCallOut setUserInteractionEnabled:NO];
  110. [self.buttonCustomeCallOut removeFromSuperview];
  111.  
  112. self.buttonCustomeCallOut=nil;
  113. }
  114. }
  115. -(void)buttonHandlerCallOut:(UIButton*)sender{
  116. NSLog(@"Annotation Clicked");
  117. }
  118.  
  119. - (UIView*)hitTest:(CGPoint)point withEvent:(UIEvent*)event
  120. {
  121. UIView* v = [super hitTest:point withEvent:event];
  122. if (v != nil)
  123. {
  124. [self.superview bringSubviewToFront:self];
  125. }
  126. return v;
  127. }
  128.  
  129. - (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent*)event
  130. {
  131. CGRect rec = self.bounds;
  132. BOOL isIn = CGRectContainsPoint(rec, point);
  133. if(!isIn)
  134. {
  135. for (UIView *v in self.subviews)
  136. {
  137. isIn = CGRectContainsPoint(v.frame, point);
  138. if(isIn)
  139. break;
  140. }
  141. }
  142. return isIn;
  143. }
  144. @end
  145.  
  146. - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
  147. static NSString *identifier = @"CustAnnotation";
  148.  
  149. CustomeAnnotationView *annotationView = (CustomeAnnotationView *) [self.mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
  150. if (annotationView == nil) {
  151. annotationView = [[CustomeAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
  152. }
  153.  
  154. annotationView.enabled = YES;
  155. annotationView.canShowCallout = NO;
  156. annotationView.centerOffset = CGPointMake(0,-10);//-18
  157.  
  158. return annotationView;
  159. }
Add Comment
Please, Sign In to add comment