Значок множественной некластеризованной функции Mapbox в iOS - PullRequest
0 голосов
/ 05 марта 2020

В iOS Я создал кластерные значки, но когда я попытался распространить их, чтобы увидеть все некластерные функции, я не могу установить несколько изображений для этого. Есть ли способ установить несколько значков для некластеризованных функций? Ниже мой код:

let onlineCarImage = UIImage(named: "map_car_icon")
let offlineCarImage = UIImage(named: "map_Jeep_car_icon")

for asset in self.assetArray {

      let time_difference = self.timeDifference(timestamp: "\(asset.timestamp!)")
      let time_hour = time_difference / 60
      let time_minute = (time_difference - time_hour * 60)
      print("hour...\(time_hour)")
                    print("minute...\(time_minute)")
                    var status : String = ""
                    if time_hour>0{
                        status = "offline"
                    }else if (time_hour==0 && time_minute>=2) {
                        status = "offline"
                    }else{
                        status = "online"
                    }

                    let speed = asset.properties?.speed ?? 0.0
                    let lat = asset.properties?.lat ?? 0.0
                    let long = asset.properties?.long ?? 0.0
                    let odometer = asset.properties?.odometer ?? 0.0
                    let batteryVoltage = asset.properties?.batteryVoltage ?? 0.0
                    let gpsUncertainty = asset.properties?.gpsUncertainty ?? 0
                    let gpsNrSat = asset.properties?.gpsNrSat ?? 0
                    let gpsMode = asset.properties?.gpsMode ?? 0
                    let firmwareVersion = asset.properties?.firmwareVersion ?? ""
                    let gsmSignalStrength = asset.properties?.gsmSignalStrength ?? 0
                    let assignedDrivers = asset.properties?.assignedDrivers ?? ""
                    let engine = asset.properties?.engine ?? ""
                    let fuelType = asset.properties?.fuelType ?? ""
                    let imagePath = asset.properties?.imagePath ?? ""
                    let manufacturer = asset.properties?.manufacturer ?? ""
                    let model = asset.properties?.model ?? ""
                    let vin = asset.properties?.vin ?? ""
                    let jsonObject: [String:Any]  = [
                        "speed": speed,
                        "lat": lat,
                        "long": long,
                        "odometer": odometer,
                        "batteryVoltage": batteryVoltage,
                        "gpsUncertainty": gpsUncertainty,
                        "gpsNrSat": gpsNrSat,
                        "gpsMode": gpsMode,
                        "firmwareVersion": firmwareVersion,
                        "gsmSignalStrength": gsmSignalStrength,
                        "assignedDrivers": assignedDrivers,
                        "engine": engine,
                        "fuelType": fuelType,
                        "imagePath": imagePath,
                        "manufacturer": manufacturer,
                        "model": model,
                        "vin": vin,
                        "driverStatus": status
                    ]

                    let feature = MGLPointFeature()
                    feature.coordinate = CLLocationCoordinate2D(latitude: lat, longitude: long)
                    feature.attributes = jsonObject
                    self.carsArray.append(feature)
                }
                let source = MGLShapeSource(identifier: "clusteredPorts", features: self.carsArray as! [MGLShape & MGLFeature], options: [.clustered: true, .clusterRadius: self.icon.size.width])
                style.addSource(source)


                //style.setImage(offlineCarImage!, forName: "icon_offline")

                // Show unclustered features as icons. The `cluster` attribute is built into clustering-enabled
                // source features.

                for item in self.carsArray {
                    let status = item.attributes["driverStatus"] as! String
                    print("status... \(status)")
                    if status == "online" {
                        style.setImage(onlineCarImage!, forName: "icon_online")
                    }else{
                        style.setImage(offlineCarImage!, forName: "icon_online")
                    }
                }

                let ports = MGLSymbolStyleLayer(identifier: "ports", source: source)
                ***ports.iconImageName = NSExpression(forConstantValue: "icon_online")***
                ports.iconColor = NSExpression(forConstantValue: UIColor.darkGray.withAlphaComponent(0.9))
                ports.predicate = NSPredicate(format: "cluster != YES")
                ports.iconAllowsOverlap = NSExpression(forConstantValue: true)
                style.addLayer(ports)



                // Color clustered features based on clustered point counts.
                let stops = [
                    20: UIColor.lightGray,
                    50: UIColor.orange,
                    100: UIColor.red,
                    200: UIColor.purple
                ]

                // Show clustered features as circles. The `point_count` attribute is built into
                // clustering-enabled source features.
                let circlesLayer = MGLCircleStyleLayer(identifier: "clusteredPorts", source: source)
                circlesLayer.circleRadius = NSExpression(forConstantValue: NSNumber(value: Double(self.icon.size.width) / 2))
                circlesLayer.circleOpacity = NSExpression(forConstantValue: 0.75)
                circlesLayer.circleStrokeColor = NSExpression(forConstantValue: UIColor.white.withAlphaComponent(0.75))
                circlesLayer.circleStrokeWidth = NSExpression(forConstantValue: 2)
                circlesLayer.circleColor = NSExpression(format: "mgl_step:from:stops:(point_count, %@, %@)", UIColor.darkGray, stops)
                circlesLayer.predicate = NSPredicate(format: "cluster == YES")
                style.addLayer(circlesLayer)

                // Label cluster circles with a layer of text indicating feature count. The value for
                // `point_count` is an integer. In order to use that value for the
                // `MGLSymbolStyleLayer.text` property, cast it as a string.
                let numbersLayer = MGLSymbolStyleLayer(identifier: "clusteredPortsNumbers", source: source)
                numbersLayer.textColor = NSExpression(forConstantValue: UIColor.white)
                numbersLayer.textFontSize = NSExpression(forConstantValue: NSNumber(value: Double(self.icon.size.width) / 2))
                numbersLayer.iconAllowsOverlap = NSExpression(forConstantValue: true)
                numbersLayer.text = NSExpression(format: "CAST(point_count, 'NSString')")

                numbersLayer.predicate = NSPredicate(format: "cluster == YES")
                style.addLayer(numbersLayer)
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...