NEW: Learn to build amazing SwiftUI apps for macOS with my new book! >>

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.

1      

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.

1      

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

1      

Hacking with Swift is sponsored by RevenueCat

SPONSORED Spend less time managing in-app purchase infrastructure so you can focus on building your app. RevenueCat gives everything you need to easily implement, manage, and analyze in-app purchases and subscriptions without managing servers or writing backend code.

Get Started

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

Reply to this topic…

You need to create an account or log in to reply.

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.