UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

Picker Containing City Names That Contain Coordinates

Forums > SwiftUI

So I am looking to make a picker which has cities for example: New York, Toronto etc. and the user will choose his city and when he chooses it will change the coordinates.

var coordinates = Coordinates(latitude: (Latitude depending on city chosen as double), longitude: (Longitude depending on city chosen as double)

I am a bit new to SwiftUI so I don't understand how to do this.

3      

You created your account the same day you asked this question. So, Welcome to Hacking With Swift forums!

In the header of each page in HackingWithSwift is a search bar. Did you search for "picker" using the search bar? @twostraws has several articles and videos where he explains (using great examples!) how to construct and use pickers. Let us know which articles helped you, or which parts need more clarification.

This may not be a SwiftUI problem. But we're glad to help you think through this problem.

Ask yourself, "Self? What do I want my user to see in the picker?"

You answered "City Names".

Then ask yourself, "What do I need when the user selects a city from the picker?"

You answered: "Latitude and Longitude"

Now you need to think about your model. HOW will you model this so that you have data to show your users and also have data to use in your application?

You should think along the lines of, "What Swift programming structure can help me organize a bunch of similar data?" Well the answer will soon be very familiar to you. It's a structure!

struct City: Identifiable {
    let id = UUID() // useful for common SwiftUI display
    let city: String  // Toronto, Naples, Berlin, etc.
    let latitude: String  // What's the best format to
    let longitude: String // store latitude and longitude?
    let description: String // maybe a few sentences?
}

You'll need to create a structure for each city in your application. Perhaps you get the data from an internet source? Or it might be a file in your application? You'll use the structure's name property to display in the picker list. When the user selects from the picker, they are selecting a structure, not a text field. Then you can unpack the latitude and longitude from the structure and use the data elsewhere in your application.

Watch a few of @twostraw's videos on structures and pickers. Let us know which ones helped, or which ones confused you.

3      

Thank you very much for your help this is what i got:

import SwiftUI

struct CityChoose: View {
    @State private var selectedCity = "Toronto"
    let Cities = ["Toronto", "New York", "London"]

  var body: some View {
      NavigationView {
          Form {
              Section {
                  VStack {
                      Picker("Please Choose Your City", selection: $selectedCity) {
                          ForEach(Cities, id: \.self) {
                              Text("\($0)")
                          }
                      }
                  }
              }
          }
          .navigationTitle("Pick Your City")
      }
  }
}

struct Toronto: Identifiable {
    let id = UUID()
    let city: String
    let latitude: Double
    let longitude: Double
    let description: String
}

struct NewYork: Identifiable {
    let id = UUID()
    let city: String
    let latitude: Double
    let longitude: Double
    let description: String
}

struct London: Identifiable {
    let id = UUID()
    let city: String
    let latitude: Double
    let longitude: Double
    let description: String
}

struct CityChoose_Previews: PreviewProvider {
    static var previews: some View {
        CityChoose()
    }
}

The picker tutorial was very helpful. But i dont understand how to include the struct in the Picker it gives me an error. Also by the latitude and longitude how will i enter them in the coordinates code

var coordinates = Coordinates(latitude: (Latitude depending on city chosen as double), longitude: (Longitude depending on city chosen as double)

Again thank you very much for your help

3      

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.