MapView is pin skrinking everytime. I'm using MKAnnotationView. Can anyone tell what what am I missing here why the pin shrinking? I want to show the pin like that star icon which is also annotation.
Also everytime I change the current location co-cordinate it will create new pin and old one also be there.
class CustomPointAnnotation: MKPointAnnotation {
var imageName: String!
}
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
if annotation is MKUserLocation {
return nil
}
let annotationView = MKMarkerAnnotationView(annotation: annotation, reuseIdentifier: "MyMarker")
annotationView.canShowCallout = true
switch annotation.title! {
case "Your Current Location":
annotationView.glyphImage = UIImage(named: "user_location")
default:
annotationView.canShowCallout = true
annotationView.glyphImage = UIImage(named: "user_location")
let buttonPin = UIButton(type: .custom)
buttonPin.frame = CGRect(x: 0, y: 0, width: 40, height: 40)
buttonPin.setImage(UIImage(named: "PhoneIcon"), for: .normal)
annotationView.rightCalloutAccessoryView = buttonPin
annotationView.markerTintColor = UIColor.purple
}
return annotationView
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
mapView.removeAnnotation(annotation)
let currentLocationValue = manager.location!.coordinate
let servicelocValue: CLLocationCoordinate2D = CLLocationCoordinate2DMake(coordinates.latitude, coordinates.longitude)
let annotation1 = CustomPointAnnotation()
annotation1.title = "Your Current Location"
annotation1.coordinate = currentLocationValue
annotation1.imageName = "user_location.png"
let annotation2 = CustomPointAnnotation()
annotation2.title = service ?? ""
annotation2.subtitle = location
annotation2.coordinate = servicelocValue
annotation2.imageName = "user_location.png"
annotations.append(annotation1)
annotations.append(annotation2)
mapView.addAnnotations(annotations)
mapView.cameraZoomRange = MKMapView.CameraZoomRange(minCenterCoordinateDistance: 50, maxCenterCoordinateDistance: 2000)
mapView.selectAnnotation(annotation2, animated: false)
}