简体   繁体   English

如何在iOS应用中的Mapbox中将GeoJSON字典显示为热图

[英]How do I display a GeoJSON dictionary as a heat map in Mapbox in iOS app

I have an iOS app in swift that is successfully displaying a map using the Mapbox iOS library. 我在swift中有一个iOS应用程序,它使用Mapbox iOS库成功显示地图。

I also have a some GeoJSON data, as a dictionary, which I have pulled from a Redis database. 我还有一些GeoJSON数据,作为字典,我从Redis数据库中提取。 I have successfully printed this data in the Xcode console after pulling it and it looks like this: 我拉动后在Xcode控制台中成功打印了这些数据,它看起来像这样:

Optional(["city": chicago, "data": {
    features =     (
                {
            geometry =             {
                coordinates =                 (
                    "-87.86810699999999",
                    "41.966483"
                );
                type = Point;
            };
            properties =             {
                weight = 1;
            };
            type = Feature;
        },
                {
            geometry =             {
                coordinates =                 (
                    "-87.866905",
                    "41.96288"
                );
                type = Point;
            };
            properties =             {
                weight = 3;
            };
            type = Feature;
        },
/*and so on with many more point features...*/

The line that converts the raw array returned from the Redis query into the above dictionary in the code is the following: 将从Redis查询返回的原始数组转换为代码中的上述字典的行如下:

let geojson_dict = (message[0] as! String).convertToDictionary()

I want to now put this GeoJson data over my MapBox map view defined in the code as: 我现在想把这个GeoJson数据放在代码中定义的MapBox地图视图中:

var mapBoxView: MGLMapView?

at the point I have the GeoJson data the mapBoxView is added as a view and visible. 在我有GeoJson数据的点上, mapBoxView被添加为视图并可见。

This example touches on how to do this: 这个例子涉及如何做到这一点:

https://docs.mapbox.com/ios/maps/examples/heatmap-example/ https://docs.mapbox.com/ios/maps/examples/heatmap-example/

but the GeoJson data has a different structure and it is not dealing with a dictionary in memory but rather pulling the GeoJson from a url. 但是GeoJson数据具有不同的结构,它不处理内存中的字典,而是从URL中提取GeoJson This coupled with the example being poorly documented/commented makes it hard to adapt to my particular use case. 这与文档记录不良/注释的示例相结合,使得很难适应我的特定用例。

I tried the following: 我尝试了以下方法:

let feature = try! MGLShape(data: geojson_dict as Data, encoding: String.Encoding.utf8.rawValue) as! MGLShapeCollectionFeature

but this doesn't seem to want to work with geojson_dict being a dictionary, nor would it add a heat map to the mapbox view. 但这似乎不想使用geojson_dict作为字典,也不会将热图添加到地图mapbox视图中。

Your geojson_dict["data"] is your GeoJson dictionary. 你的geojson_dict [“data”]是你的GeoJson字典。 I suppose you already instantiate your mapboxView and added to your viewController (very much like MapBox's Heatmap sample ), so here is how you may instantiate your MGLShapeSource. 我想你已经实例化了你的mapboxView并添加到你的viewController(非常像MapBox的Heatmap示例 ),所以这里是你如何实例化你的MGLShapeSource。

func mapView(_ mapView: MGLMapView, didFinishLoading style: MGLStyle) {

  guard let dict = geojson_dict else {
      return
  }

  var source : MGLShapeSource

  do {
    let json = try JSONSerialization.data(withJSONObject: dict[“data"], options: JSONSerialization.WritingOptions())
    let shape = try MGLShape(data: json, encoding: String.Encoding.utf8.rawValue)
    source = MGLShapeSource(identifier: dict[“city”], shape: shape, options: nil)
    mapView.style.addSource(source)

  } catch {

    // handle any exceptions here

  }

  // Continue defining your heat layer as on Mapbox sample
  …

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM