如何使用 MKMapKit 生成美国大陆边界折线?

How to use MKMapKit to generate polyline for Continental United States Boundary?

注意:如果此问题需要更多信息,请添加评论。

详情:

我想在 MKMapView 上围绕美国大陆生成一条边界线。这个想法是开始在国家轮廓之外的区域绘制覆盖图(我需要不再强调外部区域,只让美国成为主要焦点;我正在考虑模糊效果,但愿意接受有关实施或实施的建议其他方法实现objective)。

背景:

我需要模糊(或不再强调)默认 MKMapView 中出现的除美国大陆以外的其他国家/地区。

我的初步方法:

  1. 使用折线识别连续的国家/地区边界框 (类似于巴士路线在地图上的表示方式;我注意到那里 似乎是一些默认行~访问这些的任何方法 坐标?)
  2. 确定一种使用 UIVisualEffectView 来屏蔽地图的方法 模糊(或任何其他可能使地图的那部分不 边界内)

我不一定需要代码解决方案(尽管如果已发布,我会尝试)。我想知道在这方面有经验的人是否可以提供伪代码或一些关于执行此过程的最佳方法的指示。

其他研究:

我查看了其他来源,包括 MapKit documentation and this overlay tutorial

您需要在所有 MKMapView 周围绘制一个多边形,内部多边形是您不想模糊的区域。 这是我的解决方案:

首先你需要坐标覆盖地图的每一点:

struct WorldCoordinates {
    static let values = [
        CLLocationCoordinate2D(latitude: 90, longitude: 0),
        CLLocationCoordinate2D(latitude: 90, longitude: 180),
        CLLocationCoordinate2D(latitude: -90, longitude: 180),
        CLLocationCoordinate2D(latitude: -90, longitude: 0),
        CLLocationCoordinate2D(latitude: -90, longitude: -180),
        CLLocationCoordinate2D(latitude: 90, longitude: -180)
    ]
}

然后使用类似这样的方法添加内部和外部多边形: 请注意,我使用“城市”作为结构的示例,其中 boundigCoordinatesArray 是您不想模糊的区域的边界坐标数组。

func addCityBoundingPolygon(in mapView: MKMapView, with city: City) {
    // Declare your inner polygon
    let innerPolygon = MKPolygon(coordinates: city.boundingCoordinatesArray, count: city.boundingCoordinatesArray.count)
    innerPolygon.title = "\(city.id)"
        
    // Declare your outer polygon with previously created inner polygon
    let outerPolygon = MKPolygon(coordinates: WorldCoordinates.values, count: WorldCoordinates.values.count, interiorPolygons: [innerPolygon])
        
    mapView.addOverlay(outerPolygon)    
}

最后像这样在 MKMapViewDelegate 上添加外多边形的颜色:

func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
    if let polygon = overlay as? MKPolygon {
        let renderer = MKPolygonRenderer(polygon: polygon)
        renderer.fillColor = UIColor.outOfCityBounds().withAlphaComponent(0.1)
        renderer.strokeColor = UIColor.outOfCityBounds().withAlphaComponent(0.4)
        renderer.lineWidth = 1
        return renderer
    }

    return MKOverlayRenderer()
}

通过这种方法,您将能够重新创建如下内容: