Reputation: 108
The Correct Code is in the solution. The error below I refer to is occurring on the solution.
Hello I solved this my self but here is the code to display a circle ontop of a region in map kit. You just have to provide a state and city and it will place a circle ontop of that region. When I run this code everything works properly but I get this in the output
"Missing MeshRenderables for ground mesh layer for (6/6) of ground tiles. Tile debug info: (Key: 0.2.2.255 t:34 kt:0, Has mesh errors: 0, MeshInstance count: 1, PendingMaterial count: 1, Invisible MeshInstances count: 0 | Key: 1.0.2.255 t:34 kt:0, Has mesh errors: 0, MeshInstance count: 2, PendingMaterial count: 2, Invisible MeshInstances count: 0 |"
if anyone knows how to get ride of this id appreciate the help.
import SwiftUI
import MapKit
struct MapView: UIViewRepresentable {
let region: MKCoordinateRegion
let circleRadius: CLLocationDistance
@Binding var regionToDisplay: MKCoordinateRegion?
func makeUIView(context: Context) -> MKMapView {
MKMapView()
}
func updateUIView(_ view: MKMapView, context: Context) {
view.setRegion(region, animated: true)
let circle = MKCircle(center: region.center, radius: circleRadius)
view.addOverlay(circle)
}
func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
let circleRenderer = MKCircleRenderer(overlay: overlay)
circleRenderer.strokeColor = UIColor.black
let greenColorWithOpacity = UIColor.green.withAlphaComponent(0.5)
circleRenderer.fillColor = greenColorWithOpacity
circleRenderer.lineWidth = 3.0
return circleRenderer
}
}
Upvotes: 1
Views: 430
Reputation: 108
import SwiftUI
import MapKit
struct MapView: UIViewRepresentable {
@State private var coordinate = CLLocationCoordinate2DMake(45.5202471, -122.6741949)
let city: String
let state: String
func makeUIView(context: Context) -> MKMapView {
let mapView = MKMapView()
mapView.delegate = context.coordinator
let geocoder = CLGeocoder()
let address = "\(city), \(state)"
geocoder.geocodeAddressString(address) { placemarks, error in
if let placemark = placemarks?.first, let location = placemark.location {
self.coordinate = location.coordinate
mapView.setCenter(self.coordinate, animated: true)
let region = MKCoordinateRegion(center: coordinate, latitudinalMeters: 10000, longitudinalMeters: 10000)
mapView.setRegion(region, animated: true)
let regionRadius = 3500.0
let circle = MKCircle(center: coordinate, radius: regionRadius)
mapView.addOverlay(circle)
}
}
return mapView
}
func updateUIView(_ uiView: MKMapView, context: Context) {}
func makeCoordinator() -> Coordinator {
Coordinator()
}
class Coordinator: NSObject, MKMapViewDelegate {
func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
if let circle = overlay as? MKCircle {
let circleRenderer = MKCircleRenderer(circle: circle)
circleRenderer.strokeColor = UIColor.black
circleRenderer.fillColor = UIColor.green.withAlphaComponent(0.5)
circleRenderer.lineWidth = 1.0
return circleRenderer
}
return MKOverlayRenderer(overlay: overlay)
}
}
}
Upvotes: 0