GeoJSON
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
此页面介绍了如何以 GeoJSON 格式呈现地理数据
格式,使用 GMUGeoJSONParser
,
与 GMUGeometryRenderer
结合使用。GeoJSON 是
用于渲染地理数据(如点、线和多边形)的格式。
前提条件和说明
GMUGeoJSONParser
所属的国家/地区
Maps SDK for iOS 实用程序库。如果您尚未设置
库中,请先按照设置指南操作,然后再阅读本页面的其余内容。
如需查看完整的代码示例,请参阅示例应用
<ph type="x-smartling-placeholder"></ph>
GitHub。
渲染 GeoJSON 数据
如需在地图上渲染 GeoJSON 数据,请使用以下代码创建一个 GMUGeoJSONParser
:
指向 GeoJSON 资源(此例中的 GeoJSON_sample.kml
示例)。然后,创建一个 GMUGeometryRenderer
,向其传递
GMUKMLParser
实例。最后,调用
GMUGeometryRenderer.render()
。以下代码示例显示了
在地图上渲染 GeoJSON 数据:
Swift
import GoogleMapsUtils
class GeoJSON {
private var mapView: GMSMapView!
func renderGeoJSON() {
guard let path = Bundle.main.path(forResource: "GeoJSON_sample", ofType: "json") else {
return
}
let url = URL(fileURLWithPath: path)
let geoJsonParser = GMUGeoJSONParser(url: url)
geoJsonParser.parse()
let renderer = GMUGeometryRenderer(map: mapView, geometries: geoJsonParser.features)
renderer.render()
}
}
Objective-C
@import GoogleMapsUtils;
@implementation GeoJSON {
GMSMapView *_mapView;
}
- (void)renderGeoJSON {
NSString *path = [[NSBundle mainBundle] pathForResource:@"GeoJSON_sample" ofType:@"json"];
NSURL *url = [NSURL fileURLWithPath:path];
GMUGeoJSONParser *parser = [[GMUGeoJSONParser alloc] initWithURL:url];
[parser parse];
GMUGeometryRenderer *renderer = [[GMUGeometryRenderer alloc] initWithMap:_mapView
geometries:parser.features];
[renderer render];
}
@end
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-07-26。
[null,null,["最后更新时间 (UTC):2025-07-26。"],[[["\u003cp\u003eThis page demonstrates how to render geographic data in GeoJSON format on Google Maps using the \u003ccode\u003eGMUGeoJSONParser\u003c/code\u003e and \u003ccode\u003eGMUGeometryRenderer\u003c/code\u003e.\u003c/p\u003e\n"],["\u003cp\u003eGeoJSON is a commonly used format for displaying geographic data like points, lines, and polygons.\u003c/p\u003e\n"],["\u003cp\u003eYou'll need to set up the Maps SDK for iOS Utility Library before using \u003ccode\u003eGMUGeoJSONParser\u003c/code\u003e.\u003c/p\u003e\n"],["\u003cp\u003eThe provided code examples show how to render GeoJSON data on a map in both Swift and Objective-C.\u003c/p\u003e\n"]]],["The document explains how to render GeoJSON data on a map using the `GMUGeoJSONParser` and `GMUGeometryRenderer` from the Maps SDK for iOS Utility Library. First, a `GMUGeoJSONParser` is created with the GeoJSON resource path. Then, a `GMUGeometryRenderer` is initialized with the map view and the parser's features. Finally, the `GMUGeometryRenderer.render()` method is called to display the GeoJSON data, such as points, lines, and polygons, on the map.\n"],null,["Select platform: [Android](/maps/documentation/android-sdk/utility/geojson \"View this page for the Android platform docs.\") [iOS](/maps/documentation/ios-sdk/utility/geojson \"View this page for the iOS platform docs.\") [JavaScript](/maps/documentation/javascript/datalayer \"View this page for the JavaScript platform docs.\")\n\nThis page shows you how to render geographic data in the GeoJSON\nformat, using `GMUGeoJSONParser`, in\nconjunction with `GMUGeometryRenderer`. GeoJSON is a popular\nformat for rendering geographic data such as points, lines, and polygons.\n\nPrerequisites and notes\n\n`GMUGeoJSONParser` is part of\nthe [Maps SDK for iOS Utility Library](https://github.com/googlemaps/google-maps-ios-\nutils). If you haven't yet set up\nthe library, follow the [setup guide](/maps/documentation/ios-\nsdk/utility/setup) before reading the rest of this page.\n\nFor the full code sample, see the sample apps\n[on\nGitHub](https://github.com/googlemaps/google-maps-ios-utils).\n\nRendering GeoJSON data\n\nTo render GeoJSON data on a map, create a `GMUGeoJSONParser` with\nthe path to a GeoJSON resource (`GeoJSON_sample.kml` in this\nexample). Then, create a `GMUGeometryRenderer`, passing the\n`GMUKMLParser` instance. Finally, call\n`GMUGeometryRenderer.render()`. The following code example shows\nrendering GeoJSON data on a map: \n\nSwift \n\n```swift\nimport GoogleMapsUtils\n\nclass GeoJSON {\n private var mapView: GMSMapView!\n\n func renderGeoJSON() {\n guard let path = Bundle.main.path(forResource: \"GeoJSON_sample\", ofType: \"json\") else {\n return\n }\n\n let url = URL(fileURLWithPath: path)\n\n let geoJsonParser = GMUGeoJSONParser(url: url)\n geoJsonParser.parse()\n\n let renderer = GMUGeometryRenderer(map: mapView, geometries: geoJsonParser.features)\n renderer.render()\n }\n}\n \n```\n\nObjective-C \n\n```objective-c\n@import GoogleMapsUtils;\n\n@implementation GeoJSON {\n GMSMapView *_mapView;\n}\n\n- (void)renderGeoJSON {\n NSString *path = [[NSBundle mainBundle] pathForResource:@\"GeoJSON_sample\" ofType:@\"json\"];\n NSURL *url = [NSURL fileURLWithPath:path];\n GMUGeoJSONParser *parser = [[GMUGeoJSONParser alloc] initWithURL:url];\n [parser parse];\n GMUGeometryRenderer *renderer = [[GMUGeometryRenderer alloc] initWithMap:_mapView\n geometries:parser.features];\n [renderer render];\n}\n\n@end\n \n```"]]