我正在构建一个基本的地理围栏应用程序,允许用户创建地理围栏,在MKMapView上查看它们,以及激活和停用它们。它基于Ray Wenderlich教程,但我已经通过几种方式对其进行了改编。也就是说,我使用Realm来持久化数据,并且创建了一个单独的LocationHandler类,它充当LocationManagerDelegate并保存一个LocationManager。通常,我会尝试将一些函数移出viewControllers并放入单独的类中。
一切似乎都正常,除了周期性的地图注释和覆盖在模拟器中不能正确呈现。大约有20%的时间,注释和覆盖图不会在应该被删除的时候被删除。或者,颜色不会像应有的那样改变。或者,圆形覆盖会改变颜色,但关联的引脚不会。
这是由于我的代码中的一些错误,还是使用模拟器的工件?谢谢你的帮助
编辑以添加一些代码:在视图控制器中
//Clicking the 'x' deletes the geofence
func mapView(mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {
let anAnnotation = view.annotation as! GeofenceAnnotation
let geofence = anAnnotation.geofence
//stop monitoring geofence
locationManager.stopMonitoringGeofence(geofence!)
//remove representation of geofence from map
removeGeofenceRadiusCircle((geofence?.identifier)!)
mapView.removeAnnotation(anAnnotation)
//delete geofence from realm
try! realm.write {
realm.delete(geofence!)
}
updateGeofenceCount()
}
//Go through all overlays and remove appropriate one
func removeGeofenceRadiusCircle(id: String) {
self.mapView.delegate = self
if let overlays = mapView?.overlays {
for ol in overlays {
if let circleOverlay = ol as? GeofenceRadiusCircle {
let aId = circleOverlay.id
if aId == id {
mapView?.removeOverlay(circleOverlay)
break
}
}
}
}
}MKAnnotation类GeofenceAnnotation的子类: NSObject,MKAnnotation {
var coordinate: CLLocationCoordinate2D
var title: String?
var subtitle: String?
var geofence: Geofence?
init(coordinate: CLLocationCoordinate2D, title: String, subtitle: String, geofence: Geofence? = nil) {
self.coordinate = coordinate
self.title = title
self.subtitle = subtitle
self.geofence = geofence
}MKCircle的子类
class GeofenceRadiusCircle: MKCircle{
var geofence: Geofence?
var color: UIColor?
var id: String = ""
}发布于 2016-04-27 23:37:38
这似乎是我这边的一个小错误,也可能是模拟器的错误。在重新绘制viewWillAppear到account之前,我需要删除旧的覆盖。这似乎解决了覆盖和注释问题。我也有一个问题,用户的位置不能一直显示在mapView中,当我在手机上运行这个应用程序时,情况似乎并非如此。
https://stackoverflow.com/questions/36875801
复制相似问题