Lập bản đồ mật khẩu

Chọn nền tảng: Android iOS JavaScript

Bản đồ choropleth là một loại bản đồ theo chủ đề trong đó các khu vực hành chính được tô màu hoặc tô bóng theo giá trị dữ liệu. Bạn có thể dùng nhà máy kiểu hàm để tạo kiểu cho bản đồ dựa trên dữ liệu tại vị trí của từng khu vực hành chính được liên kết với một dải giá trị số. Bản đồ ví dụ sau thể hiện một bản đồ choropleth cho các tiểu bang của Hoa Kỳ.

Trong ví dụ này, dữ liệu chứa mã địa điểm của trạng thái. Phong cách hàm trạng thái ban đầu tô màu có điều kiện từng trạng thái dựa trên giá trị đã băm của thuộc tính mã địa điểm cho trạng thái.

Ảnh chụp màn hình cho thấy bản đồ một loạt các tiểu bang của Hoa Kỳ.

  1. Nếu bạn chưa thực hiện, hãy làm theo các bước trong Bắt đầu để tạo mã bản đồ mới và kiểu bản đồ. Hãy nhớ bật Lớp đối tượng Khu vực hành chính cấp 1.

  2. Lấy thông tin tham chiếu đến lớp đối tượng của Khu vực hành chính Cấp 1 khi bản đồ sẽ khởi chạy. Đối với Hoa Kỳ, những cấp hành chính này tương ứng với từng tiểu bang.

    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. Tạo một hàm factory có kiểu và áp dụng cho hàm Lớp đối tượng cấp 1 của Khu vực hành chính. Ví dụ sau áp dụng phương pháp cho đa giác đại diện cho từng tiểu bang của Hoa Kỳ.

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