Я использовал CoreLocation для определения местоположения долготы и широты, и после изменения местоположения предположим, что камера будет перемещена в текущее местоположение. Я должен определить источник и долготу назначения и широту в коде. Когда я запускаю приложения, камера находится в текущем местоположении. Но после изменения текущего местоположения камера не может переместиться на новое место. После этого я хотел бы нарисовать маршрут между этими двумя точками.
import GoogleMaps
import GooglePlaces
import CoreLocation
class MapViewContoller: UIViewController, CLLocationManagerDelegate{
let locationManager = CLLocationManager()
var mapView: GMSMapView?
override func viewDidLoad() {
super.viewDidLoad()
locationManager.requestAlwaysAuthorization()
if CLLocationManager.locationServicesEnabled(){
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.startUpdatingLocation()
}
//Destination location
let long_d:Double
long_d = 115.208422
let lat_d:Double
lat_d = 25.320287
//Source location
let long_s:Double
long_s = 116.225138
let lat_s:Double
lat_s = 21.312400
let camera = GMSCameraPosition.camera(withLatitude:lat_s,longitude: long_s, zoom: 14.0)
let mapView = GMSMapView.map(withFrame: CGRect.zero, camera: camera)
view = mapView
let currentLocation = CLLocationCoordinate2DMake(22.312400, 114.225138)
let marker = GMSMarker(position: currentLocation)
marker.title = "Source"
marker.map = mapView
//Display Destination Location
//let deliveryLocation = CLLocationCoordinate2DMake(22.320287, 114.208422)
let deliveryLocation = CLLocationCoordinate2DMake(lat_d, long_d)
let markerSecond = GMSMarker(position: deliveryLocation)
markerSecond.title = "Destination"
markerSecond.map = mapView
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]){
if let location = locations.first{
print(location.coordinate)
}
let current: CLLocation = locations[0] as CLLocation
print("Current Location is \(current.coordinate.latitude), \(current.coordinate.longitude)")
if let location = locations.first {
CATransaction.begin()
CATransaction.setValue(Int(2), forKey: kCATransactionAnimationDuration)
mapView?.animate(toLocation: location.coordinate)
mapView?.animate(toZoom: 12)
CATransaction.commit()
let thirdLocation = CLLocationCoordinate2DMake(current.coordinate.latitude, current.coordinate.longitude)
print("The Third Location is ",thirdLocation)
let newCamera = GMSCameraPosition.camera(withLatitude: current.coordinate.latitude, longitude: current.coordinate.longitude, zoom:14.0)
mapView?.camera = newCamera
locationManager.startUpdatingLocation()
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}