iOS App Mapview Line Draw

Posted

技术标签:

【中文标题】iOS App Mapview Line Draw【英文标题】: 【发布时间】:2017-02-06 21:57:23 【问题描述】:

在地图视图上显示折线时遇到问题

按照本教程 MapView Tutorial

附上我的代码。 注解出现在地图上但无法调用渲染器方法。虽然代表在那里。 主要问题:无法在两个坐标之间画线 控制台输出: 2017-02-06 22:54:56.770584 MapTest[2329:805733] [LogMessageLogging] 6.1 无法检索 CarrierName。 CTError: domain-2, code-5, errStr:((os/kern) failure) 这是代码

import UIKit
import MapKit

class ViewController: UIViewController,MKMapViewDelegate 
    @IBOutlet weak var myMap: MKMapView!

    override func viewDidLoad() 
        super.viewDidLoad()

        // 1.
        myMap.delegate = self

        // 2.
        let sourceLocation = CLLocationCoordinate2D(latitude: 40.759011, longitude: -73.984472)
        let destinationLocation = CLLocationCoordinate2D(latitude: 40.748441, longitude: -73.985564)

        // 3.
        let sourcePlacemark = MKPlacemark(coordinate: sourceLocation, addressDictionary: nil)
        let destinationPlacemark = MKPlacemark(coordinate: destinationLocation, addressDictionary: nil)

        // 4.
        let sourceMapItem = MKMapItem(placemark: sourcePlacemark)
        let destinationMapItem = MKMapItem(placemark: destinationPlacemark)

        // 5.
        let sourceAnnotation = MKPointAnnotation()
        sourceAnnotation.title = "Times Square"

        if let location = sourcePlacemark.location 
            sourceAnnotation.coordinate = location.coordinate
        


        let destinationAnnotation = MKPointAnnotation()
        destinationAnnotation.title = "Empire State Building"

        if let location = destinationPlacemark.location 
            destinationAnnotation.coordinate = location.coordinate
        

        // 6.
        self.myMap.showAnnotations([sourceAnnotation,destinationAnnotation], animated: true )

        // 7.
        let directionRequest = MKDirectionsRequest()
        directionRequest.source = sourceMapItem
        directionRequest.destination = destinationMapItem
        directionRequest.transportType = .automobile

        // Calculate the direction
        let directions = MKDirections(request: directionRequest)

        // 8.
        directions.calculate 
            (response, error) -> Void in

            guard let response = response else 
                if let error = error 
                    print("Error: \(error)")
                

                return
            

            let route = response.routes[0]
            self.myMap.add((route.polyline), level: MKOverlayLevel.aboveRoads)

            let rect = route.polyline.boundingMapRect
            self.myMap.setRegion(MKCoordinateRegionForMapRect(rect), animated: true)
        
    
    override func didReceiveMemoryWarning() 
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    
    func mapView(mapView: MKMapView, rendererForOverlay overlay: MKOverlay) -> MKOverlayRenderer 
        print("Line 85 is being called......start...")
        let renderer = MKPolylineRenderer(overlay: overlay)
        renderer.strokeColor = UIColor.red
        renderer.lineWidth = 4.0

        print("Line 85 is being called.......end..")
        return renderer
    


【问题讨论】:

【参考方案1】:

您的 rendererForOverlay 函数语法错误; Xcode 在测试您的代码时告诉我这一点。使用

func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer

相反,将在两点之间画一条线。

【讨论】:

以上是关于iOS App Mapview Line Draw的主要内容,如果未能解决你的问题,请参考以下文章

Draw a line segment with PyOpenGL

Unity 实现《物理画线》Draw Physics Line 总览

❤️你还在用visio画图吗?已经落伍啦!快来试试轻量级绘图神器draw.io吧!❤️

Android:在 MapView 上为 ItemizedOverlay 使用 9-patch 和文本

❤️你还在用visio画图吗?已经落伍啦!快来试试轻量级绘图神器draw.io吧!❤️

xml 画一条线。字体:https://stackoverflow.com/questions/3616676/how-to-draw-a-line-in-android