Reputation: 19
I fetch the user current location data.But the problem is I can't create proper geofencing . After geofencing validate current location with some static lat long radius .
import UIKit
import MapKit
import CoreLocation
import UserNotifications
class ViewController: UIViewController,CLLocationManagerDelegate,UNUserNotificationCenterDelegate {
@IBOutlet weak var mapView: MKMapView!
let manager = CLLocationManager()
let geocoder = CLGeocoder()
var locality = ""
var administrativeArea = ""
var country = ""
var totalAdress:String?
var maplatitude:String?
var maplognitude:String?
override func viewDidLoad() {
super.viewDidLoad()
manager.delegate = self
manager.desiredAccuracy = kCLLocationAccuracyBest
manager.requestWhenInUseAuthorization()
manager.startUpdatingLocation()
// Your coordinates go here (lat, lon)
let geofenceRegionCenter = CLLocationCoordinate2D(
latitude: 37.33233141,
longitude: -122.0312186
)
let geofenceRegion = CLCircularRegion(
center: geofenceRegionCenter,
radius: 100,
identifier: "UniqueIdentifier"
)
geofenceRegion.notifyOnEntry = true
geofenceRegion.notifyOnExit = true
self.manager.startMonitoring(for: geofenceRegion)
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let location = locations[0]
let span:MKCoordinateSpan = MKCoordinateSpan(latitudeDelta: 0.01, longitudeDelta: 0.01)
let myLocation:CLLocationCoordinate2D = CLLocationCoordinate2DMake(location.coordinate.latitude, location.coordinate.longitude)
let region:MKCoordinateRegion = MKCoordinateRegion(center: myLocation, span: span)
mapView.setRegion(region, animated: true)
manager.stopUpdatingLocation()
// manager.startUpdatingLocation()
// print(location.altitude)
// print(location.speed)
// maplatitude = location.altitude
self.mapView.showsUserLocation = true
var locationArray = locations as NSArray
var locationObj = locationArray.lastObject as! CLLocation
var coord = locationObj.coordinate
maplatitude = String(coord.longitude)
maplognitude = String(coord.latitude)
// longitude.text = coord.longitude
// latitude.text = coord.latitude
// longitude.text = "\(coord.longitude)"
// latitude.text = "\(coord.latitude)"
self.mapView.showsUserLocation = true
print("Latitude is ",coord.latitude)
print("Lognitude is ",coord.longitude)
// maplatitude = String(coord.longitude)
// maplognitude = String(coord.latitude)
geocoder.reverseGeocodeLocation(location, completionHandler: {(placemarks, error) in
if (error != nil) {
print("Error in reverseGeocode")
}
let placemark = placemarks! as [CLPlacemark]
if placemark.count > 0 {
let placemark = placemarks![0]
self.locality = placemark.locality!.removeWhitespace()
self.administrativeArea = placemark.administrativeArea!.removeWhitespace()
self.country = placemark.country!.removeWhitespace()
self.totalAdress = self.locality + "," + self.administrativeArea + "," + self.country
print("mylocality",self.locality)
print("country",self.country)
print("myLocality",self.totalAdress)
//self.adressNAmeLabel.text = self.totalAdress
}
})
}
}
Upvotes: 1
Views: 826
Reputation: 2351
My suggestion is to use notifications for this
let location = CLLocationCoordinate2DMake(myLatitude, myLongtitude)
let region = CLCircularRegion(center: location, radius: radius, identifier: "my-custom-uuid")
let content = UNMutableNotificationContent()
content.title = "Title"
content.body = "Body"
content.categoryIdentifier = "alarm"
content.sound = UNNotificationSound.default
region.notifyOnEntry = true
region.notifyOnExit = false
let trigger = UNLocationNotificationTrigger(region: region, repeats: false)
let request = UNNotificationRequest(identifier:"uuid", content: content, trigger: trigger)
self.center.add(request, withCompletionHandler: nil)
This is a link to one tutorial here
And also you should have in mind that you will need location authorization(the good point here is that you need just in-app not in background) and you can register up to 20 points.
Upvotes: 1