- SWIFT 3.0
- MKMAPVIEW
- iOS
Note : - (Integrated AppleMap
,Not working with GoogleMap
)
I have done the following :
- Implemented map and Added custom Image to User Location Annotation
- When map open , it shows User Location at right Place
My Requirement :
When User Move into different direction staying at same place (or different place) the pin (at current location) should automatically point the direction in which user points.
E.g : If Boat is showing at User Location position and its pointing toward North but if user move toward West then boat (User Location Pin) also should point to that direction.
Tried with following Code :
//MARK:Change Direction Methods
func angle(fromCoordinate first: CLLocationCoordinate2D, toCoordinate second: CLLocationCoordinate2D) -> Float {
let deltaLongitude = second.longitude - first.longitude
let deltaLatitude = second.latitude - first.latitude
let angle = (.pi * 0.5) - atan(deltaLatitude / deltaLongitude)
if deltaLongitude > 0 {
return Float(angle)
}
else if deltaLongitude < 0 {
return Float(angle) + .pi
}
else if deltaLatitude < 0 {
return .pi
}
return 0.0
}
//Animate direction of User Location
func animateUserLocation() {
//Old Coordinate (PLAT - Previous Lat , PLON - Previous Long)
let oldLocation = CLLocationCoordinate2D(latitude: UserDefaults.standard.value(forKey: "PLAT") as! CLLocationDegrees, longitude: UserDefaults.standard.value(forKey: "PLON") as! CLLocationDegrees)
//New Coordinate (PLAT - Current Lat , PLON - Current Long)
let newLocation = CLLocationCoordinate2D(latitude: UserDefaults.standard.value(forKey: "LAT") as! CLLocationDegrees, longitude: UserDefaults.standard.value(forKey: "LON") as! CLLocationDegrees)
let getAngle = angle(fromCoordinate:oldLocation, toCoordinate:newLocation)
var myAnnotation : RestaurantAnnotation?
if annotationArray.count > 0{
myAnnotation = annotationArray[0]
}
else {
return
}
UIView.animate(withDuration: 2, animations: {() -> Void in
myAnnotation?.coordinate = newLocation
let annotationView = self.map.view(for: myAnnotation!)
annotationView?.transform = CGAffineTransform(rotationAngle: CGFloat(getAngle))
})
//Save Previous lat long
UserDefaults.standard.set(UserDefaults.standard.value(forKey: "LAT"), forKey: "PLAT")
UserDefaults.standard.set(UserDefaults.standard.value(forKey: "LON"), forKey: "PLON")
UserDefaults().synchronize()
}
Called animateUserLocation
method from didUpdateLocation
Method but no Luck.
Kindly share your suggestion what i am doing wrong . Thanks in advance.
Understanding
iOS's
location concepts completely helps to resolve any issue regardingAppleMap
.To move map Pin or AnnotationView , we can use the
CoreLocation
framework which outputs data related to the users current location.In the above example, "
heading.magneticHeading
" will output a value representing the direction the device is pointed at.The next step is to use those values and rotate your
AnnotationView
orPin
accordingly.CGAffineTransformMakeRotation
can help with this.For example if you want to rotate to imageview to point to northeast, which would require a degree value of 45, your code might look something like this.
Just be aware that
CGAffineTransformMakeRotation()
expects a radian value, in the example above we've converted degrees to radians by multiplying degrees with the number of half circles.Following links really helpful :
Finally Resolved my issue. Hope this complete answer helps other too.