I'm trying to plot out map annotations from a JSON Response using MapKit. I've figured out some basic ways to plot out map points, but I"m not sure entirely what MapKit is expecting for map points. In my JSON response my struct is
Then my code looks like this, but I think I'm using CLLocationCoordinate2DMake incorrectly.
I tried using coordinate: places.coordinates,, but I get the following compiler error
Cannot convert value of type 'Center' to expected argument type 'CLLocationCoordinate2D'
Code Block // MARK: - Business struct Business: Codable, Identifiable { let id, alias, name: String let coordinates: Center } struct Center: Codable { let latitude, longitude: Double }
Then my code looks like this, but I think I'm using CLLocationCoordinate2DMake incorrectly.
Code Block @State var yelpbusinessdataMap: WelcomeBusiness? = nil var body: some View { Map(coordinateRegion: $region, interactionModes: MapInteractionModes.all, showsUserLocation: true, userTrackingMode: $userTrackingMode, annotationItems: yelpbusinessdataMap?.businesses ?? [] ) { places in var new2DCoord = CLLocationCoordinate2DMake(places.coordinates.latitude, places.coordinates.longitude) MapAnnotation( coordinate: new2DCoord, anchorPoint: CGPoint(x: 0.5, y: 0.5) ){ Circle() .stroke(Color.green) .frame(width: 44, height: 44) } }
I tried using coordinate: places.coordinates,, but I get the following compiler error
Cannot convert value of type 'Center' to expected argument type 'CLLocationCoordinate2D'
You should better clarify the reason why you think so.but I think I'm using CLLocationCoordinate2DMake incorrectly.
You are not showing many things in your code.
WelcomeBusiness
$region
$userTrackingMode
I assume you get error like Unable to infer complex closure return type; add explicit type to disambiguate on the line { places in.
You need to specify the return type of the closure as you have a local var (it should be let, but that's another thing).
But in SwiftUI, it is hard to specify the return type explicitly because there are so many opaque types are used.
To avoid this, you should better try to simplify the closure as to contain only one expression and no other statements or declarations.
Declare an extension some where in your project (not in any other types):
Code Block extension Center { var coordinate2D: CLLocationCoordinate2D { return CLLocationCoordinate2DMake(self.latitude, self.longitude) } }
And use it like this:
Code Block var body: some View { Map(coordinateRegion: $region, interactionModes: MapInteractionModes.all, showsUserLocation: true, userTrackingMode: $userTrackingMode, annotationItems: yelpbusinessdataMap?.businesses ?? [] ) { business in MapAnnotation( coordinate: business.coordinates.coordinate2D, //<- anchorPoint: CGPoint(x: 0.5, y: 0.5) ){ Circle() .stroke(Color.green) .frame(width: 44, height: 44) } } }
If this does not work in your project, you may need to show more code.