检测用户是不是退出 MKCircle |地图套件
Posted
技术标签:
【中文标题】检测用户是不是退出 MKCircle |地图套件【英文标题】:Detect if user stepped out MKCircle | MapKit检测用户是否退出 MKCircle |地图套件 【发布时间】:2017-12-25 18:51:53 【问题描述】:我需要的东西(更简单的解释):
-
[完成] 获取用户位置并在 UIMapView 中显示
[完成] 在 UIMapView 中围绕用户添加圈子
检测用户是否离开该圈子
这是我的 1 和 2 代码:
import UIKit
import MapKit
import CoreLocation
class ViewController: UIViewController, CLLocationManagerDelegate, MKMapViewDelegate
@IBOutlet weak var myMap: MKMapView!
let manager = CLLocationManager()
var addedCircle = false
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation])
let location = locations[0]
let span: MKCoordinateSpan = MKCoordinateSpanMake(0.01, 0.01)
let myLocation: CLLocationCoordinate2D = CLLocationCoordinate2DMake(location.coordinate.latitude,
location.coordinate.longitude)
let region: MKCoordinateRegion = MKCoordinateRegionMake(myLocation, span)
myMap.setRegion(region, animated: true)
self.myMap.showsUserLocation = true
if !addedCircle
self.addRadiusCircle(location: location)
putted = true
func addRadiusCircle(location: CLLocation)
self.myMap.delegate = self
let circle = MKCircle(center: location.coordinate, radius: 100)
self.myMap.add(circle)
func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer
if overlay is MKCircle
let circle = MKCircleRenderer(overlay: overlay)
circle.strokeColor = UIColor.red
circle.fillColor = UIColor(red: 255, green: 0, blue: 0, alpha: 0.1)
circle.lineWidth = 1
return circle
else
return MKPolylineRenderer()
override func viewDidLoad()
super.viewDidLoad()
manager.delegate = self
manager.desiredAccuracy = kCLLocationAccuracyBest
manager.requestWhenInUseAuthorization()
manager.startUpdatingLocation()
这是这段代码的结果:
那个圆圈不随用户移动,我想知道如何检测用户是否走出那个红色圆圈,提前感谢您的回答
【问题讨论】:
你是说区域监控吗?文档链接:developer.apple.com/library/content/documentation/… 但 *** 上有很多区域监控问题。您需要更具体地说明您的问题。 【参考方案1】:您可以使用CLLocation
的distance(from:)
方法来确定用户当前位置与圆心的距离。
let location = CLLocation()
let circleCenter = CLLocation()
if location.distance(from: circleCenter) > circleRadius
// User is outside of circle.
【讨论】:
以上是关于检测用户是不是退出 MKCircle |地图套件的主要内容,如果未能解决你的问题,请参考以下文章
iOS - 添加和删除 MKCircle 覆盖到 MapView 导致故障