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