- บทนำ
- เพิ่ม 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 ที่จะเพิ่มลงใน เลเยอร์
val geoJsonData: JSONObject? = // JSONObject containing the GeoJSON data val layer = GeoJsonLayer(map, geoJsonData)
JSONObject geoJsonData = // JSONObject containing the GeoJSON data GeoJsonLayer layer = new GeoJsonLayer(map, geoJsonData);
หากต้องการนำเข้าจากไฟล์ GeoJSON ในเครื่อง คุณต้องมีสิ่งต่อไปนี้
- ออบเจ็กต์
GoogleMap
รายการที่จะแสดงเลเยอร์ - ไฟล์ทรัพยากรในเครื่องที่มีข้อมูล GeoJSON
Context
ซึ่งจำเป็นสำหรับการเปิดทรัพยากรในเครื่อง ไฟล์
val layer = GeoJsonLayer(map, R.raw.geojson_file, context)
GeoJsonLayer layer = new GeoJsonLayer(map, R.raw.geojson_file, context);
หลังจากสร้าง GeoJsonLayer
แล้ว ให้โทร
addLayerToMap()
เพื่อเพิ่มข้อมูลที่นำเข้าลงในแผนที่:
layer.addLayerToMap()
layer.addLayerToMap();
ลบ GeoJsonLayer
สมมติว่าคุณได้เพิ่มเลเยอร์นี้
val geoJsonData: JSONObject? = // JSONObject containing the GeoJSON data val layer = GeoJsonLayer(map, geoJsonData)
JSONObject geoJsonData = // JSONObject containing the GeoJSON data GeoJsonLayer layer = new GeoJsonLayer(map, geoJsonData);
หากต้องการล้าง GeoJsonLayer ให้โทรหา removeLayerFromMap()
layer.removeLayerFromMap()
layer.removeLayerFromMap();
เพิ่มและนำ GeoJsonFeature ออก
องค์ประกอบใน GeoJSON มีประเภทเป็น "ฟีเจอร์" ซึ่งประกอบด้วยเรขาคณิต ของพร็อพเพอร์ตี้ และจะมีกรอบล้อมรอบหรือรหัสหรือไม่ก็ได้
คุณสามารถสร้างออบเจ็กต์ GeoJsonFeature
รายการและเพิ่มออบเจ็กต์ทีละรายการได้
เป็น GeoJsonLayer
สมมติว่าคุณสร้างสถานที่ที่มีจุด 0, 0 ด้วย มีรายการเดียวในคุณสมบัติ และไม่มีกรอบล้อมรอบ
val point = GeoJsonPoint(LatLng(0.0, 0.0)) val properties = hashMapOf("Ocean" to "South Atlantic") val pointFeature = GeoJsonFeature(point, "Origin", properties, null)
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);
หากต้องการเพิ่มฟีเจอร์ลงในเลเยอร์ ให้โทรหา addFeature()
แล้วส่งผ่าน
คุณลักษณะที่จะเพิ่ม
layer.addFeature(pointFeature)
layer.addFeature(pointFeature);
หากต้องการนำฟีเจอร์หนึ่งออกหลังจากเพิ่มลงในเลเยอร์แล้ว ให้เรียก
removeFeature()
และส่งผ่านฟีเจอร์เพื่อนำออก
layer.removeFeature(pointFeature)
layer.removeFeature(pointFeature);
เข้าถึง GeoJsonFeatures และพร็อพเพอร์ตี้
หากต้องการเข้าถึง GeoJsonFeatures ทั้งหมดที่ถูกเพิ่มลงในเลเยอร์ คุณสามารถ
โทรหา getFeatures()
ที่GeoJsonLayer
ที่คุณ
ที่สร้าง การดำเนินการนี้จะแสดงผลลัพธ์เป็น GeoJsonFeatures
ที่คุณสามารถเข้าถึงได้โดยใช้ลูปสำหรับแต่ละดังที่แสดงด้านล่าง
for (feature in layer.features) { // Do something to the feature }
for (GeoJsonFeature feature : layer.getFeatures()) { // Do something to the feature }
ใช้เมธอด hasProperty()
และ getProperty()
ใน
ใช้ร่วมกับเมธอด getFeatures()
เพื่อตรวจสอบว่าแต่ละ
ที่มีพร็อพเพอร์ตี้หนึ่งๆ และเข้าถึงได้ ถ้ามี
if (feature.hasProperty("Ocean")) { val oceanProperty = feature.getProperty("Ocean") }
if (feature.hasProperty("Ocean")) { String oceanProperty = feature.getProperty("Ocean"); }
กิจกรรมการคลิกเรขาคณิต GeoJSON
คุณสามารถใช้ GeoJsonLayer.OnFeatureClickListener()
เพื่อตรวจจับกิจกรรมการคลิกบน
ฟีเจอร์เรขาคณิตบนแผนที่ ตัวอย่างต่อไปนี้จะบันทึกชื่อของฟีเจอร์เมื่อผู้ใช้
คลิกฟีเจอร์:
// Set a listener for geometry clicked events. layer.setOnFeatureClickListener { feature -> Log.i("GeoJsonClick", "Feature clicked: ${feature.getProperty("title")}") }
// 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")); } });
จัดรูปแบบ GeoJsonLayer และ GeoJsonFeatures
คุณตั้งค่ารูปแบบเริ่มต้นสำหรับ GeoJsonLayer หรือจัดรูปแบบฟีเจอร์แต่ละรายการได้ ในเลเยอร์
รูปแบบเริ่มต้น
ใน GeoJsonLayer คุณสามารถตั้งค่ารูปแบบเริ่มต้นสำหรับจุดและสตริงเส้นใดก็ได้ และรูปหลายเหลี่ยม ที่เพิ่มลงในเลเยอร์ รูปแบบเริ่มต้นจะใช้เฉพาะในกรณีต่อไปนี้ จุดสนใจไม่มีชุดรูปแบบสำหรับรูปทรงเรขาคณิตอย่างใดอย่างหนึ่ง ช่วง การเปลี่ยนแปลงที่คุณทำในรูปแบบเริ่มต้นจะแสดงให้เห็นในจุดสนใจทั้งหมดด้วย ที่ใช้รูปแบบเริ่มต้นอยู่
ขั้นตอนในการนำรูปแบบเริ่มต้นไปใช้มีดังนี้
- ดึงออบเจ็กต์รูปแบบเริ่มต้นที่เกี่ยวข้อง ค่านี้อาจเป็น
GeoJsonPointStyle
,GeoJsonLineStringStyle
หรือGeoJsonPolygonStyle
- ใช้ตัวเลือกที่ต้องการกับรูปแบบ
ตัวอย่างเช่น ตัวอย่างโค้ดต่อไปนี้แสดงวิธีแก้ไขจุดเริ่มต้น ซึ่งจะทำให้จุดลากได้ด้วยชื่อและตัวอย่าง
val pointStyle = layer.defaultPointStyle pointStyle.isDraggable = true pointStyle.title = "Hello, World!" pointStyle.snippet = "I am a draggable marker"
GeoJsonPointStyle pointStyle = layer.getDefaultPointStyle(); pointStyle.setDraggable(true); pointStyle.setTitle("Hello, World!"); pointStyle.setSnippet("I am a draggable marker");
รูปแบบสำหรับ GeoJsonFeature โดยเฉพาะ
อีกทางเลือกหนึ่งคือ คุณสามารถจัดรูปแบบคุณลักษณะแต่ละรายการในเลเยอร์ได้ ขั้นตอนในการ
ใช้รูปแบบใน GeoJsonFeature
ดังนี้
- สร้างออบเจ็กต์รูปแบบที่เกี่ยวข้อง ซึ่งอาจเป็น
GeoJsonPointStyle
,GeoJsonLineStringStyle
หรือGeoJsonPolygonStyle
- ใช้ตัวเลือกที่ต้องการกับรูปแบบ
- ส่งออบเจ็กต์รูปแบบไปยัง
เมธอดที่เกี่ยวข้องใน
GeoJsonFeature
ซึ่งจะsetPointStyle()
,setLineStringStyle()
หรือsetPolygonStyle()
ตัวอย่างเช่น นี่คือวิธีปรับแต่งรูปแบบสตริงบรรทัดสำหรับ
GeoJsonFeature
เพื่อให้สีเป็นสีแดง
// 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
// 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);
ดูแอปเดโม
ตัวอย่างเช่น การนำเข้าไฟล์ GeoJSON จาก URL และสร้างเลเยอร์
ในแอปสาธิต โปรดดูที่ GeoJsonDemoActivity
ในแอปเดโม
ที่ส่งมาพร้อมกับห้องสมุดสาธารณูปโภค คู่มือการตั้งค่าจะแสดงวิธีการ
เพื่อเรียกใช้แอปเดโม