mapbox / mapbox-directions-swift

Traffic-aware directions and map matching in Swift on iOS, macOS, tvOS, watchOS, and Linux
https://www.mapbox.com/navigation/
ISC License
179 stars 87 forks source link

Isochrone API #296

Closed 1ec5 closed 2 years ago

1ec5 commented 5 years ago

The Isochrone API requires a new top-level class alongside Directions. Some of the parameters and many of the response properties are visual in nature, reminiscent of MapboxStatic.swift or the map SDK rather than the APIs currently supported by this library, but I don’t see a need to break it out of this library for the time being as long as colors are only specified as hexadecimal triplets.

/cc @ericdeveloper

1ec5 commented 5 years ago

In the meantime, as a workaround, developers can use NSURLSession to connect to the API directly. The response format is suitable for passing into the map SDK’s MGLShape(data:encoding:error:) to more easily create an MGLShapeSource, and the various feature properties can be used as attributes in paint and layout property expressions. For example, MGLLineStyleLayer.lineColor can be set to NSExpression(format: "CAST(mgl_join({'#', fillColor}), 'UIColor')").

1ec5 commented 2 years ago

The response format is suitable for passing into the map SDK’s MGLShape(data:encoding:error:) to more easily create an MGLShapeSource, and the various feature properties can be used as attributes in paint and layout property expressions. For example, MGLLineStyleLayer.lineColor can be set to NSExpression(format: "CAST(mgl_join({'#', fillColor}), 'UIColor')").

It’s more verbose with map SDK v10 but still possible:

URLSession.shared.dataTask(with: request) { (possibleData, possibleResponse, possibleError) in
    guard let data = possibleData,
          let geoJSON = try? JSONDecoder().decode(GeoJSONObject.self, data) else { return }
    try? style.updateGeoJSONSource(withId: "isochrone", geoJSON: geoJSON)

    var layer = LineLayer(id: "isochrone")
    layer.source = "isochrone"
    layer.lineColor = .expression(Exp(.color) {
        Exp(.concat) {
            "#"
            Exp(.get) { "fillColor" }
        }
    })
    try? style.addLayer(layer)
}