地图中注释圈的叠加 - SwiftUI

2023-12-08

我想添加一个具有一定半径的圆Map with SwiftUI仅有的。我希望能够在Map例如1公里。这是我想要实现的图片:

enter image description here

下面代码的问题是圆圈在地图上有固定的大小:

import SwiftUI
import MapKit

struct ContentView: View {
    static let usersLocation = CLLocationCoordinate2D(latitude: 52.0929779694589, longitude: 5.084964426384347)

    @State private var region = MKCoordinateRegion(
        center: ContentView.usersLocation,
        span: MKCoordinateSpan(latitudeDelta: 0.01, longitudeDelta: 0.01)
    )
    let annotations = [CurrentUsersAnnotation()]

    
    var body: some View {
        Map(
            coordinateRegion: $region, annotationItems: annotations) { _ in
                MapAnnotation(coordinate: ContentView.usersLocation) {
                    Circle()
                            .strokeBorder(Color.red, lineWidth: 4)
                            .frame(width: 40, height: 40)
                }
            }
    }
}

struct CurrentUsersAnnotation: Identifiable {
    let id = UUID() // Always unique on map
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

有仅 SwiftUI 的修复吗?我不是在寻找 UIKit 答案,我想在 SwiftUI 中完全做到这一点。


不支持叠加,但您可以轻松调整大小View/Circle通过计算之间的比率Viewspan

首先,由于您需要公里,因此您可以计算该区域的上边缘和下边缘(纬度跨度)之间的距离。

extension MKCoordinateRegion{
    ///Identify the length of the span in meters north to south
    var spanLatitude: Measurement<UnitLength>{
        let loc1 = CLLocation(latitude: center.latitude - span.latitudeDelta * 0.5, longitude: center.longitude)
        let loc2 = CLLocation(latitude: center.latitude + span.latitudeDelta * 0.5, longitude: center.longitude)
        let metersInLatitude = loc1.distance(from: loc2)
        return Measurement(value: metersInLatitude, unit: UnitLength.meters)
    }
}

using GeometryReader你可以得到height of the View/Map,然后是比率。一旦你知道了每米的大小,只需乘以千米即可。

//Size per meter
let ratio = (geo.size.height/region.spanLatitude.value)
//Size per kilometer
let kilometerSize = ratio * 1000

然后使用kilometerSize in the frame.

考虑到两者之间可能存在不一致,这可能不是完美的公里数ViewMap但可能非常接近。如果您能找到已知公里数的东西,您可以对其进行测试。

struct ScaledAnnotationView: View {
    static let usersLocation = CLLocationCoordinate2D(latitude: 52.0929779694589, longitude: 5.084964426384347)
    
    @State private var region = MKCoordinateRegion(
        center: ScaledAnnotationView.usersLocation,latitudinalMeters: 1000, longitudinalMeters: 1000
    )
    let annotations = [CurrentUsersAnnotation()]
    
    
    var body: some View {
        //Get the size of the frame for scale
        GeometryReader{ geo in
            Map(
                coordinateRegion: $region, annotationItems: annotations) { _ in
                    MapAnnotation(coordinate: ScaledAnnotationView.usersLocation) {
                            //Size per kilometer or any unit, just change the converted unit.
                            let kilometerSize = (geo.size.height/region.spanLatitude.converted(to: .kilometers).value)
                        Circle()
                            .fill(Color.red.opacity(0.5))
                        //Keep it a circle
                            .frame(width: kilometerSize, height: kilometerSize)
                    }
                }
        }
    }
}

enter image description here

这是谷歌地图距离测量,用于确认测量

enter image description here

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

地图中注释圈的叠加 - SwiftUI 的相关文章

随机推荐

  • 如何根据“<选项>”更改链接表单操作?

    该表格是搜索表格 当我点击
  • 在 ASP.NET 中实现 404 的最佳方法

    我正在尝试确定在标准 ASP NET Web 应用程序中实现 404 页面的最佳方法 我目前在 Global asax 文件中的 Application Error 事件中捕获 404 错误 并重定向到友好的 404 aspx 页面 问题是
  • 创建表,但如果表已存在则删除它

    我正在处理一个请求 我必须创建一个表来插入一些数据 所以 显然我首先要有一个删除表 在创建 st 之前但是当我第一次运行它时 在创建表之前 它会弹出一个错误 指出表未创建 然后创建表并从这里开始 因此 每次任何人第一次运行我的代码时 都会在
  • 使用 VB.net 的 Excel 文本转列

    我有一个 Excel 工作表 A 列中的条目数量可变 样本 402110000027547 97517161579 IDLE 402 11 150 402110000013260 97517117011 IDLE 402 11 190 40
  • 在 Java 中如何追加到文本文件而不是覆盖它?

