Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

api.getNearbyChargingSites mismatch #143

Open
NewTeslaUser opened this issue Jul 31, 2023 · 3 comments
Open

api.getNearbyChargingSites mismatch #143

NewTeslaUser opened this issue Jul 31, 2023 · 3 comments

Comments

@NewTeslaUser
Copy link

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
@jonasman
Copy link
Owner

were you able to fix this?

@NewTeslaUser
Copy link
Author

were you able to fix this?

No, I was not.

@jonasman
Copy link
Owner

jonasman commented Mar 7, 2024

i checked the response and im not even getting the availableStalls data.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants