你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn。
在iOS SDK 中与地图交互(预览版)
本文介绍如何使用地图事件管理器。
注意
Azure Maps iOS SDK 停用
适用于 iOS 的 Azure Maps 本机 SDK 现已弃用,将于 2025 年 3 月 31 日停用。 为了避免服务中断,请在 2025 年 3 月 31 日之前迁移到 Azure Maps Web SDK。 有关详细信息,请参阅 Azure Maps iOS SDK 迁移指南。
与地图交互
地图通过其接受委托的 events
属性管理所有事件,这些委托符合 AzureMapDelegate
协议。 下表列出了以 AzureMapDelegate
协议方法表示的所有受支持地图事件。
方法 | 说明 |
---|---|
azureMapCameraIsIdle(_ map: AzureMap) |
在地图进入“空闲”状态之前渲染的最后一帧之后触发:
|
azureMapCameraIsMoving(_ map: AzureMap) |
在从一个视图到另一个视图的动画过渡期间反复触发。 这可能是用户交互或方法的结果。 |
azureMapCameraMoveWasCanceled(_ map: AzureMap) |
在取消对相机的移动请求时触发。 |
azureMap(_ map: AzureMap, cameraMoveIsStarted reason: CameraChangeReason) |
在地图开始从一个视图过渡到另一个视图之前触发。 这可能以编程方式或作为用户交互的结果发生。 reason 参数是一个选项集,提供有关如何启动相机移动的详细信息。 以下列表概述了可能的原因:
|
azureMap(_ map: AzureMap, didTapAt location: CLLocationCoordinate2D) |
在地图上的同一点按下再释放地图时触发。 |
azureMap(_ map: AzureMap, didTapOn features: [Feature]) |
在特征上的同一点按下再释放地图时触发。 |
azureMap(_ map: AzureMap, didAddLayer layer: Layer) |
向地图添加图层时触发。 |
azureMap(_ map: AzureMap, didRemoveLayer layer: Layer) |
从地图中删除图层时触发。 |
azureMapWillLoad(_ map: AzureMap) |
在下载渲染所需的资源之前触发。 |
azureMapDidLoad(_ map: AzureMap) |
在已下载资源并完成地图的第一个视觉渲染之后触发。 |
azureMap(_ map: AzureMap, didLongPressAt location: CLLocationCoordinate2D) |
在地图上的同一点按下地图并保持一段时间,然后释放地图时触发。 |
azureMap(_ map: AzureMap, didLongPressOn features: [Feature]) |
在特征上的同一点按下地图并保持一段时间,然后释放地图时触发。 |
azureMapIsReady(_ map: AzureMap) |
符合以下条件时触发:
|
azureMap(_ map: AzureMap, didAddSource source: Source) |
向地图添加 DataSource 或 VectorTileSource 时触发。 |
azureMap(_ map: AzureMap, didRemoveSource source: Source) |
从地图中删除 DataSource 或 VectorTileSource 时触发。 |
azureMapStyleDidChange(_ map: AzureMap) |
加载或更改地图的样式时触发。 |
以下代码演示如何将 azureMap(_ map: AzureMap, didTapAt location: CLLocationCoordinate2D)
、azureMap(_ map: AzureMap, didTapOn features: [Feature])
和 azureMapCameraIsMoving(_ map: AzureMap)
事件添加到地图。
class ShowSimpleEventsHandlingViewController: UIViewController, AzureMapDelegate {
// Other Setup...
func setupMapControl() {
mapControl.onReady { map in
// Add the delegate to the map to respond to events.
map.events.addDelegate(self)
}
}
func azureMap(_ map: AzureMap, didTapAt location: CLLocationCoordinate2D) {
// Map clicked.
}
func azureMap(_ map: AzureMap, didTapOn features: [Feature]) {
// Feature clicked.
}
func azureMapCameraIsMoving(_ map: AzureMap) {
// Map camera moved.
}
}
有关详细信息,请参阅在地图中导航文章,了解如何与地图交互和触发事件。
将功能事件范围划分为层
在向地图添加委托时,可以将图层 ID 作为另一个参数传入。 传入图层后,仅当该层上发生事件时,后者才会触发。 符号层、气泡层、直线层和多边形层均支持范围为层的事件。
class ShowScopedEventsHandlingViewController: UIViewController, AzureMapDelegate {
// Other Setup...
func setupMapControl() {
mapControl.onReady { map in
// Create a data source.
let source = DataSource()
map.sources.add(source)
// Add data to the data source.
source.add(geometry: Point(CLLocationCoordinate2D(latitude: 0, longitude: 0)))
// Create a layer and add it to the map.
let layer = BubbleLayer(source: source)
map.layers.addLayer(layer)
// Add the delegate to the map to respond to events.
map.events.addDelegate(self, for: [layer.id])
}
}
func azureMap(_ map: AzureMap, didTapOn features: [Feature]) {
// One or more features tapped.
}
func azureMap(_ map: AzureMap, didLongPressOn features: [Feature]) {
// One or more features long pressed.
}
}
其他信息
有关完整代码示例,请参阅以下文章: