Swift 如何使用 MapKit 将叠加层粘贴到用户位置

How to stick an overlay to user location with MapKit in Swift

我创建了一个 mapView,它可以找到用户的位置并在用户周围绘制一个覆盖的绿色圆圈,但无论用户是移动还是静止,我都无法将这个绿色圆圈贴在用户身上。 我的所有尝试导致要么在用户之后制作这些圈子的尾巴,要么从用户的第一个位置开始制作一个圈子。

在我的 viewDidLoad() 中,我还启动了一个位置管理器实例,我从中获取位置数据。

您可以在下面看到我的位置经理委托。

我的 didUpdateLocations 是:

//CLLocationManager Delegate
func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
    //create array of locations of user
    var locationArray = locations as NSArray
    //find the last object of location
    var locationObj = locationArray.lastObject as CLLocation
    //creating circle
    var newCircle = MKCircle(centerCoordinate: locationObj.coordinate, radius: 100 as CLLocationDistance)
    if locations.count>1 {
        //I tried dispatch but still tail of green circles
        dispatch_sync(dispatch_queue_t(), { () -> Void in
        self.treesMap.removeOverlay(newCircle)
        })
    }

     self.treesMap.addOverlay(newCircle)        
    //if location determination is fixed
    if  locationFixAchieved == false {
    //declare that location is fixed locationManager.location.coordinate
        locationFixAchieved = true

        var prevLocation = centreLocation
        self.centreLocationCoordinate = (locationObj.coordinate as CLLocationCoordinate2D)
            println("location changed")

        //for output purpose
        println("locations = \(locations)")                

     }

}

我的 renderForOverlay 函数是:

/circle overlay function
func mapView(
    mapView: MKMapView!, rendererForOverlay   overlay: MKOverlay!) -> MKOverlayRenderer! {
        //if the overlay was type of circle
        if (overlay.isKindOfClass(MKCircle))
        {
            //declaring circleRenderer variable
            var circleRenderer = MKCircleRenderer(overlay: overlay)
            //setting stroke color of the circle
            circleRenderer.strokeColor = UIColor.greenColor()
            //setting fill color of cirlce
            circleRenderer.fillColor = UIColor(
                red: 0,
                green: 1.0,
                blue: 0,
                alpha: 0.5)
            //return rendererForOverlay function
            return circleRenderer
        }
        //if overlay was not type of MKCircle
        return nil
}

我正在寻找一种方法,使圆形叠加层始终贴在用户位置并在其旁边移动。

这段代码中:

var newCircle = MKCircle(centerCoordinate: locationObj.coordinate, radius: 100 as CLLocationDistance)
if locations.count>1 {
    //I tried dispatch but still tail of green circles
    dispatch_sync(dispatch_queue_t(), { () -> Void in
    self.treesMap.removeOverlay(newCircle)
    })
}

self.treesMap.addOverlay(newCircle)

removeOverlay 行(如果它甚至执行)不会删除以前的圆覆盖。

相反,它会尝试删除一些 新实例 圆形叠加层 (newCircle),它甚至还没有被添加到地图中——该行将基本上什么都不做。


由于您在地图上唯一想要的叠加层是单个圆圈,因此在添加新圆圈之前,您可以调用 removeOverlays(复数)并告诉地图删除所有现有的叠加层。

示例:

var newCircle = MKCircle(centerCoordinate: locationObj.coordinate, radius: 100 as CLLocationDistance)

/* comment this block out -- don't need it
if locations.count>1 {
    //I tried dispatch but still tail of green circles
    dispatch_sync(dispatch_queue_t(), { () -> Void in
    self.treesMap.removeOverlay(newCircle)
    })
}
*/

self.treesMap.removeOverlays(self.treesMap.overlays)

self.treesMap.addOverlay(newCircle)