Swift Mapbox 每秒从服务器添加坐标图层
Swift Mapbox add layer with coordinates coming from server every second
我每秒都有来自服务器的坐标,我需要根据此更新在地图上平滑地移动汽车图像。据我所知,它不能使用注释来完成,所以我正在尝试使用层来代替。以下是我尝试创建它们的方法:
var layer = LocationIndicatorLayer(id: vanPosition.userId)
guard let lat = vanPosition.latitude, let lon = vanPosition.longitude else { return }
layer.location = .constant([lat, lon])
let image = UIImage(named: "van-pin")!
layer.topImage = .constant(.name("van-pin"))
layer.source = "some-source"
layer.sourceLayer = nil
try? mapView.mapboxMap.style.addImage(image, id: vanPosition.userId, stretchX: [], stretchY: [])
try? mapView.mapboxMap.style.addLayer(layer, layerPosition: .default)
但是地图上什么也没有显示。我也试过创建这样的来源:
var source = GeoJSONSource()
source.data = .feature(Feature(geometry: .point(Point(vanPosition.coordinate!))))
try! mapView.mapboxMap.style.addSource(source, id: vanPosition.userId)
var layer = LocationIndicatorLayer(id: vanPosition.userId)
guard let lat = vanPosition.latitude, let lon = vanPosition.longitude else { return }
layer.location = .constant([lat, lon])
let image = UIImage(named: "van-pin")!
layer.topImage = .constant(.name("van-pin"))
layer.source = vanPosition.userId
layer.sourceLayer = nil
try? mapView.mapboxMap.style.addImage(image, id: vanPosition.userId, stretchX: [], stretchY: [])
try? mapView.mapboxMap.style.addLayer(layer, layerPosition: .default)
但它也不起作用。图层实际上并没有添加到 mapView,如果我用 mapView.mapboxMap.style.layerExists(withId: vanPosition.userId)
检查什么也没有。感谢您提供的任何帮助,如有需要请向我询问更多信息。
图层未创建,因为我的地图中没有“van-pin”图像 - 如果您想使用自定义图像,您必须手动添加它们。像这样:
try? mapView.mapboxMap.style.addImage(UIImage(named: "van-pin")!, id: "van-pin", stretchX: [], stretchY: [])
而且通过 mapbox 似乎无法实现流畅的动画效果。只需启动计时器并手动更新层的位置。
我每秒都有来自服务器的坐标,我需要根据此更新在地图上平滑地移动汽车图像。据我所知,它不能使用注释来完成,所以我正在尝试使用层来代替。以下是我尝试创建它们的方法:
var layer = LocationIndicatorLayer(id: vanPosition.userId)
guard let lat = vanPosition.latitude, let lon = vanPosition.longitude else { return }
layer.location = .constant([lat, lon])
let image = UIImage(named: "van-pin")!
layer.topImage = .constant(.name("van-pin"))
layer.source = "some-source"
layer.sourceLayer = nil
try? mapView.mapboxMap.style.addImage(image, id: vanPosition.userId, stretchX: [], stretchY: [])
try? mapView.mapboxMap.style.addLayer(layer, layerPosition: .default)
但是地图上什么也没有显示。我也试过创建这样的来源:
var source = GeoJSONSource()
source.data = .feature(Feature(geometry: .point(Point(vanPosition.coordinate!))))
try! mapView.mapboxMap.style.addSource(source, id: vanPosition.userId)
var layer = LocationIndicatorLayer(id: vanPosition.userId)
guard let lat = vanPosition.latitude, let lon = vanPosition.longitude else { return }
layer.location = .constant([lat, lon])
let image = UIImage(named: "van-pin")!
layer.topImage = .constant(.name("van-pin"))
layer.source = vanPosition.userId
layer.sourceLayer = nil
try? mapView.mapboxMap.style.addImage(image, id: vanPosition.userId, stretchX: [], stretchY: [])
try? mapView.mapboxMap.style.addLayer(layer, layerPosition: .default)
但它也不起作用。图层实际上并没有添加到 mapView,如果我用 mapView.mapboxMap.style.layerExists(withId: vanPosition.userId)
检查什么也没有。感谢您提供的任何帮助,如有需要请向我询问更多信息。
图层未创建,因为我的地图中没有“van-pin”图像 - 如果您想使用自定义图像,您必须手动添加它们。像这样:
try? mapView.mapboxMap.style.addImage(UIImage(named: "van-pin")!, id: "van-pin", stretchX: [], stretchY: [])
而且通过 mapbox 似乎无法实现流畅的动画效果。只需启动计时器并手动更新层的位置。