put CLLocationCoordinate2D into CLLocation for Swift - ios

I have a method I want to call however when I get back the center of the map, it is in CLLocationCoordinate2D type.
How do I put the results of CLLocationCoordinate2D into CLLocation?

Figured it out.
When mapView changes region, get the Lat and Lon from CLLocationCoordinate2D and create a CLLocation variable with the lat and lon passed in.
func mapView(mapView: MKMapView!, regionDidChangeAnimated animated: Bool){
var centre = mapView.centerCoordinate as CLLocationCoordinate2D
var getLat: CLLocationDegrees = centre.latitude
var getLon: CLLocationDegrees = centre.longitude
var getMovedMapCenter: CLLocation = CLLocation(latitude: getLat, longitude: getLon)
self.lastLocation = getMovedMapCenter
self.fetchCafesAroundLocation(getMovedMapCenter)
}

Related

Instantiate an object of type CLLocation (Swift) [duplicate]

I have a method I want to call however when I get back the center of the map, it is in CLLocationCoordinate2D type.
How do I put the results of CLLocationCoordinate2D into CLLocation?
Figured it out.
When mapView changes region, get the Lat and Lon from CLLocationCoordinate2D and create a CLLocation variable with the lat and lon passed in.
func mapView(mapView: MKMapView!, regionDidChangeAnimated animated: Bool){
var centre = mapView.centerCoordinate as CLLocationCoordinate2D
var getLat: CLLocationDegrees = centre.latitude
var getLon: CLLocationDegrees = centre.longitude
var getMovedMapCenter: CLLocation = CLLocation(latitude: getLat, longitude: getLon)
self.lastLocation = getMovedMapCenter
self.fetchCafesAroundLocation(getMovedMapCenter)
}

How to Show Live Tracking Polyline in swift3?

I want to show live tracking in Map View.I am having latitudes and Longitudes List in separate Array… How to Show this in Map as a point.. Please anybody HelpOut For this Problem.
func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!)
{
let location = locations.last as! CLLocation
let location1 = locations.first as! CLLocation
// Have to get OldLocation And New Location From API
let oldCoordinates = location.coordinate
let newCoordinates = location1.coordinate
var area = [oldCoordinates, newCoordinates]
let polyline = MKPolyline(coordinates: &area, count: area.count)
mapView.add(polyline)
self.currentCLLocation = CLLocationCoordinate2D(latitude: newCoordinates.latitude, longitude: newCoordinates.longitude)
let region = MKCoordinateRegion(center: self.currentCLLocation, span: MKCoordinateSpan(latitudeDelta: 0.01, longitudeDelta: 0.01))
self.mapView.setRegion(region, animated: true)
print("Location Update Details")
print(location.coordinate.latitude)
print(location.coordinate.longitude)
self.mapView.delegate = self
let annotation = MKPointAnnotation()
annotation.coordinate = location1.coordinate
self.mapView.addAnnotation(annotation)
}
Values In Array:
Latitudes Array==>>
["12.988415", "12.988445", "12.988492", "12.988558", "12.988673", "12.988788", "12.988903", "12.989039"]
Longitudes Array==>>
["80.218386", "80.21839199999999", "80.218422", "80.218461", "80.218515", "80.218547", "80.21860100000001"]
How to Loop the latitude and Longitude Values in Location and Location1 Variable?
DidUpdate Method is not getting called Every time we update map?
How To show Polyline based on Lat and Long?

How to Set UITabBar background image change?

How can i sort the array based on distance from current location and show in tableview .when i use sorting am not getting any proper results ,am getting the array with random distance.can any one guide me for solve this issue
To sort locations based on distance from current location in best possible way would be have location points in form of struct
struct LocationPoints {
var latitude: CLLocationDegrees
var longitude: CLLocationDegrees
func distance(to currentLocation: CLLocation) -> CLLocationDistance {
return CLLocation(latitude: self.latitude, longitude: self.longitude).distance(from: currentLocation)
}
}
Let suppose you have an array of LocationPoints having latitude & longitude
var coordinates: [LocationPoints] = []
coordinates.append(LocationPoints(latitude: Double(25), longitude: Double(24)))
coordinates.append( LocationPoints(latitude: Double(23), longitude: Double(22)))
sort function
coordinates = sortLocationsWithCurrentLocation(locations: coordinates, currentLocation: CLLocation(latitude: Double(20), longitude: Double(21)))
func sortLocationsWithCurrentLocation(locations:[LocationPoints],currentLocation:CLLocation) -> [LocationPoints] {
//set here current position as current location
let currentPosition : CLLocation = CLLocation(latitude: 30, longitude: 24)
let sortedLocations = locations.sorted(by: { (point1 : LocationPoints, point2 :LocationPoints) -> Bool in
if point1.distance(to: currentPosition) < point2.distance(to: currentPosition)
{
return true
}
return false
})
return sortedLocations
}

