swiftMap视图不显示圆形叠加已解决

gijlo24d  于 2023-08-02  发布在  Swift
关注(0)|答案(1)|浏览(101)

正确的代码在解决方案中。下面我提到的错误发生在解决方案上。
你好,我自己解决了这个问题,但这里是在Map工具包中显示一个区域顶部的圆圈的代码。你只需要提供一个州和城市,它会在该地区的顶部放置一个圆圈。当我运行这段代码时,一切正常,但我在输出中得到了这个
“缺少(6/6)地面瓷砖的地面网格层的MeshRenderables。平铺调试信息:(关键字:0.2.2.255 t:34 kt:0,有网格错误:0,网格示例计数:1、待处理物料计数:1、不可见网格示例数:0| Key:1.0.2.255 t:34 kt:0,Has mesh errors:0,网格示例计数:2、PendingMaterial计数:2、Invisible MeshInstances计数:0|“
如果有人知道如何摆脱这个,我很感激你的帮助。

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
    }
}

字符串

nnt7mjpx

nnt7mjpx1#

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)
        }
    }
}

字符串

相关问题