- iOS 11.0+
- Xcode 11.6
UrbvanCenterPinMapView is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod 'UrbvanCenterPinMapView'
- Change the class of a view from UIView to UrbvanCenterPinMapView
- Programmatically:
let pinMapView = UrbvanCenterPinMapView(frame: myFrame)
let pinMapView = UrbvanCenterPinMapView(frame: myFrame)
Center Pin's Pin image view's image to assign a custom pin asset
pinMapView.pinImage = UIImage(named: "my-pin-image")
Center Pin's Shadow image view's image to assign a custom pin asset
pinMapView.shadowImage = UIImage(named: "my-shadow-image")
Alternate shadow image, if specified the center pin's shadow will change to this one while the user us dragging the map
pinMapView.shadowImageWhenDragged = UIImage(named: "my-optional-second-shadow-image")
Center Pin's Shadow image view alpha value customization
pinMapView.shadowAlpha = 0.8
Different offsets if you want to adjust your custom assets for pin and shadow
pinMapView.pinOffsetY = 13
pinMapView.shadowOffsetX = 12
pinMapView.shadowOffsetY = 10
If you would like to know if map is being moved
You can implement UrbvanCenterPinMapView delegate to implement your own didStartDragging and didEndDragging functionality.
pinMapView.delegate = self
extension MyViewController: UrbvanCenterPinMapViewDelegate {
func didStartDragging() {
// My custom actions
}
func didEndDragging() {
// My custom actions
selectedLocation = pinMapView.mapview.centerCoordinate
}
}
You can also set your own MKMapView delegate while keeping UrbvanCenterPinMapView core functionality by using updateDragging() un MKMapViewDelegate
pinMapView.mapview.delegate = self
extension MyViewController: MKMapViewDelegate {
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
// My custom implementation
}
func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
// My custom implementation
}
func mapView(_ mapView: MKMapView, regionWillChangeAnimated animated: Bool) {
// My custom implementation
pinMapView.updateDragging() // Place this code to keep UrbvanCenterPinMapView delegate functionality
}
func mapView(_ mapView: MKMapView, regionDidChangeAnimated animated: Bool) {
// My custom implementation
pinMapView.updateDragging() // Place this code to keep UrbvanCenterPinMapView delegate functionality
}
}
If you would like to animate your pin while its being dragged you shloud use UIImageView animation. example:
pinMapView.delegate = self
let pinImages = (1...36).map { UIImage(named: "pin-\($0)")! }
pinMapView.pin.pin.animationImages = pinImages
pinMapView.pin.pin.animationDuration = 0.8
pinMapView.delegate = self
extension MyViewController: UrbvanCenterPinMapViewDelegate {
func didStartDragging() {
// My custom actions
pinMapView.pin.pin.startAnimating()
}
func didEndDragging() {
// My custom actions
pinMapView.pin.pin.stopAnimating()
}
}
You can center UrbvanCenterPinMapView on your current location by using:
pinMapView.pinMapView.centerOnUserLocation()
Or you cans set a custom coordinate where you want to center your map:
pinMapView.pinMapView.center(on coordinate: myCoordinate)
On both methods you can set a custom span for how much map you wish to show:
let mySpan = MKCoordinateSpan(latitudeDelta: 0.01, longitudeDelta: 0.01)
pinMapView.pinMapView.center(on coordinate: myCoordinate, span: mySpan)
If you update your map insets at run time you should call:
pinMapView.pinMapView.updateFrames(animated: true)
to set a new center for the pin in a smooth transition.
Daniel Esteban Salinas Suárez @Urbvan, [email protected]
UrbvanCenterPinMapView is available under the MIT license. See the LICENSE file for more info.