2017-01-14 123 views
2

我用这个项目来产生我的热图:iOS的 - 迅速3 - 热图

https://github.com/dataminr/DTMHeatmap/issues/1 

从@johndpope:

https://github.com/johndpope/TranSafe 

https://github.com/dataminr/DTMHeatmap 

我综合了代码中规定

首先,它编译成功,但是当我使用像这样的“readData”:

readData([[52.517138, 13.401489], [52.517137, 13.401488], [52.517136, 13.401487]]) 

我得到的

这里是方法:

func readData(_ array: [[Double]]){ 
    self.heatmap = DTMHeatmap() 
    var dict = Dictionary<NSObject, AnyObject>(); 
    for entry in array{ 

     let coordinate = CLLocationCoordinate2D(latitude: entry[1], longitude: entry[0]); 

     let mapPoint = MKMapPointForCoordinate(coordinate) 

     let type = NSValue(mkCoordinate: coordinate).objCType // <- THIS IS IT 

     let value = NSValue(bytes: Unmanaged.passUnretained(mapPoint as AnyObject).toOpaque(), objCType: type); 
     dict[value] = 1 as AnyObject?; 

    } 
    self.heatmap.setData(dict as [AnyHashable: Any]); 
    self.mapView.add(self.heatmap) 
} 

func MKMapPointForCoordinate(_ coordinate: CLLocationCoordinate2D) -> MKMapPoint { 

    return MKMapPointForCoordinate(coordinate); 

} 

// etc ... 

我真的不知道我做错了,任何人都可以帮助我解决这个问题?

+0

'coordinate'看起来像预期的那样? – shallowThought

+0

请参阅http://stackoverflow.com/questions/32454230/convert-mkmappoint-to-nsvalue-in-swift – Sulthan

+0

坐标看起来如预期的那样? –

回答

2

据我可以从DTMHeatmap的原始代码中读取,字典传递给setData的密钥需要为NSValue s,其中包含MKMapPoint。而你所显示的代码并不是一个合适的代码来制作这样的NSValue。 (我真的怀疑你会发现实际工作...原来斯威夫特2码,MKMapView不能在斯威夫特2桥接Objective-C的对象,所以mapPoint as! AnyObject应该总是失败。)

readData方法应该是这样的这样的:

func readData(_ array: [[Double]]){ 
    self.heatmap = DTMHeatmap() 
    var dict: [AnyHashable: Any] = [:] 
    for entry in array{ 

     let coordinate = CLLocationCoordinate2D(latitude: entry[1], longitude: entry[0]); 

     var mapPoint = MKMapPointForCoordinate(coordinate) 

     //Creating `objCType` manually is not recommended, but Swift does not have `@encoding()`... 
     let type = "{MKMapPoint=dd}" 

     let value = NSValue(bytes: &mapPoint, objCType: type) 
     dict[value] = 1 

    } 
    self.heatmap.setData(dict) 
    self.mapView.add(self.heatmap) 
} 

(我还没有与实际DTMHeatmap检查,所以你可能需要一些更多的修复)

+0

感谢参与...我更换了代码,但它仍然没有绘制地图..我什至不能猜测这个实现有什么问题..此外它奇怪,即ios/swift不提供基本的热图库? –

+0

@Creativecrypter,我可以告诉你的一件事是你应该从实际工作的示例代码开始。你不应该依赖一些代码,它甚至不能构建一个简单的'NSValue' ... – OOPer

+0

但是说实话,它是关于“ios上的热图”的唯一资源,所以没有其他的可能性..你认为怎么样? –

0

非常高性能的热图库。我设法得到这个工作,它很好地呈现。我添加了这个答案,因为它还包括渲染器对于需要工作的叠加委托功能。

class HeatmapViewController: UIViewController, MKMapViewDelegate { 

var heatmap: DTMHeatmap? = nil 
var diffHeatmap: DTMDiffHeatmap? = nil 
@IBOutlet weak var mapView: MKMapView! 

override func viewDidLoad() { 
    super.viewDidLoad() 
    var ret: [AnyHashable: Any] = [:] 

    for location in locations { 
     var mapPoint = MKMapPointForCoordinate(location.coordinate) 
     let mapPointValue = NSValue(bytes: &mapPoint, objCType: "{MKMapPoint=dd}") 

     ret[mapPointValue] = 10.0 // weight 
    } 

    self.heatmap = DTMHeatmap() 
    self.heatmap?.setData(ret) 

    self.mapView.delegate = self; // Important 
    self.mapView.add(self.heatmap!) 
    } 
} 

这部分是非常重要的,为了这个工作,你需要将mapView的委托设置为self。

func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer { 
    return DTMHeatmapRenderer(overlay: overlay) 
}