如何快速将地理围栏位置验证为当前位置?

时间:2019-07-26 07:05:33

标签: ios swift

我获取了用户当前的位置数据。但是问题是我无法创建正确的地理围栏。经过地理围栏后,以一定的静态纬度长半径验证当前位置。

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
        }
    })
}
}

1 个答案:

答案 0 :(得分:1)

我的建议是为此使用通知

                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)

这是一个教程here

的链接

并且还应该记住,您将需要位置授权(这里的好处是您只需要在应用程序中而不是在后台),并且最多可以注册20个点。

相关问题