- Notifications
You must be signed in to change notification settings - Fork21
Use any custom view as custom callout view for MKMapView with cool animations. Use any image as annotation view.
License
okhanokbay/MapViewPlus
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
MapViewPlus gives you the missing methods of MapKit which are:imageForAnnotation andcalloutViewForAnnotationView delegate methods.
- Return any UIImage from
imageForAnnotation(image shouldn't have paddings) - Create any UIView to use as your custom callout view and return it from
calloutViewForAnnotationView - MapViewPlus will:
- Add an anchor view to bottom of your callout view
- Combine callout view with the anchor view and add shadow to both of them
- Add a cool animation to
CalloutAndAnchorView - Make it user interaction enabled (this may be easy but tricky sometimes)
- Scrolls map view to show the callout view completely after tapping the annotation view
- Even give a ready-to-use template for callout view
- Forward all of the delegate methods of
MKMapViewto your subclass ofMapViewPlus(exceptmapView:viewForAnnotation:)
- Swift 5.0
- iOS 10.0+
MapViewPlus is available throughCocoaPods. To installit, simply add the following line to your Podfile:
pod'MapViewPlus'
If you don't use CocoaPods, you can drag and drop all the classes and use it in your project.
1) If you are using Interface Builder, set your map view's class and module as MapViewPlus:
2) Setup your Callout View Model:
import UIKitimport MapViewPlus// CalloutViewModel is a protocol of MapVieWPlus. Currently, it has no requirements to its conformant classes.classYourCalloutViewModel:CalloutViewModel{vartitle:Stringvarimage:UIImageinit(title:String, image:UIImage){self.title= titleself.image= image}}
3) Setup your CalloutView:
- Create any view in an xib file (or programmatically):
- Wire your view to your callout view class:
import UIKitimport MapViewPlus// CalloutViewPlus is a protocol of MapViewPlus. // Currently, it has just one requirement -> func configureCallout(_ viewModel: CalloutViewModel)classYourCalloutView:UIView,CalloutViewPlus{@IBOutlet weakvarlabel:UILabel!@IBOutlet weakvarimageView:UIImageView!func configureCallout(_ viewModel:CalloutViewModel){letviewModel= viewModelas!YourCalloutViewModel label.text= viewModel.title imageView.image= viewModel.image}}
4) Setup your Annotations in your View Controller:
import UIKitimport CoreLocationimport MapViewPlusclassYourViewController:UIViewController{@IBOutlet weakvarmapView:MapViewPlus!overridefunc viewDidLoad(){ super.viewDidLoad() //Required mapView.delegate=selfletviewModel=YourCalloutViewModel(title:"Cafe", image:UIImage(named:"cafe.png")!)letannotation=AnnotationPlus(viewModel: viewModel, coordinate:CLLocationCoordinate2DMake(50.11,8.68))varannotations:[AnnotationPlus]=[] annotations.append(annotation) mapView.setup(withAnnotations: annotations)}}
5) Return the image for Annotation and the View for Callout
extensionYourViewController:MapViewPlusDelegate{func mapView(_ mapView:MapViewPlus, imageFor annotation:AnnotationPlus)->UIImage{returnUIImage(named:"your_annotation_image.png")!}func mapView(_ mapView:MapViewPlus, calloutViewFor annotationView:AnnotationViewPlus)->CalloutViewPlus{letcalloutView=Bundle.main.loadNibNamed("YourCalloutView", owner:nil, options:nil)!.firstas!YourCalloutViewreturn calloutView}}
This is it. You are all set and ready to go now.
MapViewPlus is highly customizable:
- Change the center of the callout view related to anchor view and callout view:
func mapView(_ mapView:MapViewPlus, centerForCalloutViewOf annotationView:AnnotationViewPlus)->CalloutViewPlusCenter
- Change the bounds for callout view without changing the frames in Interface Builder. If you don't use this delegate method, then your callout view's frame will stay the same as the Interface Builder file. You can also use this to change frame dynamically according to the data in callout view!
func mapView(_ mapView:MapViewPlus, boundsForCalloutViewOf annotationView:AnnotationViewPlus)->CalloutViewPlusBound
- Change the inset of anchor view related to callout view. Anchor view will go under the callout view as amount the value you returned from this method. Defaults to 0.
func mapView(_ mapView:MapViewPlus, insetFor calloutView:CalloutViewPlus)->CGFloat
- Change the animation type forshowing the callout view. Defaults to
.fromBottomand available types are,.fromTop,.fromBottom,.fromLeft,.fromRight
func mapView(_ mapView:MapViewPlus, animationTypeForShowingCalloutViewOf annotationView:AnnotationViewPlus)->CalloutViewShowingAnimationType
- Change the animation type forhiding the callout view. Defaults to
.toBottomand available types are,.toTop,.toBottom,.toLeft,.toRight
func mapView(_ mapView:MapViewPlus, animationTypeForHidingCalloutViewOf annoationView:AnnotationViewPlus)->CalloutViewHidingAnimationType
- Change the height for anchor. Anchor viewwill always draw a equilateral triangle by taking the value you supplied as the triangle's height. You can change the height and the anchor view will calculate the necessary size for you:
func mapView(_ mapView:MapViewPlus, heightForAnchorOf calloutView:CalloutViewPlus)->CGFloat
- Change the background color of anchor view. This is very important. You can supply any color you want for the anchor view. Generally you would like to make this color as same as the background color of your callout view. Just change it like:
func mapView(_ mapView:MapViewPlus, fillColorForAnchorOf calloutView:CalloutViewPlus)->UIColor
- MapViewPlus supplies you a ready-to-go template for callout view. You can see the usage example of it in the example project.
- It allows you to specify the source of the image. It has three options:
.downloadable(imageURL: URL, placeholder: UIImage?)- Downloads the image with the help of Kingfisher framework
.fromBundle(image: UIImage).none
MapViewPlus uses methods fromMKMapViewDelegate, but not all of them. It forwards all of the delegate methods exceptmapView:viewForAnnotation:. This method is used internally and won't be redirected to your subclass.
Normally,MapViewPlus will abstract you fromMapKit when you don't want to use the other methods ofMKMapViewDelegate. But when you want to use the other methods fromMKMapViewDelegate, you can easily do that without any extra efforts. Just write them down and they will get called byMapViewPlusDelegate. Please see howmapView(_:regionDidChangeAnimated:) method is being called inDefaultCalloutViewController.swift (in the example project) even if you don't conform toMKMapViewDelegate. If in the future, some new methods are added toMKMapViewDelegate, they will be automagically forwarded to you byMapViewPlusDelegate without a new version of the framework. There is no wrapping occuring in the background.
To run the example project, clone the repo, and runpod install from the Example directory first.
MapViewPlus is available under the MIT license. See the LICENSE file for more info.
About
Use any custom view as custom callout view for MKMapView with cool animations. Use any image as annotation view.
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Uh oh!
There was an error while loading.Please reload this page.
Contributors2
Uh oh!
There was an error while loading.Please reload this page.





