I am integrating Mapbox iOS SDK into my app.
Right now I am stuck at a point where I want to achieve car tracking feature like Uber app.
I used to have that feature with Google Maps SDK but I cant make it work with Mapbox SDK.
I am adding MGLPointAnnotation object to add it on map and want to move it from point A to point B with animation.
I am doing it using
UIView.animate(withDuration: TimeInterval(duration), animations: {
// Update annotation coordinate to be the destination coordinate
point.coordinate = newCoordinate
}, completion: nil)
But for MGLPointAnnotation I can't change its image because when there's a turn I want to rotate the image(Annotation).
If I use MGLAnnotationView object I can change the image but I cant change its coordinate because its readonly.
What should I do here to achieve that functionality?
Several years ago I wrote smth similar, I used GoggleMaps, look through the code, maybe it will be helpful, I really don't remember what are all that numbers in angle counting
extension Double {
var degrees: Double {
return self * 180.0 / Double.pi
}
}
extension CLLocationCoordinate2D {
func angleToPosition(position : CLLocationCoordinate2D) -> CLLocationDegrees {
let bearingRadians = atan2(Double(position.latitude - latitude), Double(position.longitude - longitude))
var bearingDegrees = bearingRadians.degrees
// print("\(bearingDegrees)")
var roundDegrees = 360.0
if bearingDegrees < 0 {
if bearingDegrees > -90 {
roundDegrees = 350
}
if bearingDegrees < -90 && bearingDegrees > -180 {
roundDegrees = 370
}
bearingDegrees += roundDegrees
return 360 - bearingDegrees
}
roundDegrees = bearingDegrees < 90 ? 350 : 360
if bearingDegrees > 90 && bearingDegrees < 180 {
roundDegrees = 370
}
UserDefaults.standard.set(bearingDegrees, forKey: "bearingDegrees")
return roundDegrees - bearingDegrees
}
func duration(toDestination destination: CLLocationCoordinate2D, withSpeed speed : Double) -> Double {
let distance = GMSGeometryDistance(self, destination)
return distance / (speed * (1000 / 3600))
}
}
And this is the func which do the rotation, you can use it as soon as you receive new coordinates, or call it in for loop if you have certain polyline
func animateCarDrive(info: [String: Any]) {
let speed = info["speed"] as? Double ?? 40 // if your car's speed is changeable
let position = info["position"] as? CLLocationCoordinate2D // new point position
let duration = marker.position.duration(toDestination: position, withSpeed: speed)
marker.rotation = marker.position.angleToPosition(position: position)
CATransaction.begin()
CATransaction.setAnimationDuration(duration)
marker.position = position
CATransaction.commit()
}
Related
I am creating an Uber like iOS app(swift).I have integrated google map and added marker.Also i am receiving current latitude and longitude of the vehicle from backend API. Now i want to show the movement of vehicle in my app. I have written some code and it is working. But there is some issue with movement of the vehicle. There is some difference between position of marker in my app and the actual location of vehicle. Accuracy is not good. Here i am sharing my code.I calls the backend API in each 5 seconds.
func moveCab()
{
let oldCoordinate: CLLocationCoordinate2D? = CLLocationCoordinate2DMake(Double(oldLatitude)!,Double(oldLongitude)!)
let newCoordinate: CLLocationCoordinate2D? = CLLocationCoordinate2DMake(Double(currentLatitude)!,Double(currentLongitude)!)
mMarker.groundAnchor = CGPoint(x: CGFloat(0.5), y: CGFloat(0.5))
mMarker.rotation = CLLocationDegrees(getHeadingForDirection(fromCoordinate: oldCoordinate!, toCoordinate: newCoordinate!))
//found bearing value by calculation when marker add
mMarker.position = oldCoordinate!
//this can be old position to make car movement to new position
mMarker.map = mapView
//marker movement animation
CATransaction.begin()
CATransaction.setValue(Int(2.0), forKey: kCATransactionAnimationDuration)
CATransaction.setCompletionBlock({() -> Void in
self.mMarker.groundAnchor = CGPoint(x: CGFloat(0.5), y: CGFloat(0.5))
// mMarker.rotation = CDouble(data.value(forKey: "bearing"))
//New bearing value from backend after car movement is done
})
mMarker.position = newCoordinate!
mMarker.map = mapView
mMarker.groundAnchor = CGPoint(x: CGFloat(0.5), y: CGFloat(0.5))
mMarker.rotation = CLLocationDegrees(getHeadingForDirection(fromCoordinate: oldCoordinate!, toCoordinate: newCoordinate!))
//found bearing value by calculation
CATransaction.commit()
oldLatitude = currentLatitude
oldLongitude = currentLongitude
}
func getHeadingForDirection(fromCoordinate fromLoc: CLLocationCoordinate2D, toCoordinate toLoc: CLLocationCoordinate2D) -> Float {
let fLat: Float = Float((fromLoc.latitude).degreesToRadians)
let fLng: Float = Float((fromLoc.longitude).degreesToRadians)
let tLat: Float = Float((toLoc.latitude).degreesToRadians)
let tLng: Float = Float((toLoc.longitude).degreesToRadians)
let degree: Float = (atan2(sin(tLng - fLng) * cos(tLat), cos(fLat) * sin(tLat) - sin(fLat) * cos(tLat) * cos(tLng - fLng))).radiansToDegrees
if degree >= 0 {
return degree
}
else {
return 360 + degree
}
extension Int {
var degreesToRadians: Double { return Double(self) * .pi / 180 }
}
extension FloatingPoint {
var degreesToRadians: Self { return self * .pi / 180 }
var radiansToDegrees: Self { return self * 180 / .pi }
}
I need to find equidistant locations on a MKPolyline to add annotations as shown below.
My function to get locations on MKPolyline is given below, I have the values start and end coordinates of Polyline. But the locations are slightly ,moving out of polyline as shown in the image below
My function to find location is
func getEquidistantPoints(from startPoint: CLLocationCoordinate2D, to endPoint: CLLocationCoordinate2D, numberOfPoints: Int) -> [CLLocationCoordinate2D] {
var midPoints: [CLLocationCoordinate2D] = []
var newPoint: CLLocationCoordinate2D = CLLocationCoordinate2DMake(0, 0)
let count = numberOfPoints + 1
let latitudeModifier = (endPoint.latitude - startPoint.latitude) / Double(count)
let longitudeModifier = (endPoint.longitude - startPoint.longitude) / Double(count)
for i in 0..<count {
newPoint.latitude = CLLocationDegrees(startPoint.latitude + (latitudeModifier * Double(i)))
newPoint.longitude = CLLocationDegrees(startPoint.longitude + (longitudeModifier * Double(i)))
midPoints.append(newPoint)
}
return midPoints
}
In viewdidload
let coordinatesArray = getEquidistantPoints(from: sourceCoordinate, to: destinationCoordinate, numberOfPoints: 5)
for coordinate in coordinatesArray {
let annotation = MKPointAnnotation()
annotation.coordinate = coordinate
self.mapView.addAnnotation(annotation)
}
How can I solve this error in calculating locations?
The problem is that the earth is round. So your "line" is not a line; it is a curve traced out on the surface of a nominal sphere. You cannot find "equidistant" points along the line using your simple-minded method. You need to use some much more serious math than you are using. This will prove to be far from trivial.
I'm working on an App where I get the user location and some custom pins (Restaurants) that I added. What I want to do is set radius (between 5km and 10km) distance between the user and the restaurants and then show those that are within the area.
My question is, how can I set that radius and get the locations within it
Best regards!
You can do some distance calculations to determine what pins to show.
let pins = Array<MKPointAnnotation>() // This is an empty array, so just use your array of locations or add to this.
if let currentLocation = locationManager.location?.coordinate {
for pin in pins {
let locationMapPoint = MKMapPointForCoordinate(currentLocation)
let pinMapPoint = MKMapPointForCoordinate(pin.coordinate)
let distance = MKMetersBetweenMapPoints(locationMapPoint, pinMapPoint)
if distance >= 5000 && distance <= 10000 {
self.map.addAnnotation(pin)
}
}
}
If you wanted something tidier, you could also do this.
let pins = Array<MKPointAnnotation>()
if let currentLocation = locationManager.location?.coordinate {
let filtered = pins.filter { $0.coordinate.distance(to: currentLocation) >= 5000 && $0.coordinate.distance(to: currentLocation) <= 10000 }
self.map.addAnnotations(filtered)
}
You'll need this extension aswell.
extension CLLocationCoordinate2D {
func distance(to coordinate: CLLocationCoordinate2D) -> Double {
return MKMetersBetweenMapPoints(MKMapPointForCoordinate(self), MKMapPointForCoordinate(coordinate))
}
}
I am trying to develop an application based on Uber and Ola like concept. So for this i need to integrate the Google Map for the pick up and drop location ion iOS. So please tell me how to achieve the Moving annotation(car)animation in iOS using the Google map.
using Swift 3.1
var oldCoodinate: CLLocationCoordinate2D? = CLLocationCoordinate2DMake(CDouble((data.value(forKey: "lat") as? CLLocationCoordinate2D)), CDouble((data.value(forKey: "lng") as? CLLocationCoordinate2D)))
var newCoodinate: CLLocationCoordinate2D? = CLLocationCoordinate2DMake(CDouble((data.value(forKey: "lat") as? CLLocationCoordinate2D)), CDouble((data.value(forKey: "lng") as? CLLocationCoordinate2D)))
driverMarker.groundAnchor = CGPoint(x: CGFloat(0.5), y: CGFloat(0.5))
driverMarker.rotation = getHeadingForDirection(fromCoordinate: oldCoodinate, toCoordinate: newCoodinate)
//found bearing value by calculation when marker add
driverMarker.position = oldCoodinate
//this can be old position to make car movement to new position
driverMarker.map = mapView_
//marker movement animation
CATransaction.begin()
CATransaction.setValue(Int(2.0), forKey: kCATransactionAnimationDuration)
CATransaction.setCompletionBlock({() -> Void in
driverMarker.groundAnchor = CGPoint(x: CGFloat(0.5), y: CGFloat(0.5))
driverMarker.rotation = CDouble(data.value(forKey: "bearing"))
//New bearing value from backend after car movement is done
})
driverMarker.position = newCoodinate
//this can be new position after car moved from old position to new position with animation
driverMarker.map = mapView_
driverMarker.groundAnchor = CGPoint(x: CGFloat(0.5), y: CGFloat(0.5))
driverMarker.rotation = getHeadingForDirection(fromCoordinate: oldCoodinate, toCoordinate: newCoodinate)
//found bearing value by calculation
CATransaction.commit()
extension Int {
var degreesToRadians: Double { return Double(self) * .pi / 180 }
}
extension FloatingPoint {
var degreesToRadians: Self { return self * .pi / 180 }
var radiansToDegrees: Self { return self * 180 / .pi }
}
method for get bearing value from old and new coordinates
func getHeadingForDirection(fromCoordinate fromLoc: CLLocationCoordinate2D, toCoordinate toLoc: CLLocationCoordinate2D) -> Float {
let fLat: Float = Float((fromLoc.latitude).degreesToRadians)
let fLng: Float = Float((fromLoc.longitude).degreesToRadians)
let tLat: Float = Float((toLoc.latitude).degreesToRadians)
let tLng: Float = Float((toLoc.longitude).degreesToRadians)
let degree: Float = (atan2(sin(tLng - fLng) * cos(tLat), cos(fLat) * sin(tLat) - sin(fLat) * cos(tLat) * cos(tLng - fLng))).radiansToDegrees
if degree >= 0 {
return degree
}
else {
return 360 + degree
}
}
for Objective C code: Move GMSMarker on Google Map Like UBER
For github: ARCarMovement
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 regarding AppleMap.
To move map Pin or AnnotationView , we can use the CoreLocation framework which outputs data related to the users current location.
import CoreLocation
class ViewController: UIViewController, CLLocationManagerDelegate {
var locationManager:CLLocationManager!
override func viewDidLoad() {
super.viewDidLoad()
locationManager = CLLocationManager()
locationManager.delegate = self
locationManager.startUpdatingHeading()
}
func locationManager(manager: CLLocationManager!, didUpdateHeading heading: CLHeading!) {
// This will print out the direction the device is heading
println(heading.magneticHeading) }
}
}
In the above example, "heading.magneticHeading" will output a value representing the direction the device is pointed at.
0 means north
90 means east
180 means south
270 means west
everything else in between
The next step is to use those values and rotate your AnnotationView or Pin 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.
float degrees = 45
imageView.transform = CGAffineTransformMakeRotation(degrees * M_PI/180)
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 :
https://stackoverflow.com/a/7634232/3400991
Finally Resolved my issue. Hope this complete answer helps other too.