本頁面顯示如何使用 GMUGeoJSONParser
與 GMUGeometryRenderer
搭配使用,以 GeoJSON 格式呈現地理資料。GeoJSON 是一種熱門的格式,用來轉譯點資料、線條和多邊形等地理資料。
必要條件和注意事項
GMUGeoJSONParser
是 Maps SDK for iOS 公用程式庫的一部分。如果您尚未設定程式庫,請先按照設定指南的步驟完成設定,再閱讀本頁面的其他內容。
如需完整程式碼範例,請參閱 GitHub 中的範例應用程式。
轉譯 GeoJSON 資料
如要在地圖上顯示 GeoJSON 資料,請建立包含 GeoJSON 資源路徑 (在此範例中為 GeoJSON_sample.kml
) 的 GMUGeoJSONParser
。然後建立 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