This is the code snippet for the map that I am using:
Map(
coordinateRegion: $locationVM.region,
interactionModes: MapInteractionModes.all,
showsUserLocation: true,
userTrackingMode: nil,
annotationItems: locationVM.cityLocationCoordinates,
annotationContent: { location in
MapAnnotation(
coordinate: location.coordinate,
content: {
StoreMapPinView(storeName:location.name, locationVM:locationVM)
.scaleEffect("(location.coordinate.latitude)" == locationVM.clickedStore?.latitude /locationVM.storeList?[locationVM.visibleStoreIndex].latitude ?? ""/ && "(location.coordinate.longitude)" == /locationVM.storeList?[locationVM.visibleStoreIndex].longitude ?? ""/locationVM.clickedStore?.longitude ? 1 : 0.5)
.onTapGesture {
locationVM.setVisibleStoreIndex(isScrollList: true, tappedLocation: location)
proxy.scrollTo(locationVM.visibleStoreIndex, anchor: .top)
}
// setMapPin(coordinate: location.coordinate)
}
)
}
)
.animation((isAppOpening ? .none : .linear), value: locationVM.region)
.accentColor(Color.blue)
and the function that using for region update on appearing the screen:
@Published var clickedStore: StoreListModel? {
didSet {
updateStoreMapRegion(storeLocation:clickedStore)
}
}
private func updateStoreMapRegion(storeLocation: StoreListModel?) {
if let latitude = storeLocation?.latitude, let longitude = storeLocation?.longitude, let latitudeObj = CLLocationDegrees(latitude), let longitudeObj = CLLocationDegrees(longitude) {
switch self.locationManager.authorizationStatus {
case .notDetermined, .restricted, .denied:
print("No access")
self.region = MKCoordinateRegion(center:CLLocationCoordinate2D(latitude: latitudeObj, longitude: longitudeObj), span: MKCoordinateSpan(latitudeDelta: 0.01, longitudeDelta: 0.01))
case .authorizedAlways, .authorizedWhenInUse:
print("Access")
if CLLocationCoordinate2DIsValid(CLLocationCoordinate2D(latitude: self.userLat, longitude: self.userLng)) && CLLocationCoordinate2DIsValid(CLLocationCoordinate2D(latitude: latitudeObj, longitude: longitudeObj)) {
self.region = MKCoordinateRegion(coordinates: [CLLocationCoordinate2D(latitude: self.userLat, longitude: self.userLng), CLLocationCoordinate2D(latitude: latitudeObj, longitude: longitudeObj)])
} else {
showErrorAlert = true
locationAlert = .failure
serverErrorMessage = "Location is not valid."
}
@unknown default:
break
}
}
}