ios - 在 MapBox Android 和 iOS 上显示 GeoJSON

标签 ios swift mapbox geojson

我使用 mapbox 在 iOS 和 Android 应用程序上显示 geojson。

同一个 geojson 在两个平台上的显示不同。我认为某些形状在 iOS 上会隐藏其他形状。

mapbox ios sdk 上没有 MGLGeoJSONSource,看起来他们将其删除了。所以要达到同样的结果要花很多时间。

也许有人有更好的打点方法?

这是我的java代码:

JSONArray features = json.getJSONArray("features");
for (int i = 0; i < features.length(); i++) {
    JSONObject feature = features.getJSONObject(i);

    GeoJsonSource geoJsonSource = new GeoJsonSource(i, feature.toString());

    JSONObject properties = feature.getJSONObject("properties");
    int rounded_value = (int)Math.round(properties.getDouble("value"););

    list_value_geojson.add(new AbstractMap.SimpleEntry<>(rounded_value, geoJsonSource));
}

for (int i=0; i < list_value_geojson.size(); i++){
    Map.Entry<Integer, GeoJsonSource> entry = list_value_geojson.get(i);

    mapboxMap.addSource(entry.getValue());

    FillLayer fillLayer = new FillLayer(entry.getValue().getId(), entry.getValue().getId());
    fillLayer.setProperties(PropertyFactory.fillColor(Color.parseColor(hashMapColors.get(entry.getKey()))));

    mapboxMap.addLayer(fillLayer);
}

这是我的 Swift 代码:

if let features = jsonDict["features"] as? NSArray {
    var sourceIndex = 0
    for feature in features {
        if let feature = feature as? NSDictionary {
            if let geometry = feature["geometry"] as? NSDictionary {
                let coordinates_array_of_array = geometry["coordinates"] as! NSArray
                for coordinates_array in coordinates_array_of_array{

                    var coordinates_collection : [CLLocationCoordinate2D] = []

                    let locations = coordinates_array as? [[Double]]

                    for location in locations! {
                        // Make a CLLocationCoordinate2D with the lat, lng
                        let coordinate = CLLocationCoordinate2D(latitude: location[1], longitude: location[0])

                        // Add coordinate to coordinates array
                        coordinates_collection.append(coordinate)
                    }

                    if let properties = feature["properties"] as? NSDictionary {
                        let mglpf = MGLPolygonFeature(coordinates: coordinates_collection, count: UInt(coordinates_collection.count))


                        mglpf.title = String(properties["value"] as! Int32)

                        mglpf.attributes = ["color":getColorFromValue(value: mglpf.title!)]

                        let source = MGLShapeSource(identifier: "shapeSource"+String(sourceIndex), features: [mglpf], options: nil)

                        mapbox?.style?.addSource(source)
                        sourceIndex = sourceIndex + 1

                        let layer = MGLFillStyleLayer(identifier: "layer"+String(sourceIndex), source: source)
                        layer.fillColor = MGLStyleValue<UIColor>(rawValue: Util.hexStringToUIColor(hex: getColorFromValue(value: mglpf.title!)))
                        mapbox?.style?.addLayer(layer)
                    }

                }
            }
        }
    }
}

iOS iOS map 框

Android 安卓 map 箱

最佳答案

一种选择是使用 MGLShapeSource 来 work with GeoJSON data .然后,您可以将其用作 MGLFillStyleLayer 的来源。

您可能还想研究使用在最新的 Android 和 iOS SDK 测试版中引入的数据驱动样式。对于 iOS,您可以将 MGLSourceStyleFunctionfillColor on your MGLFillStyleLayer 一起使用根据特征属性设置样式。

关于ios - 在 MapBox Android 和 iOS 上显示 GeoJSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42651710/

相关文章:

swift - 使用 sqlite.swift 读/写数据库路径

android - MapBox android中的标记方向

ios - 尝试抛出错误,但为什么呢?

ios - iTunes 连接上没有下载 dSYM 选项

ios - 读取 NSFetchedResultsController 的 fetchedObjects 属性时出现 EXC_BAD_ACCESS 错误(仅在模拟器上)

ios - 仅支持设备的 SDK

ios - 如何使 UITextField 从两个类继承?

ios - 子类化 NSInputStream,重写委托(delegate)?

ios - Mapbox SDK离线 map 下载

ios - 如何限制 RMMapView 中的 map 边界和缩放级别