ios - 检索屏幕区域的城市名称 [Mapbox]

标签 ios swift maps mapbox mapbox-ios

我对 iOS 和 Mapbox 开发还比较陌生。我正在开发一个应用程序,用户可以在其中自由操作充满他们保存的地点的 map 。

当他们达到完全被城市地理填充的缩放级别时,我想在横幅样式 View 中显示他们正在查看的城市的名称,即使城市标签不在其中在 map 上查看(放大时通常会出现这种情况)。

Here's a screenshot of the UI for context.

我正在尝试使用以下代码查询 Mapbox 切片集的城市名称:

func mapViewRegionIsChanging(_ mapView: MGLMapView) {

    let zoomLevel = mapView.zoomLevel

    if zoomLevel >= 14.0 {

            // layer identifier taken from layer name in Mapbox Studio
            let layerIdentifier = "place-city-lg-n"

            let screenRect = UIScreen.main.bounds

            let cityName = mapView.visibleFeatures(in: screenRect, styleLayerIdentifiers: Set([layerIdentifier]))

            print(cityName)
    }

我认为此代码不起作用,因为标签未以指定的缩放级别显示在屏幕上。

我想知道使用 visibleFeaturesInRect 是否是满足我需求的最佳方法 - 有没有更好的方法来检索城市名称,而不管可见元素和缩放级别如何?

最佳答案

对于此任务,我建议使用 MapboxGeocoder来自 map 盒。用于获取有关城市/村庄的信息。 你可以安装 pod:

pod 'MapboxGeocoder.swift', '~> 0.12'

并使用此代码:

let geocoder = Geocoder.shared

func mapViewRegionIsChanging(_ mapView: MGLMapView) {

    let geocodeOptions = ReverseGeocodeOptions(coordinate: mapView.centerCoordinate)
    geocodeOptions.allowedScopes = [.place]

    let _ = geocoder.geocode(geocodeOptions) { (placemarks, attribution, error) in
        guard let placemark = placemarks?.first else { return }
        print(placemark.name)
        print(placemark.qualifiedName)
    }
}

您可以添加您的条件,这确实有助于解决您的任务

关于ios - 检索屏幕区域的城市名称 [Mapbox],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59707597/

相关文章:

java - 如何创建一个接受随机键和值java的映射?

android - 确定 GPS 纬度的东或西

ios - 对于 iOS,如何在按钮内创建可拖动按钮?

Iphone - NSUserDefault 设置未出现在设置页面上

ios - 实时添加的 Firebase 子项未在 Collection View 中更新

swift - 如何防止自定义 init 方法在 Xcode 中泄露绝对源路径?

ios - Xcode 8 中的 "No such module"错误

ios - ios中的图像翻译

ios - 更改 collectionView 单元格中项目的颜色和文本

java - 如何将Map值添加到ArrayList中?