ยูทิลิตี Android GeoJSON ของ Google Maps

เลือกแพลตฟอร์ม: Android iOS JavaScript
  1. บทนำ
  2. เพิ่ม GeoJsonLayer ลงในแผนที่
  3. นำ GeoJsonLayer ออก
  4. เพิ่มและนำ GeoJsonFeature
  5. เข้าถึง GeoJsonFeatures และพร็อพเพอร์ตี้
  6. จัดรูปแบบ GeoJsonLayer และ GeoJsonFeatures
  7. ดูแอปเดโม

เกริ่นนำ

GeoJSON เป็นส่วนขยายของรูปแบบข้อมูล JSON และแสดงข้อมูลทางภูมิศาสตร์ เมื่อใช้ยูทิลิตีนี้ คุณจะจัดเก็บฟีเจอร์ทางภูมิศาสตร์ในรูปแบบ GeoJSON และแสดงผลเป็นเลเยอร์ที่ด้านบนของแผนที่ได้ หากต้องการเพิ่มและนำข้อมูล GeoJSON ออกจากแผนที่ โปรดเรียก addLayerToMap() และ removeLayerFromMap() ตามลำดับ ในทำนองเดียวกัน คุณสามารถเพิ่มและนำฟีเจอร์แต่ละรายการออกได้โดยเรียกใช้ addFeature() และ removeFeature() แล้วส่งในออบเจ็กต์ GeoJsonFeature หากต้องการเข้าถึงฟีเจอร์ โปรดเรียกใช้ getFeatures() เพื่อรับออบเจ็กต์ GeoJsonFeature ทั้งหมดที่เพิ่มลงในเลเยอร์อีกครั้ง

คุณยังตั้งค่ารูปแบบเริ่มต้นที่จะใช้กับฟีเจอร์ก่อนที่จะเพิ่มลงในเลเยอร์ได้ด้วยโดยการเรียกใช้ getDefaultPointStyle(), getDefaultLineStringStyle() หรือ getDefaultPolygonStyle() และตั้งค่าตัวเลือกรูปแบบในแต่ละรูปแบบ หรือคุณจะตั้งค่ารูปแบบสำหรับ GeoJsonFeature แต่ละรายการโดยการเรียกใช้ setPointStyle(), setLineStringStyle() หรือ setPolygonStyle() ในฟีเจอร์แล้วส่งผ่านออบเจ็กต์รูปแบบที่เกี่ยวข้องก็ได้

เพิ่ม GeoJsonLayer ลงในแผนที่ของคุณ

หากต้องการเพิ่มเลเยอร์ GeoJson ลงในแผนที่ ก่อนอื่นให้สร้างอินสแตนซ์ของคลาส GeoJsonLayer การสร้าง GeoJsonLayer มีอยู่สองวิธี

หากต้องการนำเข้าจาก JSONObject คุณต้องมีสิ่งต่อไปนี้

  • ออบเจ็กต์ GoogleMap รายการที่จะแสดงผลเลเยอร์
  • JSONObject ที่มีข้อมูล GeoJSON ที่จะเพิ่มลงในเลเยอร์

Kotlin


val geoJsonData: JSONObject? = // JSONObject containing the GeoJSON data
val layer = GeoJsonLayer(map, geoJsonData)

      

Java

JSONObject geoJsonData = // JSONObject containing the GeoJSON data
GeoJsonLayer layer = new GeoJsonLayer(map, geoJsonData);

      

หากต้องการนำเข้าจากไฟล์ GeoJSON ในเครื่อง คุณต้องมีสิ่งต่อไปนี้

  • ออบเจ็กต์ GoogleMap รายการที่จะแสดงผลเลเยอร์
  • ไฟล์ทรัพยากรในเครื่องที่มีข้อมูล GeoJSON
  • Context ซึ่งต้องใช้เพื่อเปิดไฟล์ทรัพยากรในเครื่อง

Kotlin


val layer = GeoJsonLayer(map, R.raw.geojson_file, context)

      

Java