Locations tracker icon not showing up on map

Currently I'm setting up location services but for some strange reason the tracker icon isn't appearing:
I know it's working because if I add a pin the pin shows up correctly:
I'm not sure why the tracker isn't showing up, here is the code:
#IBOutlet weak var map: MKMapView!
let locationsManager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
locationsManager.delegate = self
locationsManager.desiredAccuracy = kCLLocationAccuracyBest // sets to best location accuracy
locationsManager.requestWhenInUseAuthorization()// requests user location when app is opened
locationsManager.startUpdatingLocation()// updates user location
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let userLocation: CLLocation = locations[0]
let latitude: CLLocationDegrees = userLocation.coordinate.latitude
let longitude: CLLocationDegrees = userLocation.coordinate.longitude
let latDelta: CLLocationDegrees = 0.05
let longDelta: CLLocationDegrees = 0.05
let span: MKCoordinateSpan = MKCoordinateSpanMake(latDelta, longDelta)
let location: CLLocationCoordinate2D = CLLocationCoordinate2DMake(latitude,longitude)
let region: MKCoordinateRegion = MKCoordinateRegionMake(location, span)
map.setRegion(region, animated: true)
let pin = MKPointAnnotation()
pin.coordinate.latitude = userLocation.coordinate.latitude
pin.coordinate.longitude = userLocation.coordinate.longitude
pin.title = "My current location"
map.addAnnotation(pin)
}
You need to set the property showsUsersLocation to true. https://developer.apple.com/reference/mapkit/mkmapview/1452682-showsuserlocation
MKMapView can display the user location and track user location for you:
map.showsUserLocation = true
map.userTrackingMode = .follow
There is no need to implement your own location tracking delegate method.
Nevermind, figured it out. In viewDidload():
map.showsUserLocation = true

Swift : How to save latitude and longitude to use in another swift file

I have this code in ViewController to capture the current location from users:
#IBOutlet weak var userMapView: MKMapView!
var locationManager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
locationManager = CLLocationManager()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestWhenInUseAuthorization()
locationManager.startUpdatingLocation()
}
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let location : CLLocationCoordinate2D = manager.location!.coordinate
print(location.latitude)
let latitudeDelta : CLLocationDegrees = 0.01
let longitudeDelta : CLLocationDegrees = 0.01
let span : MKCoordinateSpan = MKCoordinateSpanMake(latitudeDelta, longitudeDelta)
let center : CLLocationCoordinate2D = CLLocationCoordinate2DMake(location.latitude, location.longitude)
let region : MKCoordinateRegion = MKCoordinateRegionMake(center, span)
self.userMapView.setRegion(region, animated: false)
self.userMapView.removeAnnotations(userMapView.annotations)
let userPinLocation : CLLocationCoordinate2D = CLLocationCoordinate2DMake(location.latitude, location.longitude)
let pinAnnotation = MKPointAnnotation()
pinAnnotation.coordinate = userPinLocation
pinAnnotation.title = "Sua localização atual"
self.userMapView.addAnnotation(pinAnnotation)
}
I have another swift file with the Database Functions, and I need save the latitude and longitude for use in database
How I store the latitude and longitude in variables to use outside the locationManager function?
do database operations in your locationManager(_:didUpdateLocations:) Instance Method.
Or var a Global Model.
CLLocationCoordinate2D is just a Struct containing 2 Doubles.
The very first line of your didUpdateLocations function extracts the current location into a local variable location:
let location : CLLocationCoordinate2D = manager.location!.coordinate
You could just change that from a local variable to an instance variable and it would be visible outside of that function. I'd make it an optional so it would be nil if the value was never assigned.

Resources