หน้านี้แสดงวิธีแสดงข้อมูลทางภูมิศาสตร์ในรูปแบบ GeoJSON โดยใช้ GMUGeoJSONParser
ร่วมกับ GMUGeometryRenderer
GeoJSON เป็นรูปแบบที่ได้รับความนิยมในการแสดงผลข้อมูลทางภูมิศาสตร์ เช่น จุด เส้น และรูปหลายเหลี่ยม
ข้อกำหนดเบื้องต้นและหมายเหตุ
GMUGeoJSONParser
เป็นส่วนหนึ่งของ Maps SDK สำหรับ iOS Utility Library หากคุณยังไม่ได้ตั้งค่าไลบรารี ให้ทำตามคู่มือการตั้งค่าก่อนอ่านส่วนที่เหลือของหน้านี้
ดูตัวอย่างโค้ดแบบเต็มได้ใน 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