swiftui 轨迹_解析SwiftUI布局细节(三)地图的基本操作

本文介绍了如何在SwiftUI中使用UIKit控件,特别是如何结合地图进行操作。通过`UIViewRepresentable`协议,我们可以将SwiftUI与UIKit的`MKMapView`集成,并添加点击事件来获取经纬度。网络请求到的数据可以通过SwiftUI的绑定数据模式进行刷新。此外,文章还详细阐述了地图的使用,包括定位、地理编码、反地理编码以及自定义大头针。最后讨论了不同地图坐标系之间的转换,如WGS84、GCJ02和BD09。
摘要由CSDN通过智能技术生成

前言

前面的几篇文章总结了怎样用 SwiftUI 搭建基本框架时候的一些注意点(和这篇文章在相同的分类里面,有需要了可以点进去看看),这篇文章要总结的东西是用地图数据处理结合来说的,通过这篇文章我们能总结到的点有下面几点:

1、SwiftUI怎样使用UIKit的控件

2、网络请求到的数据我们怎样刷新页面(模拟)

3、顺便总结下系统地图的一些基本使用(定位、地图显示、自定义大头针等等)

abbb32f393951cf12d916b0ef34eb34d.png

(点击地图位置会获取经纬度,反地理编译得到具体的位置信息,显示在列表中)

SwiftUI怎样使用UIKit的控件

我们来总结一下,SwiftUI怎么使用UIKit的控件,中间的连接就是 UIViewRepresentable,UIViewRepresentable 是一个协议。我们结合他的源码来具体看看它的内容:

@available(iOS 13.0, tvOS 13.0, *)

@available(macOS, unavailable)

@available(watchOS, unavailable)

public protocol UIViewRepresentable : View where Self.Body == Never {

/// The type of view to present.

associatedtype UIViewType : UIView

/// Creates the view object and configures its initial state.

///

/// You must implement this method and use it to create your view object.

/// Configure the view using your app's current data and contents of the

/// `context` parameter. The system calls this method only once, when it

/// creates your view for the first time. For all subsequent updates, the

/// system calls the ``UIViewRepresentable/updateUIView(_:context:)``

/// method.

///

/// - Parameter context: A context structure containing information about

/// the current state of the system.

///

/// - Returns: Your UIKit view configured with the provided information.

func makeUIView(context: Self.Context) -> Self.UIViewType

/// Updates the state of the specified view with new information from

/// SwiftUI.

///

/// When the state of your app changes, SwiftUI updates the portions of your

/// interface affected by those changes. SwiftUI calls this method for any

/// changes affecting the corresponding UIKit view. Use this method to

/// update the configuration of your view to match the new state information

/// provided in the `context` parameter.

///

/// - Parameters:

/// - uiView: Your custom view object.

/// - context: A context structure containing information about the current

/// state of the system.

func updateUIView(_ uiView: Self.UIViewType, context: Self.Context)

static func dismantleUIView(_ uiView: Self.UIViewType, coordinator: Self.Coordinator)

/// A type to coordinate with the view.

associatedtype Coordinator = Void

func makeCoordinator() -> Self.Coordinator

typealias Context = UIViewRepresentableContext

}

上面的代码可以分析出 UIViewRepresentable 是一个协议,它也是遵守了 View 这个协议的,条件就是内容不能为空,它有一个关联类型  (associatedtype UIViewType : UIView) ,

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值