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

时间:2015-02-19 03:22:17

标签: ios swift mkmapview cllocationmanager mkoverlay

我创建了一个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
}

我正在寻找一种方法,我可以让圆形贴图一直贴在用户位置并随之移动。

1 个答案:

答案 0 :(得分:0)

在这段代码中:

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)的某些新实例 - 该行基本上什么都不做。< / p>


由于地图上唯一的叠加层是单个圆圈,因此在添加新贴图之前,您只需调用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)