- บทนำ
- เพิ่ม GeoJsonLayer ลงในแผนที่
- นํา GeoJsonLayer ออก
- เพิ่มและนํา GeoJsonFeature ออก
- เข้าถึง GeoJsonFeatures และพร็อพเพอร์ตี้
- จัดรูปแบบ GeoJsonLayer และ GeoJsonFeatures
- ดูแอปเดโม
บทนำ
GeoJSON เป็นส่วนขยายของรูปแบบข้อมูล JSON และแสดงถึงข้อมูลทางภูมิศาสตร์ คุณสามารถใช้ยูทิลิตีนี้เพื่อจัดเก็บฟีเจอร์ทางภูมิศาสตร์ในรูปแบบ GeoJSON และแสดงผลเป็นเลเยอร์ที่ด้านบนของแผนที่ หากต้องการเพิ่มและนําข้อมูล GeoJSON ออกและออกจากแผนที่ ให้เรียกใช้ addLayerToMap()
และ removeLayerFromMap()
ตามลําดับ ในทํานองเดียวกัน
คุณเพิ่มหรือนําฟีเจอร์แต่ละรายการออกได้โดยเรียกใช้ addFeature()
และ removeFeature()
และส่งผ่านในออบเจ็กต์ GeoJsonFeature
หากต้องการเข้าถึงฟีเจอร์ดังกล่าว คุณสามารถเรียก getFeatures()
ให้ทําซ้ําออบเจ็กต์
GeoJsonFeature
ทั้งหมดที่ได้เพิ่มลงในเลเยอร์แล้ว
และยังกําหนดรูปแบบเริ่มต้นเพื่อใช้กับฟีเจอร์ก่อนที่จะเพิ่มลงในเลเยอร์ได้ด้วยการเรียก getDefaultPointStyle()
, getDefaultLineStringStyle()
หรือ
getDefaultPolygonStyle()
และตั้งค่าตัวเลือกรูปแบบในแต่ละฟีเจอร์
หรือคุณจะกําหนดรูปแบบให้ GeoJsonFeature
แต่ละรายการก็ได้ โดยเรียกใช้ setPointStyle()
, setLineStringStyle()
หรือ setPolygonStyle()
ในฟีเจอร์ แล้วส่งผ่านในออบเจ็กต์รูปแบบที่เกี่ยวข้อง
เพิ่ม GeoJsonLayer ลงในแผนที่
หากต้องการเพิ่มเลเยอร์ GeoJson ลงในแผนที่ ก่อนอื่นให้สร้างอินสแตนซ์ของคลาส GeoJsonLayer การสร้าง GeoJsonLayer ทําได้ 2 วิธี
หากต้องการนําเข้าจาก JSONObject
คุณต้องมีสิ่งต่อไปนี้
- ออบเจ็กต์
GoogleMap
ที่จะแสดงผลเลเยอร์ JSONObject
ที่มีข้อมูล GeoJSON ที่จะเพิ่มไปยังเลเยอร์
Java
JSONObject geoJsonData = // JSONObject containing the GeoJSON data GeoJsonLayer layer = new GeoJsonLayer(map, geoJsonData);
Kotlin
val geoJsonData: JSONObject? = // JSONObject containing the GeoJSON data val layer = GeoJsonLayer(map, geoJsonData)
หากต้องการนําเข้าจากไฟล์ GeoJSON ในเครื่อง คุณต้องมีสิ่งต่อไปนี้
- ออบเจ็กต์
GoogleMap
ที่จะแสดงผลเลเยอร์ - ไฟล์ทรัพยากรในเครื่องที่มีข้อมูล GeoJSON
- ออบเจ็กต์
Context
ซึ่งจําเป็นสําหรับการเปิดไฟล์ทรัพยากรในเครื่อง
Java
GeoJsonLayer layer = new GeoJsonLayer(map, R.raw.geojson_file, context);
Kotlin
val layer = GeoJsonLayer(map, R.raw.geojson_file, context)
หลังจากที่สร้าง GeoJsonLayer
แล้ว ให้เรียกใช้ addLayerToMap()
เพื่อเพิ่มข้อมูลที่นําเข้าลงในแผนที่
Java
layer.addLayerToMap();
Kotlin
layer.addLayerToMap()
นํา GeoJsonLayer ออก
สมมติว่าคุณได้เพิ่มเลเยอร์นี้
Java
JSONObject geoJsonData = // JSONObject containing the GeoJSON data GeoJsonLayer layer = new GeoJsonLayer(map, geoJsonData);
Kotlin
val geoJsonData: JSONObject? = // JSONObject containing the GeoJSON data val layer = GeoJsonLayer(map, geoJsonData)
หากต้องการล้าง GeoJsonLayer ให้เรียกใช้ removeLayerFromMap()
Java
layer.removeLayerFromMap();
Kotlin
layer.removeLayerFromMap()
เพิ่มและนํา GeoJsonFeature ออก
ฟีเจอร์ใน GeoJSON เป็นประเภท "ฟีเจอร์" ซึ่งมีเรขาคณิต สมาชิกพร็อพเพอร์ตี้ และจะมีช่องล้อมรอบหรือรหัส
คุณสร้างออบเจ็กต์ GeoJsonFeature
ทีละรายการและเพิ่มไปยัง GeoJsonLayer
ได้
สมมติว่าคุณสร้างฟีเจอร์ที่มีจุดเป็น 0, 0 กับ 1 รายการในพร็อพเพอร์ตี้และไม่มีกรอบล้อมรอบ
Java
GeoJsonPoint point = new GeoJsonPoint(new LatLng(0, 0)); HashMap<String, String> properties = new HashMap<>(); properties.put("Ocean", "South Atlantic"); GeoJsonFeature pointFeature = new GeoJsonFeature(point, "Origin", properties, null);
Kotlin
val point = GeoJsonPoint(LatLng(0.0, 0.0)) val properties = hashMapOf("Ocean" to "South Atlantic") val pointFeature = GeoJsonFeature(point, "Origin", properties, null)
หากต้องการเพิ่มฟีเจอร์ในเลเยอร์ ให้เรียก addFeature()
และส่งผ่านฟีเจอร์เพื่อเพิ่ม
Java
layer.addFeature(pointFeature);
Kotlin
layer.addFeature(pointFeature)
หากต้องการนําฟีเจอร์ออกหลังจากเพิ่มเข้าไปในเลเยอร์แล้ว ให้เรียก removeFeature()
แล้วส่งผ่านฟีเจอร์เพื่อนําออก
Java
layer.removeFeature(pointFeature);
Kotlin
layer.removeFeature(pointFeature)
เข้าถึง GeoJsonFeatures และพร็อพเพอร์ตี้
หากต้องการเข้าถึง GeoJsonFeatures ทั้งหมดที่เพิ่มลงในเลเยอร์แล้ว คุณสามารถเรียกใช้ getFeatures()
ใน GeoJsonLayer
ที่สร้างขึ้นได้ การดําเนินการนี้จะแสดงผล GeoJsonFeatures
ที่ซ้ําได้ซึ่งคุณจะเข้าถึงได้โดยใช้ Loop สําหรับแต่ละรายการ ดังที่แสดงด้านล่าง
Java
for (GeoJsonFeature feature : layer.getFeatures()) { // Do something to the feature }
Kotlin
for (feature in layer.features) { // Do something to the feature }
ใช้วิธีการ hasProperty()
และ getProperty()
ร่วมกับเมธอด getFeatures()
เพื่อตรวจสอบว่าแต่ละฟีเจอร์ที่จัดเก็บไว้มีพร็อพเพอร์ตี้ที่เฉพาะเจาะจงหรือไม่ และเข้าถึงหากมีอยู่
Java
if (feature.hasProperty("Ocean")) { String oceanProperty = feature.getProperty("Ocean"); }
Kotlin
if (feature.hasProperty("Ocean")) { val oceanProperty = feature.getProperty("Ocean") }
เหตุการณ์การคลิกเรขาคณิต GeoJSON
คุณใช้ GeoJsonLayer.OnFeatureClickListener()
เพื่อฟังเหตุการณ์การคลิกในฟีเจอร์เรขาคณิตบนแผนที่ได้ ตัวอย่างต่อไปนี้บันทึกชื่อฟีเจอร์เมื่อผู้ใช้คลิกฟีเจอร์
Java
// Set a listener for geometry clicked events. layer.setOnFeatureClickListener(new Layer.OnFeatureClickListener() { @Override public void onFeatureClick(Feature feature) { Log.i("GeoJsonClick", "Feature clicked: " + feature.getProperty("title")); } });
Kotlin
// Set a listener for geometry clicked events. layer.setOnFeatureClickListener { feature -> Log.i("GeoJsonClick", "Feature clicked: ${feature.getProperty("title")}") }
จัดรูปแบบ GeoJsonLayer และ GeoJsonFeatures
คุณสามารถตั้งค่ารูปแบบเริ่มต้นสําหรับ GeoJsonLayer หรือจัดรูปแบบฟีเจอร์แต่ละรายการในเลเยอร์ได้
รูปแบบเริ่มต้น
ใน GeoJsonLayer คุณสามารถตั้งค่ารูปแบบเริ่มต้นสําหรับจุด สตริงสตริง และรูปหลายเหลี่ยมที่เพิ่มในเลเยอร์ ระบบจะใช้รูปแบบเริ่มต้นเฉพาะเมื่อฟีเจอร์ดังกล่าวไม่ได้ตั้งค่าสไตล์ให้กับเรขาคณิตทั้ง 2 รูป การเปลี่ยนแปลงใดๆ ที่ทํากับสไตล์เริ่มต้นจะปรากฏในฟีเจอร์ทั้งหมดที่ใช้สไตล์เริ่มต้นด้วย
ขั้นตอนการใช้รูปแบบเริ่มต้นมีดังนี้
- เรียกดูออบเจ็กต์รูปแบบเริ่มต้นที่เกี่ยวข้อง ซึ่งอาจเป็น
GeoJsonPointStyle
,GeoJsonLineStringStyle
หรือGeoJsonPolygonStyle
- ใช้ตัวเลือกที่ต้องการกับสไตล์
ตัวอย่างเช่น ตัวอย่างโค้ดต่อไปนี้จะแสดงวิธีแก้ไขรูปแบบจุดเริ่มต้น ซึ่งจะทําให้จุดต่างๆ ลากได้โดยใช้ชื่อและข้อมูลโค้ด
Java
GeoJsonPointStyle pointStyle = layer.getDefaultPointStyle(); pointStyle.setDraggable(true); pointStyle.setTitle("Hello, World!"); pointStyle.setSnippet("I am a draggable marker");
Kotlin
val pointStyle = layer.defaultPointStyle pointStyle.isDraggable = true pointStyle.title = "Hello, World!" pointStyle.snippet = "I am a draggable marker"
รูปแบบที่เฉพาะเจาะจงสําหรับ GeoJsonFeature
หรือจัดรูปแบบแต่ละฟีเจอร์ในเลเยอร์ก็ได้ ขั้นตอนการใช้รูปแบบใน GeoJsonFeature
มีดังนี้
- สร้างออบเจ็กต์รูปแบบที่เกี่ยวข้อง ซึ่งอาจเป็น
GeoJsonPointStyle
,GeoJsonLineStringStyle
หรือGeoJsonPolygonStyle
- ใช้ตัวเลือกที่ต้องการกับสไตล์
- ส่งออบเจ็กต์รูปแบบไปยังวิธีการที่เกี่ยวข้องใน
GeoJsonFeature
ซึ่งจะเป็นsetPointStyle()
,setLineStringStyle()
หรือsetPolygonStyle()
ตัวอย่างเช่น นี่คือวิธีปรับแต่งรูปแบบสตริงสตริงสําหรับ GeoJsonFeature
เพื่อให้สีเป็นสีแดง
Java
// Create a new feature containing a linestring List<LatLng> lineStringArray = new ArrayList<LatLng>(); lineStringArray.add(new LatLng(0, 0)); lineStringArray.add(new LatLng(50, 50)); GeoJsonLineString lineString = new GeoJsonLineString(lineStringArray); GeoJsonFeature lineStringFeature = new GeoJsonFeature(lineString, null, null, null); // Set the color of the linestring to red GeoJsonLineStringStyle lineStringStyle = new GeoJsonLineStringStyle(); lineStringStyle.setColor(Color.RED); // Set the style of the feature lineStringFeature.setLineStringStyle(lineStringStyle);
Kotlin
// Create a new feature containing a linestring val lineStringArray: MutableList<LatLng> = ArrayList() lineStringArray.add(LatLng(0.0, 0.0)) lineStringArray.add(LatLng(50.0, 50.0)) val lineString = GeoJsonLineString(lineStringArray) val lineStringFeature = GeoJsonFeature(lineString, null, null, null) // Set the color of the linestring to red val lineStringStyle = GeoJsonLineStringStyle() lineStringStyle.color = Color.RED // Set the style of the feature lineStringFeature.lineStringStyle = lineStringStyle
ดูแอปเดโม
โปรดดูตัวอย่างการนําเข้าไฟล์ GeoJSON จาก URL และสร้างเลเยอร์ด้วย GeoJsonDemoActivity
ในแอปสาธิตที่จัดส่งด้วยไลบรารียูทิลิตี คู่มือการตั้งค่าจะแสดงวิธีเรียกใช้แอปสาธิต