Map Markers - Markers not appearing

Hi Chris, please can you help me again with this, its driving me crazy, the markers are not appearing on the map. The array is been populated.

Thanks Geoff

//
// ContentView.swift
// South Africa 24-02-2024
//
// Created by Geoff on 24/02/2024.
// Edited 26-02-2024
// Edited 26-02-2024
// Edited 06-03-2024

import SwiftUI
import MapKit

struct ContentView: View {
@State var tags: [String] =
@State var lats: [Double] =
@State var lons: [Double] =

@State var latCount:  Int  = 0
@State var longCount: Int  = 0
@State var tagCount:  Int  = 0

@State var camera: MapCameraPosition = .automatic // change map view here

struct LandMark{  //Previously called MapMarker
    var latitude:  Double
    var longitude: Double
    var tag:       String
               }

@State var mapMarkers = [LandMark]() // Create an array
@State var mytext:      String =   "Working on it"
@State var inbody:      String =   "Now in body"
@State var inOnappear:  String =   "In //onappear"
@State var myText:      String =   "Working"
@State var inpopMarkers: String =  "Now in populate markers"
@State var myBlue:      String  =  "Blue Train"

var body: some View {
    Map()
    VStack{
        Text(inbody)
            .onAppear() 
            { populateMapMarkers()
              printCounters()
                print ("inOnappear")
                //mapMarkers.count
            }
    }
}

func populateMapMarkers() {  //populate the markers array
    print ("inpopMarkers")
    
    mapMarkers.append(LandMark(latitude: -25.757947887867694,
                               longitude: 28.189196654067267,
                               tag: "bluetrain"))
       
    mapMarkers.append(LandMark(latitude: -17.82536423477478,
                               longitude: 25.81926011162876, 
                               tag: "livAir")) // liv air
    
    mapMarkers.append(LandMark(latitude: -25.757947887867694,
                               longitude: 28.189196654067267,
                               tag: "bluetrain"))
    
    mapMarkers.append(LandMark(latitude: -17.82536423477478, //liv air
                               longitude: 25.81926011162876,
                               tag: "livAir"))

    mapMarkers.append(LandMark(latitude: -26.13305962090586, //jo air
                               longitude: 28.23189168563858,
                               tag: "jo_air"))

    mapMarkers.append(LandMark(latitude: -33.971204942427406, //cap_air
                               longitude: 18.598276359810573,
                               tag: "cap_air"))
            
    mapMarkers.append(LandMark(latitude: -26.14440600932509, // Peer
                               longitude: 28.22131032404206,
                               tag: "// Peer"))

    mapMarkers.append(LandMark(latitude: -17.864082081117978, // lvingstone train
                               longitude: 25.854742557680495,
                               tag: "// Liv Train"))

    mapMarkers.append(LandMark(latitude: -17.925316439275523, // vic falls
                               longitude: 25.858513402639517,
                               tag: "// V_Falls"))
    
    mapMarkers.append(LandMark(latitude: -25.185360010673655, // //ivy lodge
                               longitude: 27.1470279923556,
                               tag: "Ivy"))
    
    mapMarkers.append(LandMark(latitude: -25.24023368183164, // Pilan
                               longitude: 27.0759065323271,
                               tag: "// Pilan"))
    
    print("List of array entries below ")
    print("Array: \(mapMarkers)")
    // Add the remainder of the markers in a similar fashion
   }
//var result2 = mapMarkers.count
    //  Print statements

func printCounters()
{
    print (mapMarkers.count)
    //print("tagCount:  \(tagCount)")
}

    }

#Preview {
ContentView()
}

@wonkydonky

Hi Geoff,

Sorry mate, I meant to get back to you on the other thread but have since got sidetracked on other stuff. I think I am burning the candle at both ends.

I played around with getting the MapMarkers to appear on a Map and ended up changing the struct that forms the template for the markers.

Have a look at this code and see if you can understand what I have done.

import SwiftUI
import MapKit

struct MapLocation: Identifiable {
    var id = UUID()
    var name: String
    var coordinate: CLLocationCoordinate2D
}

struct ContentView1: View {

    @State var mytext: String = "Working on it"

    @State var camera: MapCameraPosition = .automatic

    @State var mapMarkers = [MapLocation]()
    @State var region = MKCoordinateRegion(center: CLLocationCoordinate2D(latitude: -25.24023368183164, longitude: 27.0759065323271), span: MKCoordinateSpan(latitudeDelta: 10, longitudeDelta: 10))


    var body: some View {
        VStack{
            Text(mytext)
            Map(initialPosition: camera) {
                ForEach(mapMarkers) { marker in
                    Marker(marker.name, coordinate: marker.coordinate)
                }

            }
        }
        .onAppear() {
            populateMapMarkers()
        }
    }

    func populateMapMarkers() {
        mapMarkers.append(MapLocation(name: "bluetrain", coordinate: CLLocationCoordinate2D(latitude: -25.757947887867694, longitude: 28.189196654067267))) // Blue Train
        mapMarkers.append(MapLocation(name: "livAir", coordinate: CLLocationCoordinate2D(latitude: -17.82536423477478, longitude: 25.81926011162876))) // liv air
        mapMarkers.append(MapLocation(name: "jo_air", coordinate: CLLocationCoordinate2D(latitude: -26.13305962090586, longitude: 28.23189168563858))) // jo_air
        mapMarkers.append(MapLocation(name: "cap_air", coordinate: CLLocationCoordinate2D(latitude: -33.971204942427406, longitude: 18.598276359810573))) // cap_air
        mapMarkers.append(MapLocation(name: "Peer", coordinate: CLLocationCoordinate2D(latitude: -26.14440600932509, longitude: 28.22131032404206))) // Peer
        mapMarkers.append(MapLocation(name: "Liv Train", coordinate: CLLocationCoordinate2D(latitude: -17.864082081117978, longitude: 25.854742557680495))) //lvingstone train
        mapMarkers.append(MapLocation(name: "V_Falls", coordinate: CLLocationCoordinate2D(latitude: -17.925316439275523, longitude: 25.858513402639517))) // vic falls
        mapMarkers.append(MapLocation(name: "Ivy", coordinate: CLLocationCoordinate2D(latitude: -25.185360010673655, longitude: 27.1470279923556))) //ivy lodge
        mapMarkers.append(MapLocation(name: "Pilan", coordinate: CLLocationCoordinate2D( latitude: -25.24023368183164, longitude: 27.0759065323271))) //Pilan

        print("mapMarkers count: \(mapMarkers.count)")
    }
}

#Preview {
    ContentView1()
}

Thank you so much for doing this for me, it’s appreciated.

When I copy the code into Xcode I get the message saying 'Failed to build the scheme’ full report below - cannot find ‘ContentView’ in scope.

Please can you offer guidance?

I can see how you are creating many instances of mapMakers and giving them their own unique identity.

Also, you have added some useful features.

Many thanks, again

WOW, WOW, WOW!!! I just changed ContentView1 to ContentView, and everything sprung into life.

Pretty cool huh?