I am trying to adjust the extent of map in the Map() statement. This is for a golf app, and depending on the hole location I want to limit the map extent for that particular hole. The current code covers the entire golf course but as soon as I add the coordinateRegion to Map(), I get an error message…I am at a loss on what I am doing wrong…
Here is the code that works.. however as soon as I change “Map()” to “Map(coordinateRegion: $mapRegion)”, I get an error message “Contextual type for closure argument list expects 1 argument, which cannot be implicitly ignored”
struct ContentView: View {
@State var current_hole: Int = 0
@StateObject var locationManager = LocationManager()
@State var mapRegion = MKCoordinateRegion( center:CLLocationCoordinate2D(latitude: 29.65, longitude: -95.9333), span: MKCoordinateSpan(latitudeDelta: 0.03, longitudeDelta: 0.03))
var userLatitude: String {
return "\(locationManager.lastLocation?.coordinate.latitude ?? 0)"
}
var userLongitude: String {
return "\(locationManager.lastLocation?.coordinate.longitude ?? 0)"
}
var body: some View {
Map(){ // error if changed to Map(coordinaterRegion: $mapRegion)
Marker("", coordinate: wf_pin_coordinates[0])
Marker("", coordinate: wf_pin_coordinates[1])
Marker("", coordinate: wf_pin_coordinates[2])
//... and so on
}.mapStyle(.hybrid)
HStack {
Button(("Hole "+String(current_hole+1))) {
current_hole = current_hole + 1
if ( current_hole > 17 ) {
current_hole = 0
}
}
Text(hole_description[current_hole]).fontWeight(.bold)
}
}
}
#Preview {
ContentView()
}
There are some changes in the most recent version of the Map
struct.
Replace
@State var mapRegion = MKCoordinateRegion( center:CLLocationCoordinate2D(latitude: 29.65, longitude: -95.9333), span: MKCoordinateSpan(latitudeDelta: 0.03, longitudeDelta: 0.03))
with
@State private var cameraPosition = MapCameraPosition.region(
MKCoordinateRegion(center: CLLocationCoordinate2D(latitude: 29.65, longitude: -95.9333),
span: MKCoordinateSpan(latitudeDelta: 0.03, longitudeDelta: 0.03)))
and
Map() {
with
Map(position: $cameraPosition) {
The default position of the most recent version of
Map
is based on aMapCameraPosition
, the related init method isinit(position:bounds:interactionModes:scope:content:)
stackoverflow.com/questions/71698728/…
Thanks Vadian.. still not sure how I change to extent though.