在给定坐标附近,在 Swift、MapKit 和 corelocation 中打印 hello
Near given coordinates, print hello in Swift, MapKit and corelocation
我想知道如何检查人是否在某个坐标附近,然后向控制台打招呼之类的操作进行测试。现在我已经有了用户位置,每次人移动时它都会更新,但是如何知道他是否在某个坐标或地址附近?例如:
func sayHello(){
if mapView.userLocation.coordinate.latitude == 26{
print("Hello")
}
我已经完成的代码:
import UIKit
import MapKit
import CoreLocation
class ViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate {
@IBOutlet weak var mapView: MKMapView!
let locationManager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
self.locationManager.delegate = self
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
self.locationManager.requestAlwaysAuthorization()
self.locationManager.startUpdatingLocation()
self.mapView.showsUserLocation = true
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation])
{
let location = locations.last
let center = CLLocationCoordinate2D(latitude: location!.coordinate.latitude, longitude: location!.coordinate.longitude)
let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 0.01, longitudeDelta: 0.01))
self.mapView.setRegion(region, animated: true)
self.locationManager.stopUpdatingLocation()
}
func locationManager(manager: CLLocationManager, didFailWithError error: NSError)
{
print("Errors: " + error.localizedDescription)
}
}
获取两个坐标之间的距离,如果小于 5m 则匹配 show print 语句
CLLocation *locA = [[CLLocation alloc] initWithLatitude:lat1 longitude:long1];
CLLocation *locB = [[CLLocation alloc] initWithLatitude:lat2 longitude:long2];
CLLocationDistance distance = [locA distanceFromLocation:locB];
我会做的是每次更新用户位置时,检查用户位置与您存储的地址位置之间的距离。如果用户在 x 米以内,则打印 "Hello"。
下面是我用来获取您的用户和地址之间的距离的代码。如果您有一个对象数组,其中包含您的地址及其坐标,您可以遍历每个地址并为 x 米内的每个地址打印 Hello。
let userLocation:CLLocation = CLLocation(latitude: 10.000, longitude: 29.000)
let addressLocation:CLLocation = CLLocation(latitude: 15.000, longitude: 20.000)
let meters:CLLocationDistance = userLocation.distanceFromLocation(addressLocation)
我想知道如何检查人是否在某个坐标附近,然后向控制台打招呼之类的操作进行测试。现在我已经有了用户位置,每次人移动时它都会更新,但是如何知道他是否在某个坐标或地址附近?例如:
func sayHello(){
if mapView.userLocation.coordinate.latitude == 26{
print("Hello")
}
我已经完成的代码:
import UIKit
import MapKit
import CoreLocation
class ViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate {
@IBOutlet weak var mapView: MKMapView!
let locationManager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
self.locationManager.delegate = self
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
self.locationManager.requestAlwaysAuthorization()
self.locationManager.startUpdatingLocation()
self.mapView.showsUserLocation = true
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation])
{
let location = locations.last
let center = CLLocationCoordinate2D(latitude: location!.coordinate.latitude, longitude: location!.coordinate.longitude)
let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 0.01, longitudeDelta: 0.01))
self.mapView.setRegion(region, animated: true)
self.locationManager.stopUpdatingLocation()
}
func locationManager(manager: CLLocationManager, didFailWithError error: NSError)
{
print("Errors: " + error.localizedDescription)
}
}
获取两个坐标之间的距离,如果小于 5m 则匹配 show print 语句
CLLocation *locA = [[CLLocation alloc] initWithLatitude:lat1 longitude:long1];
CLLocation *locB = [[CLLocation alloc] initWithLatitude:lat2 longitude:long2];
CLLocationDistance distance = [locA distanceFromLocation:locB];
我会做的是每次更新用户位置时,检查用户位置与您存储的地址位置之间的距离。如果用户在 x 米以内,则打印 "Hello"。
下面是我用来获取您的用户和地址之间的距离的代码。如果您有一个对象数组,其中包含您的地址及其坐标,您可以遍历每个地址并为 x 米内的每个地址打印 Hello。
let userLocation:CLLocation = CLLocation(latitude: 10.000, longitude: 29.000)
let addressLocation:CLLocation = CLLocation(latitude: 15.000, longitude: 20.000)
let meters:CLLocationDistance = userLocation.distanceFromLocation(addressLocation)