首页 > 解决方案 > 如何在 Mapbox Swift 中设置特征类型?

问题描述

默认情况下,使用 Mapbox Swift 列出附近的兴趣点会返回一个结果。将limit增加到 10 会引发以下错误:

反向地理编码时,limit 必须与单个类型参数结合使用

这可以通过将特征类型传递为 来解决POI

curl -X GET 'https://api.mapbox.com/geocoding/v5/mapbox.places/55.274111,25.197139.json?
access_token=pk..&limit=10&types=poi'

如何types在 Mapbox Swift 中设置?类中没有这样的属性ReverseGeocodeOptions

let options = ReverseGeocodeOptions(coordinate: 
        CLLocationCoordinate2D(latitude: 40.733, longitude: -73.989))

let task = geocoder.geocode(options) { (placemarks, attribution, error) in
    guard let placemark = placemarks?.first else {
        return
    }

    print(placemark.imageName ?? "")
        // telephone
    print(placemark.genres?.joined(separator: ", ") ?? "")
        // computer, electronic
    print(placemark.administrativeRegion?.name ?? "")
        // New York
    print(placemark.administrativeRegion?.code ?? "")
        // US-NY 
} 

标签: iosswiftmapbox

解决方案


通过搜索MapboxGeocoder.swift项目,我找到了types 列表。他们是:

  • “国家”
  • “地区”
  • “区”
  • “邮政编码”
  • “地方”
  • “地方”
  • “邻里”
  • “地址”
  • “poi.地标”
  • “宝”

更新

为了设置选项类型ReverseGeocodeOptions,设置allowedScopes属性。

let geocodeOptions = ReverseGeocodeOptions(coordinate: coordinate)
geocodeOptions.allowedScopes = .pointOfInterest

请注意,这allowedScopes是一组类型,因此您可以创建多种类型,例如

geocodeOptions.allowedScopes = [.pointOfInterest, .landmark]

推荐阅读