GeoJsonLayer layer = new GeoJsonLayer(map, R.raw.geojson_file, context);

      

หลังจากที่คุณสร้าง GeoJsonLayer ให้เรียก addLayerToMap() เพื่อเพิ่มข้อมูลที่นำเข้าลงในแผนที่:

Kotlin


layer.addLayerToMap()

      

Java

layer.addLayerToMap();

      

นำ GeoJsonLayer ออก

สมมติว่าคุณได้เพิ่มเลเยอร์นี้แล้ว

Kotlin


val geoJsonData: JSONObject? = // JSONObject containing the GeoJSON data
val layer = GeoJsonLayer(map, geoJsonData)

      

Java

JSONObject geoJsonData = // JSONObject containing the GeoJSON data
GeoJsonLayer layer = new GeoJsonLayer(map, geoJsonData);

      

หากต้องการล้าง GeoJsonLayer ให้เรียก removeLayerFromMap()

Kotlin


layer.removeLayerFromMap()

      

Java

layer.removeLayerFromMap();

      

เพิ่มและนำ GeoJsonFeature ออก

ฟีเจอร์ใน GeoJSON มีประเภทเป็น "ฟีเจอร์" โดยมีเรขาคณิต สมาชิกพร็อพเพอร์ตี้ และ (ไม่บังคับ) มีกรอบล้อมรอบหรือรหัส

คุณสร้างออบเจ็กต์ GeoJsonFeature ทีละรายการแล้วเพิ่มไปยัง GeoJsonLayer ได้

สมมติว่าคุณได้สร้างฟีเจอร์ที่มีจุด 0 และ 0 โดยมี 1 รายการในคุณสมบัติและไม่มีกรอบล้อมรอบ

Kotlin


val point = GeoJsonPoint(LatLng(0.0, 0.0))
val properties = hashMapOf("Ocean" to "South Atlantic")
val pointFeature = GeoJsonFeature(point, "Origin", properties, null)

      

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);

      

หากต้องการเพิ่มฟีเจอร์ลงในเลเยอร์ ให้เรียก addFeature() แล้วส่งฟีเจอร์ลงในฟีเจอร์ที่จะเพิ่ม

Kotlin


layer.addFeature(pointFeature)

      

Java

layer.addFeature(pointFeature);

      

หากต้องการนำฟีเจอร์ออกหลังจากเพิ่มลงในเลเยอร์ ให้เรียก removeFeature() แล้วส่งฟีเจอร์เพื่อนำออก

Kotlin


layer.removeFeature(pointFeature)

      

Java

layer.removeFeature(pointFeature);

      

เข้าถึง GeoJsonFeatures และพร็อพเพอร์ตี้

หากต้องการเข้าถึง GeoJsonFeatures ทั้งหมดที่เพิ่มลงในเลเยอร์แล้ว คุณสามารถเรียกใช้ getFeatures() ใน GeoJsonLayer ที่คุณสร้างไว้ ซึ่งจะแสดง GeoJsonFeatures ที่ทำซ้ำได้ที่คุณเข้าถึงได้โดยใช้การวนซ้ำสำหรับแต่ละรอบดังที่แสดงด้านล่าง

Kotlin


for (feature in layer.features) {
    // Do something to the feature
}

      

Java

for (GeoJsonFeature feature : layer.getFeatures()) {
    // Do something to the feature
}

      

ใช้เมธอด hasProperty() และ getProperty() ร่วมกับเมธอด getFeatures() เพื่อตรวจสอบว่าฟีเจอร์ที่จัดเก็บไว้แต่ละรายการมีพร็อพเพอร์ตี้เฉพาะหรือไม่ และเข้าถึงได้หากมี

Kotlin


if (feature.hasProperty("Ocean")) {
    val oceanProperty = feature.getProperty("Ocean")
}

      

Java

if (feature.hasProperty("Ocean")) {
    String oceanProperty = feature.getProperty("Ocean");
}

      

เหตุการณ์การคลิกเรขาคณิต GeoJSON

