使用保存在核心数据中的坐标绘制地图视图

时间:2022-05-20 21:20:48

I have a list of coordinates stored in Core Data, along with the time at which they were saved, in the following format:

我有一份存储在核心数据中的坐标列表,以及保存它们的时间,格式如下:

game - Integer 16

游戏-整数16

latitude - Double

纬度-双

longitude - Double

经度-双

time - Date

日期-时间

I have managed to take the user's location and store it in Core Data without issue, but I am having trouble retrieving it and displaying it on a map. I have tried looking for a suitable tutorial, but I can't find anything that combines the Core Data and the MKPolyline aspects of my query. I'm quite new to coding in general and I can't quite align the two.

我成功地获取了用户的位置并将其存储在Core Data中,没有问题,但是我在检索和在地图上显示它时遇到了麻烦。我已经尝试寻找一个合适的教程,但是我找不到任何可以结合我的查询的核心数据和MKPolyline方面的东西。一般来说,我对编码很陌生,我不能很好地将两者结合起来。

So, I know the following code will help me draw on to the map:

因此,我知道下面的代码将帮助我绘制地图:

var coordinates = locations.map({ (location: CLLocation) -> 
CLLocationCoordinate2D in
    return location.coordinate
})

var polyline = MKPolyline(coordinates: &coordinates, 
                        count: locations.count)

And I already have this in my code to get the Core Data:

我已经在我的代码中得到了Core Data:

@IBOutlet weak var mapView: MKMapView!

var locationsList: [Locations] = []

var contextMap = (UIApplication.sharedApplication().delegate as AppDelegate).managedObjectContext!

    var requestMap = NSFetchRequest(entityName: "Locations")
    let predMap = NSPredicate(format: "game = %d", gameNumber)
    requestMap.predicate = predMap
    requestMap.sortDescriptors = [NSSortDescriptor(key:"time", ascending: false)]

    self.locationsList = context.executeFetchRequest(requestMap, error: nil)! as [Locations]

But I just can't marry the two up in order to take my coordinates from Core Data and put them on the map.

但我不能为了从核心数据中获取坐标并把它们放在地图上而把它们结合起来。

Any help would be great - thanks.

任何帮助都很好——谢谢。

1 个解决方案

#1


1  

You already have an array of CLLocation coordinates and built a MKPolyline. Everything fine so far.

您已经有了一个CLLocation坐标的数组,并构建了一个MKPolyline。到目前为止一切都好。

Now you need to tell the MKMapView to draw that polyline on the map:

现在你需要告诉MKMapView在地图上画一条折线:

...
var polyline = MKPolyline(coordinates: &coordinates, count: locations.count)

// add the overlay
self.mapView.addOverlay(polyLine, level: MKOverlayLevel.AboveLabels)
...

To draw the overlay you need to create a MKOverlayRenderer

要绘制覆盖,需要创建一个MKOverlayRenderer

func mapView(mapView: MKMapView!, rendererForOverlay overlay: MKOverlay!) -> MKOverlayRenderer! {
    if overlay.isKindOfClass(MKPolyline) {
        // draw the track
        let polyLine = overlay
        let polyLineRenderer = MKPolylineRenderer(overlay: polyLine)
        polyLineRenderer.strokeColor = UIColor.blueColor()
        polyLineRenderer.lineWidth = 2.0

        return polyLineRenderer
    }

    return nil
}

#1


1  

You already have an array of CLLocation coordinates and built a MKPolyline. Everything fine so far.

您已经有了一个CLLocation坐标的数组,并构建了一个MKPolyline。到目前为止一切都好。

Now you need to tell the MKMapView to draw that polyline on the map:

现在你需要告诉MKMapView在地图上画一条折线:

...
var polyline = MKPolyline(coordinates: &coordinates, count: locations.count)

// add the overlay
self.mapView.addOverlay(polyLine, level: MKOverlayLevel.AboveLabels)
...

To draw the overlay you need to create a MKOverlayRenderer

要绘制覆盖,需要创建一个MKOverlayRenderer

func mapView(mapView: MKMapView!, rendererForOverlay overlay: MKOverlay!) -> MKOverlayRenderer! {
    if overlay.isKindOfClass(MKPolyline) {
        // draw the track
        let polyLine = overlay
        let polyLineRenderer = MKPolylineRenderer(overlay: polyLine)
        polyLineRenderer.strokeColor = UIColor.blueColor()
        polyLineRenderer.lineWidth = 2.0

        return polyLineRenderer
    }

    return nil
}