Skip to content

api.getNearbyChargingSites mismatch #143

@NewTeslaUser

Description

@NewTeslaUser

Not sure if I am doing something wrong here, or if this is a common fault.

The image on the left side is the Main Tesla App, and the image on the right it my app(look at the bottom of this post). The names are correct, but the available stalls are not correct. Any ideas what might be wrong here?

The code below is the code I am using, and on the bottom is the image:


func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return chargerAnnotations.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! MapTableViewCell
    
    cell.titleLabel.text = nameArray[indexPath.row]
    cell.titleLabel.frame.size.width = cell.titleLabel.intrinsicContentSize.width
    
    let aStalls = availablestallsArray[indexPath.row]
    let tStalls = totalstallsArray[indexPath.row]
    
    cell.availableLabel.text = "\(aStalls) / \(tStalls) available"
    cell.availableLabel.frame.size.width = cell.availableLabel.intrinsicContentSize.width
    
    let formattedDistance = String(format: "%.0f", distanceArray[indexPath.row].inKilometersTest())
    cell.distanceLabel.text = "\(formattedDistance) km"
    
    return cell
}

var nameArray = [String]()
var latitudeArray = [Double]()
var longitudeArray = [Double]()
var distanceArray = [Double]()
var totalstallsArray = [Int]()
var availablestallsArray = [Int]()

var chargerAnnotations = [MKAnnotation]()
var annotations = [MKAnnotation]()

var currentDistance = Float()
var currentTotalStalls = Int()
var currentAvailableStalls = Int()


func getNearbyChargingSites() {
    Task { @MainActor in
        if let vehicle = GlobalSelectedVehicle {
            do {
                let nearbyChargingSites = try await api.getNearbyChargingSites(vehicle)
                print("nearbyChargingSites11: \(nearbyChargingSites.jsonString!)")
                
                if let dataFromString = nearbyChargingSites.jsonString!.data(using: .utf8, allowLossyConversion: false) {
                    let json = try! JSON(data: dataFromString,options: .allowFragments)
                    
                    for supercharger in json["superchargers"].arrayValue {
                        let jsonName = supercharger["name"].stringValue
                        let jsonDistance = supercharger["distance_miles"].doubleValue
                        let jsonTotalStalls = supercharger["total_stalls"].intValue
                        let jsonAvailableStalls = supercharger["available_stalls"].intValue
                        let jsonLat = supercharger["location"]["lat"].doubleValue
                        let jsonLong = supercharger["location"]["long"].doubleValue
                        
                        self.nameArray.append(jsonName)
                        self.totalstallsArray.append(jsonTotalStalls)
                        self.availablestallsArray.append(jsonAvailableStalls)
                        self.distanceArray.append(jsonDistance)
                        self.latitudeArray.append(jsonLat)
                        self.longitudeArray.append(jsonLong)
                        
                        let annotation = MKPointAnnotation()
                        annotation.title = jsonName
                        annotation.coordinate = CLLocationCoordinate2D(latitude: jsonLat, longitude: jsonLong)
                        self.annotations.append(annotation)
                        self.chargerAnnotations.append(annotation)
                    }
                    self.mapView.addAnnotations(self.annotations)
                    self.mapView.fitAll(in: self.annotations, andShow: true)
                    
                    self.tableView.reloadData()
                }
                
            } catch {
                // Handle the error here if the API call fails.
                // Error: Vehicle Unavailable
                
                print("Error: \(error.localizedDescription)")
            }
        }
    }
}
image

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions