0

I'm having a problem figuring out how to pass dynamic lat/long values from one view and having it display the annotation. I can set the coordinateRegion but I can't figure out how to get the annotation to display on the map without hardcoding it. Here's what I have currently which after running the map annotation only shows in the hardcoded location. I thought I would be able to update that hard-coded lat/long with my function that runs along with setting the viewable region. So right now the map shows but not the pin, unless I'm actually viewing the location that matches the hardcoded annotation.

import SwiftUI
import SDWebImageSwiftUI
import MapKit
import CoreLocation
import Firebase

struct RestaurantDetail: View {

    var edges = UIApplication.shared.windows.first?.safeAreaInsets

var name: String
var image: String
var category: String
var location: GeoPoint
var latitude: Double
var longitude: Double
var address: String
var city: String
var state: String
var zip: String
var website: String
var phone: String
var delivery: Bool
var takeout: Bool
var sitdown: Bool
var outdoor: Bool



@State private var coordinateRegion = MKCoordinateRegion(center: CLLocationCoordinate2D(latitude: 40.7, longitude: -73.9), span: MKCoordinateSpan(latitudeDelta: 0.001, longitudeDelta: 0.001))

private let places: [Spot] = [
    Spot(name: "Test", coordinate: CLLocationCoordinate2D(latitude: 28.552811, longitude: -81.503676))
]

var body: some View {
    
    ScrollView(.vertical, showsIndicators: false) {
        VStack {
            HStack {
                WebImage(url: URL(string: image))
                    .onSuccess { image, data, cacheType in
                    }
                    .resizable()
                    
                    .placeholder(Image(systemName: "photo")) // Placeholder Image
                    // Supports ViewBuilder as well
                    .placeholder {
                        Rectangle().foregroundColor(.gray)
                    }
                    .indicator(.activity) // Activity Indicator
                    .transition(.fade(duration: 0.5))
                    .frame(height: 270, alignment: .top)
                    
                    .aspectRatio(contentMode: .fit)
                    .clipped()
            }.onAppear(perform: getCoordinates)
            VStack {
                HStack {
                    Text(name)
                        .font(.title2)
                        .fontWeight(.bold)
                    Spacer()
                }
                .padding(.top)
                HStack {
                    Text(address)
                        .font(.callout)
                        .foregroundColor(Color("Subtext"))
                    Spacer()
                }
                HStack {
                    Text(city)
                        .font(.callout)
                        .foregroundColor(Color("Subtext"))
                        + Text(", \(state)")
                        .font(.callout)
                        .foregroundColor(Color("Subtext"))
                        + Text(", \(zip)")
                        .font(.callout)
                        .foregroundColor(Color("Subtext"))
                    Spacer()
                }
          
                VStack {
                    Divider()
                        .padding(.top)
                    HStack {
                        if sitdown == true {
                            Image(systemName: "checkmark")
                                .font(.footnote)
                                .foregroundColor(Color.green)
                        } else {
                            Image(systemName: "xmark")
                                .font(.footnote)
                                .foregroundColor(Color.red)
                        }
                        Text("Dine-in")
                            .font(.footnote)
                            .padding(.trailing, 8)
                        Text("· ")
                            .font(.footnote)
                        if takeout == true {
                            Image(systemName: "checkmark")
                                .font(.footnote)
                                .foregroundColor(Color.green)
                        } else {
                            Image(systemName: "xmark")
                                .font(.footnote)
                                .foregroundColor(Color.red)
                        }
                        Text("Takeout")
                            .font(.footnote)
                            .padding(.trailing, 8)
                        Text("· ")
                            .font(.footnote)
                        if delivery == true {
                            Image(systemName: "checkmark")
                                .font(.footnote)
                                .foregroundColor(Color.green)
                        } else {
                            Image(systemName: "xmark")
                                .font(.footnote)
                                .foregroundColor(Color.red)
                        }
                        Text("Delivery")
                            .font(.footnote)
                        Spacer()
                    }.padding(.vertical, 5)
                    
                    //                            HStack {
                    //                                if outdoor == true {
                    //                                    Image(systemName: "checkmark")
                    //                                    } else {
                    //                                        Image(systemName: "xmark")
                    //                                    }
                    //                                Text("Outdoor Seating")
                    //                                    .font(.footnote)
                    //                                    .padding(.top, 8)
                    //                            }.padding(.bottom)
                    
                    Divider()
                        .padding(.bottom)
                }
               // MARK: Map View
                VStack {

                    
                        Map(coordinateRegion: $coordinateRegion, annotationItems: places, annotationContent: { place in MapPin(coordinate: place.coordinate)})

                }.frame(height: 230)
                
                VStack {
                    HStack {
                        Text(category)
                        Spacer()
                    }
                    HStack {
                        if website == "NA" {
                            Text("")
                                .frame(height: 0)
                        } else {
                            Image(systemName: "link.circle.fill")
                            Link(website, destination: URL(string: "http://\(website)")!)
                            //Text(website)
                        }
                        Spacer()
                    }.padding(.top)
                    HStack {
                        if phone == "0" {
                            Text("Phone number not available.")
                        } else {
                            Image(systemName: "phone.circle.fill")
                            Link(phone, destination: URL(string: "tel:\(phone)")!)
                        }
                        // Text(phone)
                        Spacer()
                    }.padding(.top)
                }
            }.padding(.horizontal)
            
            Spacer()
        }
        
        
        
    }.ignoresSafeArea(.all)
    
    
    
}

func getCoordinates() {
   // print(latitude)
    
    coordinateRegion.center.latitude = latitude
    coordinateRegion.center.longitude = longitude
    //Spot(name: name, coordinate: CLLocationCoordinate2D(latitude: latitude, longitude: longitude))
    Spot.init(name: name, coordinate: CLLocationCoordinate2D(latitude: latitude, longitude: longitude))
    print(Spot.init(name: name, coordinate: CLLocationCoordinate2D(latitude: latitude, longitude: longitude))

    
       // print (coordinateRegion.center)
    }
}


struct Spot: Identifiable {
    let id = UUID()
    var name: String
    var coordinate: CLLocationCoordinate2D
}
Jason Tremain
  • 1,259
  • 3
  • 20
  • 35
  • 1
    There's a lot of code here. It would help us if you could make a minimum reproducible example (ie. If I copy and paste this into Xcode my program wouldn't build bc I don't have all of your pods and such.) Anyway, I'm just taking a guess here but maybe in getCoordinates, instead of just changing the coordinateRegion.center...., change the entire variable to a new instance of MKCoordinateRegion. ? – nicksarno Nov 09 '20 at 22:54
  • Does this answer your question https://stackoverflow.com/questions/62543034/swiftui-map-display-annotations? – Asperi Nov 10 '20 at 04:42

0 Answers0