สร้างแผนที่แผนที่

เลือกแพลตฟอร์ม Android iOS JavaScript

แผนที่โคโรเพลทเป็นแผนที่ธีมประเภทหนึ่งที่เขตบริหาร จะมีสีหรือแรเงาตามค่าข้อมูล คุณใช้ค่าเริ่มต้นรูปแบบได้ เพื่อจัดรูปแบบแผนที่โดยยึดตามข้อมูลที่เขตบริหารแต่ละแห่ง ซึ่งเชื่อมโยงกับช่วงของค่าตัวเลข ตัวอย่างแผนที่ต่อไปนี้แสดง แผนที่โคโรเพลท (choropleth) สำหรับรัฐในสหรัฐอเมริกา

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

ภาพหน้าจอแสดงแผนที่ Choropleth ของรัฐในสหรัฐอเมริกา

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

  2. รับการอ้างอิงถึงเลเยอร์คุณลักษณะระดับเขตบริหาร 1 เมื่อ เริ่มต้นได้ สำหรับสหรัฐอเมริกา ระดับการดูแลระบบเหล่านี้ จะสอดคล้องกับแต่ละรัฐ

    Java

    private FeatureLayer areaLevel1Layer;
    @Override public void onMapReady(GoogleMap map) { areaLevel1Layer = map.getFeatureLayer(new FeatureLayerOptions.Builder() .featureType(FeatureType.ADMINISTRATIVE_AREA_LEVEL_1) .build());
    // Apply style factory function to ADMINISTRATIVE_AREA_LEVEL_1 layer. styleAreaLevel1Layer(); }

    Kotlin

    private var areaLevel1Layer: FeatureLayer? = null
    override fun onMapReady(googleMap: GoogleMap) { // Get the ADMINISTRATIVE_AREA_LEVEL_1 feature layer. areaLevel1Layer = googleMap.getFeatureLayer(FeatureLayerOptions.Builder() .featureType(FeatureType.ADMINISTRATIVE_AREA_LEVEL_1) .build())
    // Apply style factory function to ADMINISTRATIVE_AREA_LEVEL_1 layer. styleAreaLevel1Layer() }

  3. สร้างฟังก์ชันเริ่มต้นสไตล์แล้วนำไปใช้กับ เลเยอร์คุณลักษณะของเขตบริหารระดับ 1 ตัวอย่างต่อไปนี้ใช้ กับรูปหลายเหลี่ยมที่แทนแต่ละรัฐของสหรัฐอเมริกา

    Java

    private void styleAreaLevel1Layer() {
      FeatureLayer.StyleFactory styleFactory = (Feature feature) -> {
        if (feature instanceof PlaceFeature) {
          PlaceFeature placeFeature = (PlaceFeature) feature;
    // Return a hueColor in the range [-299,299]. If the value is // negative, add 300 to make the value positive. int hueColor = placeFeature.getPlaceId().hashCode() % 300; if (hueColor < 0) { hueColor += 300; }
    return new FeatureStyle.Builder() // Set the fill color for the state based on the hashed hue color. .fillColor(Color.HSVToColor(150, new float[] {hueColor, 1, 1})) .build(); } return null; };
    // Apply the style factory function to the feature layer. areaLevel1Layer.setFeatureStyle(styleFactory); }

    Kotlin

    private fun styleAreaLevel1Layer() {
      val styleFactory = FeatureLayer.StyleFactory { feature: Feature ->
          if (feature is PlaceFeature) {
              val placeFeature: PlaceFeature = feature as PlaceFeature
    // Return a hueColor in the range [-299,299]. If the value is // negative, add 300 to make the value positive. var hueColor: Int = placeFeature.getPlaceId().hashCode() % 300 if (hueColor < 0) { hueColor += 300 } return@StyleFactory FeatureStyle.Builder() // Set the fill color for the state based on the hashed hue color. .fillColor(Color.HSVToColor(150, floatArrayOf(hueColor.toFloat(), 1f, 1f))) .build() } return@StyleFactory null }
    // Apply the style factory function to the feature layer. areaLevel1Layer?.setFeatureStyle(styleFactory) }