Roman H
Roman H

Reputation: 251

How do I get the latitude and longitude out of my array structure and create a marker?

I am new to swift and currently trying to let my app display markers in my Google Maps view. The latitude and the longitude are saved in an array structure. For some reason I am unable to get the data out of my array. So my main questions are why is it not working and how will it work?

Besides that I wondered if setting the struct elements to a CLLocation type would work?

struct pickerStruct 
{
var lat: Double
var long: Double
}

func showPickers() {

    //For every pickerStruct in pickers create a picker
    //The marker should have the in pickers saved latitude and longitude

    for pickerStruct in pickers {

        let marker = GMSMarker()
        marker.position = CLLocationCoordinate2D(latitude: pickers.lat, longitude: pickers.long)
        marker.map = mapView

    }

}

The Error I get says: "Value of type '[pickerStruct]' has no member 'lat'"

Where pickers comes from:

Pickers is a array which gets data from my Firebase Database.

    ref = Database.database().reference()

    ref.child("locations").observe(.childAdded, with: { snapshot in

        let newLat = snapshot.value as? NSDictionary
        let lat:Double = (newLat?["lat"] as? Double)!

        let newLong = snapshot.value as? NSDictionary
        let long:Double = (newLong?["long"] as? Double)!


        self.pickers.append(pickerStruct(lat: lat, long: long)) 
})

Upvotes: 1

Views: 266

Answers (2)

Teetz
Teetz

Reputation: 3765

Like Leo Dabus mentioned in the comments, your showPickers-function should look like this: (You were using the array pickers and not one element pickerstruct)

func showPickers() {

        //For every pickerStruct in pickers create a picker
        //The marker should have the in pickers saved latitude and longitude

        for pickerStruct in pickers {

            let marker = GMSMarker()
            marker.position = CLLocationCoordinate2D(latitude: pickerStruct.lat, longitude: pickerStruct.long)
            marker.map = mapView

        }

}

Upvotes: 3

Leo Dabus
Leo Dabus

Reputation: 236260

The issue there is that you are trying to access the array lat property when you should access the element property.

for picker in pickers {
    let marker = GMSMarker()
    marker.position = CLLocationCoordinate2D(latitude: picker.lat, longitude: picker.long)
    marker.map = mapView
}

Upvotes: 3

Related Questions