Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- let ann = MKPointAnnotation()
- self.ann.coordinate = annLoc
- self.ann.title = "Customize me"
- self.ann.subtitle = "???"
- self.mapView.addAnnotation(ann)
- func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
- if annotation is MKUserLocation {
- return nil
- }
- let identifier = "MyCustomAnnotation"
- var annotationView = mapView.dequeueReusableAnnotationViewWithIdentifier(identifier)
- if annotationView == nil {
- annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: identifier)
- annotationView?.canShowCallout = true
- } else {
- annotationView!.annotation = annotation
- }
- configureDetailView(annotationView!)
- return annotationView
- }
- func configureDetailView(annotationView: MKAnnotationView) {
- let width = 300
- let height = 200
- let snapshotView = UIView()
- let views = ["snapshotView": snapshotView]
- snapshotView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:[snapshotView(300)]", options: [], metrics: nil, views: views))
- snapshotView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:[snapshotView(200)]", options: [], metrics: nil, views: views))
- let options = MKMapSnapshotOptions()
- options.size = CGSize(width: width, height: height)
- options.mapType = .SatelliteFlyover
- options.camera = MKMapCamera(lookingAtCenterCoordinate: annotationView.annotation!.coordinate, fromDistance: 250, pitch: 65, heading: 0)
- let snapshotter = MKMapSnapshotter(options: options)
- snapshotter.startWithCompletionHandler { snapshot, error in
- if snapshot != nil {
- let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: width, height: height))
- imageView.image = snapshot!.image
- snapshotView.addSubview(imageView)
- }
- }
- annotationView.detailCalloutAccessoryView = snapshotView
- }
- func mapView(mapView: MKMapView, didSelectAnnotationView view: MKAnnotationView) {
- let calloutView = ...
- calloutView.translatesAutoresizingMaskIntoConstraints = false
- calloutView.backgroundColor = UIColor.lightGrayColor()
- view.addSubview(calloutView)
- NSLayoutConstraint.activateConstraints([
- calloutView.bottomAnchor.constraintEqualToAnchor(view.topAnchor, constant: 0),
- calloutView.widthAnchor.constraintEqualToConstant(60),
- calloutView.heightAnchor.constraintEqualToConstant(30),
- calloutView.centerXAnchor.constraintEqualToAnchor(view.centerXAnchor, constant: view.calloutOffset.x)
- ])
- }
- @interface CustomeAnnotationView : MKAnnotationView
- @property (strong, nonatomic) UIButton *buttonCustomeCallOut;
- - (void)setSelected:(BOOL)selected animated:(BOOL)animated;
- @end
- @implementation CustomeAnnotationView
- -(id)initWithFrame:(CGRect)frame
- {
- self = [super initWithFrame:frame];
- if (self) {
- // Initialization code
- }
- return self;
- }
- - (void)setSelected:(BOOL)selected animated:(BOOL)animated{
- [super setSelected:selected animated:animated];
- if(selected)
- {
- self.buttonCustomeCallOut = [UIButton buttonWithType:UIButtonTypeCustom];//iconShare//iconShareBlue
- [self.buttonCustomeCallOut addTarget:self action:@selector(buttonHandlerCallOut:) forControlEvents:UIControlEventTouchDown];
- [self.buttonCustomeCallOut setBackgroundColor:[UIColor blueColor]];
- [self.buttonCustomeCallOut setFrame:CGRectMake(-40,-80, 100, 100)];
- [self addSubview:self.buttonCustomeCallOut];
- [self.buttonCustomeCallOut setUserInteractionEnabled:YES];
- }
- else
- {
- //Remove your custom view...
- [self.buttonCustomeCallOut setUserInteractionEnabled:NO];
- [self.buttonCustomeCallOut removeFromSuperview];
- self.buttonCustomeCallOut=nil;
- }
- }
- -(void)buttonHandlerCallOut:(UIButton*)sender{
- NSLog(@"Annotation Clicked");
- }
- - (UIView*)hitTest:(CGPoint)point withEvent:(UIEvent*)event
- {
- UIView* v = [super hitTest:point withEvent:event];
- if (v != nil)
- {
- [self.superview bringSubviewToFront:self];
- }
- return v;
- }
- - (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent*)event
- {
- CGRect rec = self.bounds;
- BOOL isIn = CGRectContainsPoint(rec, point);
- if(!isIn)
- {
- for (UIView *v in self.subviews)
- {
- isIn = CGRectContainsPoint(v.frame, point);
- if(isIn)
- break;
- }
- }
- return isIn;
- }
- @end
- - (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
- static NSString *identifier = @"CustAnnotation";
- CustomeAnnotationView *annotationView = (CustomeAnnotationView *) [self.mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
- if (annotationView == nil) {
- annotationView = [[CustomeAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:identifier];
- }
- annotationView.enabled = YES;
- annotationView.canShowCallout = NO;
- annotationView.centerOffset = CGPointMake(0,-10);//-18
- return annotationView;
- }
Add Comment
Please, Sign In to add comment