    我正在尝试使用 Java 在文本文件中添加一行 当我运行程序时 我的意思是添加一行简单的行 但我的程序在写入新数据之前会删除文本文件中的所有旧数据 这是代码 FileWriter fw null PrintWriter pw null tr
  • php邮件总是进入垃圾邮件

    如何优化 php mail 而不将电子邮件发送到垃圾邮件 我的网页所做的一切都会将电子邮件发送到垃圾邮件 如何使其不发送到垃圾邮件 这取决于您拥有的垃圾邮件引擎 您无法为此优化 PHP 函数 只需尝试使用正确的标头创建电子邮件并从真实的电子
  • Orion API 通过 Keycloak 进行身份验证

    我想通过 Keycloak IdM 在 Orion API 上添加身份验证 我知道可以将 Orion 与 Pep Proxy Wilma 和 Keyrock 一起使用来完成此任务 并且可能的解决方法是将 keyrock 与 keycloak
  • 优化 Visual Studio 解决方案构建 - 在哪里放置 DLL 文件?

    我发现 如果您没有在任何地方启用 复制本地 则许多项目的 C 解决方案的构建时间会变得更快 我做了一些测试 似乎 至少对于我们的解决方案 我们只需删除 复制本地 就可以将构建时间增加 2 3 倍 这可能意味着我们必须将库存储在某个公共目录中
  • Laravel 中的 Composer 更新错误“无法将需求解析为一组可安装的软件包”[重复]

    这个问题在这里已经有答案了 我刚刚更新我的作曲家 它返回一些错误和问题 Your requirements could not be resolved to an installable set of packages Problem 1
  • 使用二分搜索改进插入排序的最坏情况运行时间

    while 循环使用线性搜索向后扫描 但是 我们知道 while 循环中的数组已经排序 所以我们可以用二分查找代替线性查找 这样O n 就变成了O lg n 然而 我对此的看法是 它不会有助于减少总时间 因为我们仍然需要将元素向前移动一个索
  • Jquery:DOM 加载后淡入图像。有时有效..?

    fade in images after the image has loaded document ready function image ad hide bind load function this fadeIn 400 如果有人有
  • 无法使用 C# Windows 应用程序在访问中触发更新和插入查询

    我正在尝试使用 MS Access 后端创建一个 Windows 应用程序 但我遇到了插入和更新查询的一些问题 select 语句对我来说工作正常 但插入和更新不起作用 消息是 syntax error in update and inse
  • Hibernate:OutOfMemoryError:永久代空间[重复]

    这个问题在这里已经有答案了 谁能说我的申请有什么问题吗 public class HibernateUtil private static final SessionFactory sessionFactory static try ses
  • Power Query:当特定值出现在另一列中时如何向列添加一个

    我有一个 ID 列 并且我正在寻找每次特定项目出现在我的列表中时增加我的 ID 的方法Geography柱子 ItalyZ ItalyM UKY or UKM 被发现 身份证号为ItalyZ从 0 开始 到 4000 结束 身份证号为Ita
  • @Transactional(传播=传播.必需)

    如果有人可以解释这个注释的作用以及我们何时使用它 Transactional propagation Propagation REQUIRED Thanks 如果您需要外行人对超出本文档中提供的用途的解释春季文档 考虑这段代码 class
  • 使用经典 ASP VBScript 时无法获取原始 POST 数据

    我花了两天时间尝试设置一个满足第三方提供商要求的端点 他们将通过 HTTPS POST 向我们发送有关业务对象状态的更新 请求的内容将为 JSON 不幸的是 目前它必须用 VBScript 编写 目前 我无法获取他们发送给我的请求的原始内容
  • 采用任何类型及其子类型的函数

    我想写下面的函数 function override
  • 截断文本文件不会更改文件

    当新手 像我一样 要求在 python 中读取 处理文本文件时 他经常得到如下答案 with open input txt r as f for line in f do your stuff 现在我想在特殊行之后截断我正在阅读的文件中的所
  • cmake Windows 10 SDK

    尝试在 Windows 10 上使用 Visual Studio 编译器使用 cmake 编译 VTK 但是 cmake 表示 无法找到此计算机上安装的 Windows 10 SDK 的适当版本 好的 所以我安装了Windows 10 SD
  • 地图中注释圈的叠加 - SwiftUI

    我想添加一个具有一定半径的圆Map with SwiftUI仅有的 我希望能够在Map例如1公里 这是我想要实现的图片 下面代码的问题是圆圈在地图上有固定的大小 import SwiftUI import MapKit struct Con