并非所有mapbox.com的服务在中国提供
iOS 地图 SDK
并非所有mapbox.com的服务在中国提供
Current version: v0.0.1
Mapbox 中国插件 iOS 版为使用 Mapbox 中国地图提供重要支持。该插件可以很方便访问 Mapbox 中国 styles, 还提供了专门的 transformer 帮助你把 WGS-84 坐标转换为 GCJ-02 坐标, 以及一个自定义的 location manager 用来获取基于 GCJ-02 坐标系的当前用户地理位置。
要想使用 Mapbox 中国 styles, 你首先需要一个中国 access token。 请填写打开网址 https://www.mapbox.cn/contact 填写在线表单来申请 Mapbox 中国 access token。 在获得 Mapbox 中国 access token 之前,你暂时无法访问 Mapbox 中国矢量瓦片数据。
Mapbox 中国插件需要配合 Mapbox Maps SDK iOS 版使用。 Maps SDK 请参阅 first steps guide 进行安装。
在 Podfile 文件中添加:pod 'MapboxChinaPlugins'
然后在控制台执行 pod update
更新项目文件。
在 Cartfile:中添加:binary "https://www.mapbox.cn/mapbox-china-plugin/ios/Mapbox-iOS-China-Plugin.json" ~> 0.0.1
然后在控制台执行 carthage update
更新项目文件。
Info.plist
添加配置项 MGLMapboxAPIBaseURL
, 并将其值设置为 https://api.mapbox.cn
。 该配置将改变 Mapbox 地图的数据 API。Mapbox 目前提供 3 种通过中国政府审核的地图 styles,它们分别是: Mapbox China Streets, Dark, and Light。 而这个插件能自动帮你吧 Mapbox Streets, Dark, 以及 Light 转换为它们相对于的中国 styles, 没有设置任何 style 情况下,地图将使用 Mapbox China Streets。
同时,该插件还提供了便捷的方法获取这些 style URL。
样式名称 | 样式URL | 获取接口 |
---|---|---|
Streets | mapbox://styles/mapbox/streets-zh-v1 | [MGLStyle mbcn_streetsChineseStyleURL] |
Dark | mapbox://styles/mapbox/dark-zh-v1 | [MGLStyle mbcn_darkChineseStyleURL] |
Light | mapbox://styles/mapbox/light-zh-v1 | [MGLStyle mbcn_lightChineseStyleURL] |
使用 -addToMapView:
方法将中国插件添加到一个新创建的 MGLMapView
对象。 该方法将把地图的 style URL 改为对应的中国 style URL, 也将把地图对象的 locationManager
设置为一个基于 GCJ-02 坐标系的 location manager。
import MapboxChinaPlugin class ViewController: UIViewController, MGLMapViewDelegate { override func viewDidLoad() { super.viewDidLoad() let mapView = MGLMapView(frame: view.bounds) let chinaPlugin = MBXChinaPlugin() chinaPlugin.add(to: mapView) view.addSubview(mapView) } }
默认情况下, Maps SDK 使用 WGS-84 坐标系。 该插件提供了一个 value transformer 帮你把 WGS-84 坐标转换为 GCJ-02 坐标, 从而符合中国政府的相关规定。 目前我们的插件支持转换 CLLocationCoordinate2D
, MGLShape
, 和 MGLFeature
对象。 这些转换操作依赖 NSValueTransformer
, 和 MGLConvertToDatumTransformerName
, 它是一种 NSValueTransformer
.
let transformerName = NSValueTransformerName(rawValue: MGLConvertToDatumTransformerName) let transformer = ValueTransformer(forName: transformerName)
NSValueTransformer *transformer = [NSValueTransformer valueTransformerForName:MGLConvertToDatumTransformerName];
如果要转换 annotation, 只需转换它的 coordinate 属性。 以下演示如何将 WGS-84 坐标转换为 GCJ-02 坐标,转换后的坐标也可以用来设置地图的中心坐标。
let unshiftedCoordinate = CLLocationCoordinate2D(latitude: 31.22894, longitude: 121.45434) // Use the value transformer to shift the coordinate. guard let transformedValue = transformer.transformedValue(NSValue(mglCoordinate: unshiftedCoordinate)) as? NSValue else { return } let annotation = MGLPointAnnotation() // Convert the NSValue back to an CLLocationCoordinate2D. Set the annotation's coordinate property to that shifted value. let shiftedCoordinate = transformedValue.mglCoordinateValue annotation.coordinate = shiftedCoordinate mapView.addAnnotation(annotation) // Set the center coordinate for the map view to the transformed coordinate. mapView.setCenter(shiftedCoordinate, zoomLevel: 15, animated: false)
CLLocationCoordinate2D unshiftedCoordinate = CLLocationCoordinate2DMake(31.22894, 121.45434); // Use the value transformer to shift the coordinate. NSValue *transformedValue = [transformer transformedValue:[NSValue valueWithMGLCoordinate:unshiftedCoordinate]]; MGLPointAnnotation *annotation = [[MGLPointAnnotation alloc] init]; // Convert the NSValue back to an CLLocationCoordinate2D. Set the annotation's coordinate property to that shifted value. CLLocationCoordinate2D shiftedCoordinate = [transformedValue MGLCoordinateValue]; annotation.coordinate = shiftedCoordinate; [mapView addAnnotation:annotation]; // Set the center coordinate for the map view to the transformed coordinate. [mapView setCenterCoordinate:shiftedCoordinate zoomLevel:15 animated:NO];
这个转换方法也可以用来转换 MGLShape 和 MGLFeature 对象, 包含用 GeoJSON 初始化的这些对象。
let originalShape = [ CLLocationCoordinate2D(latitude: 31.22869, longitude: 121.4534), CLLocationCoordinate2D(latitude: 31.22894, longitude: 121.45319), CLLocationCoordinate2D(latitude: 31.2287, longitude: 121.45279), CLLocationCoordinate2D(latitude: 31.22844, longitude: 121.45301), CLLocationCoordinate2D(latitude: 31.22869, longitude: 121.4534)] let shape = MGLPolygon(coordinates: originalShape, count: UInt(originalShape.count)) // Shift the shape's coordinates. let transformedValue = transformer.transformedValue(shape) as! MGLPolygon
CLLocationCoordinate2D originalShape[5] = { CLLocationCoordinate2DMake(31.22869, 121.4534), CLLocationCoordinate2DMake(31.22894, 121.45319), CLLocationCoordinate2DMake(31.2287, 121.45279), CLLocationCoordinate2DMake(31.22844, 121.45301), CLLocationCoordinate2DMake(31.22869, 121.4534)}; MGLPolygon *shape = [MGLPolygon polygonWithCoordinates:originalShape count:5]; // Shift the shape's coordinates.MGLPolygon *transformedShape = [transformer transformedValue:shape];
如果你在使用 Mapbox 中国插件 iOS 版过程中遇到任何问题, 请 联系我们。