คุณสามารถใช้ GeoJsonLayer.OnFeatureClickListener() เพื่อฟังกิจกรรมการคลิกบนฟีเจอร์เรขาคณิตบนแผนที่ ตัวอย่างต่อไปนี้จะบันทึกชื่อของฟีเจอร์เมื่อผู้ใช้คลิกฟีเจอร์

Kotlin


// Set a listener for geometry clicked events.
layer.setOnFeatureClickListener { feature ->
    Log.i("GeoJsonClick", "Feature clicked: ${feature.getProperty("title")}")
}

      

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"));
    }
});

      

จัดรูปแบบ GeoJsonLayer และ GeoJsonFeatures

คุณจะตั้งค่ารูปแบบเริ่มต้นสำหรับ GeoJsonLayer หรือจัดรูปแบบแต่ละจุดสนใจในเลเยอร์ก็ได้

รูปแบบเริ่มต้น

ใน GeoJsonLayer คุณจะตั้งค่ารูปแบบเริ่มต้นสำหรับจุด สตริงเส้น และรูปหลายเหลี่ยมที่เพิ่มลงในเลเยอร์ได้ จะใช้รูปแบบเริ่มต้นก็ต่อเมื่อฟีเจอร์ไม่มีชุดรูปแบบสำหรับเรขาคณิต การเปลี่ยนแปลงใดๆ ที่คุณทำกับสไตล์เริ่มต้นจะแสดงในฟีเจอร์ทั้งหมดที่ใช้สไตล์เริ่มต้นด้วย

ขั้นตอนในการนำสไตล์เริ่มต้นไปใช้มีดังนี้

  1. ดึงข้อมูลออบเจ็กต์รูปแบบเริ่มต้นที่เกี่ยวข้อง ซึ่งสามารถเป็นหนึ่งใน GeoJsonPointStyle, GeoJsonLineStringStyle หรือ GeoJsonPolygonStyle
  2. ใช้ตัวเลือกที่ต้องการกับสไตล์

ตัวอย่างเช่น ตัวอย่างโค้ดต่อไปนี้แสดงวิธีแก้ไขรูปแบบของจุดเริ่มต้น ซึ่งจะทําให้ลากจุดได้ด้วยชื่อและตัวอย่างข้อมูล

Kotlin


val pointStyle = layer.defaultPointStyle
pointStyle.isDraggable = true
pointStyle.title = "Hello, World!"
pointStyle.snippet = "I am a draggable marker"

      

Java

GeoJsonPointStyle pointStyle = layer.getDefaultPointStyle();
pointStyle.setDraggable(true);
pointStyle.setTitle("Hello, World!");
pointStyle.setSnippet("I am a draggable marker");

      

รูปแบบเ��พาะสำหรับ GeoJsonFeature

หรือคุณสามารถกำหนดรูปแบบของจุดสนใจแต่ละรายการในเลเยอร์ได้ ขั้นตอนในการนำสไตล์ไปใช้ใน GeoJsonFeature มีดังนี้

  1. สร้างออบเจ็กต์รูปแบบที่เกี่ยวข้อง ซึ่งอาจเป็น GeoJsonPointStyle, GeoJsonLineStringStyle หรือ GeoJsonPolygonStyle
  2. ใช้ตัวเลือกที่ต้องการกับสไตล์
  3. ส่งออบเจ็กต์รูปแบบไปยังเมธอดที่เกี่ยวข้องใน GeoJsonFeature ซึ่งจะเป็น setPointStyle(), setLineStringStyle() หรือ setPolygonStyle()

ด้านล่างนี้เป็นวิธีปรับแต่งรูปแบบสตริงเส้นของ GeoJsonFeature ให้มีสีเป็นสีแดง

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

      

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);

      

ดูแอปเดโม

สำหรับตัวอย่างการนำเข้าไฟล์ GeoJSON จาก URL และสร้างเลเยอร์ด้วยไฟล์ โปรดดู GeoJsonDemoActivity ในแอปสาธิตที่มาพร้อมกับไลบรารียูทิลิตี คู่มือการตั้งค่าจะแสดงวิธีเรียกใช้แอปเดโม