Ẩn tính năng bản đồ với định kiểu

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

Ngoài việc thay đổi kiểu của các đối tượng trên bản đồ, bạn cũng có thể ẩn hoàn toàn các đối tượng đó. Ví dụ này cho bạn thấy cách ẩn các địa điểm yêu thích (POI) cho doanh nghiệp và biểu tượng phương tiện công cộng trên bản đồ.

Tính năng định kiểu chỉ hoạt động trên loại bản đồ normal. Việc tạo kiểu không ảnh hưởng đến bản đồ trong nhà, vì vậy, việc sử dụng kiểu để ẩn các đối tượng không ngăn sơ đồ tầng trong nhà xuất hiện trên bản đồ.

Truyền một đối tượng kiểu JSON đến bản đồ của bạn

Để tạo kiểu cho bản đồ, hãy gọi GoogleMap.setMapStyle() truyền đối tượng MapStyleOptions chứa nội dung khai báo kiểu của bạn ở định dạng JSON. Bạn có thể tải JSON từ một tài nguyên thô hoặc một chuỗi, như trong các ví dụ sau:

Tài nguyên thô

Mã mẫu sau đây giả định dự án của bạn chứa tài nguyên thô có tên style_json:

// Copyright 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.example.styledmap;

import android.content.res.Resources;
import android.os.Bundle;
import android.util.Log;
import androidx.appcompat.app.AppCompatActivity;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MapStyleOptions;

/**
 * A styled map using JSON styles from a raw resource.
 */
public class MapsActivityRaw extends AppCompatActivity
        implements OnMapReadyCallback {

    private static final String TAG = MapsActivityRaw.class.getSimpleName();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Retrieve the content view that renders the map.
        setContentView(R.layout.activity_maps_raw);

        // Get the SupportMapFragment and register for the callback
        // when the map is ready for use.
        SupportMapFragment mapFragment =
                (SupportMapFragment) getSupportFragmentManager()
                        .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);
    }

    /**
     * Manipulates the map when it's available.
     * The API invokes this callback when the map is ready for use.
     */
    @Override
    public void onMapReady(GoogleMap googleMap) {

        try {
            // Customise the styling of the base map using a JSON object defined
            // in a raw resource file.
            boolean success = googleMap.setMapStyle(
                    MapStyleOptions.loadRawResourceStyle(
                            this, R.raw.style_json));

            if (!success) {
                Log.e(TAG, "Style parsing failed.");
            }
        } catch (Resources.NotFoundException e) {
            Log.e(TAG, "Can't find style. Error: ", e);
        }
        // Position the map's camera near Sydney, Australia.
        googleMap.moveCamera(CameraUpdateFactory.newLatLng(new LatLng(-34, 151)));
    }
}

Xác định tài nguyên thô trong /res/raw/style_json.json, chứa nội dung khai báo kiểu JSON sau để ẩn các địa điểm kinh doanh yêu thích (POI):

Nội dung khai báo kiểu sau đây ẩn các địa điểm yêu thích (POI) của doanh nghiệp và biểu tượng phương tiện công cộng:

Bố cục (activity_maps.xml) sẽ có dạng như sau:

Tài nguyên chuỗi

Mã mẫu sau đây giả định dự án của bạn chứa một tài nguyên chuỗi có tên style_json:

package com.example.styledmap;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MapStyleOptions;

/**
 * A styled map using JSON styles from a string resource.
 */
public class MapsActivityString extends AppCompatActivity
        implements OnMapReadyCallback {

    private static final String TAG = MapsActivityString.class.getSimpleName();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Retrieve the content view that renders the map.
        setContentView(R.layout.activity_maps_string);

        // Get the SupportMapFragment and register for the callback
        // when the map is ready for use.
        SupportMapFragment mapFragment =
                (SupportMapFragment) getSupportFragmentManager()
                        .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);
    }

    /**
     * Manipulates the map when it's available.
     * The API invokes this callback when the map is ready for use.
     */
    @Override
    public void onMapReady(GoogleMap googleMap) {

        // Customise the styling of the base map using a JSON object defined
        // in a string resource file. First create a MapStyleOptions object
        // from the JSON styles string, then pass this to the setMapStyle
        // method of the GoogleMap object.
        boolean success = googleMap.setMapStyle(new MapStyleOptions(getResources()
                .getString(R.string.style_json)));

        if (!success) {
            Log.e(TAG, "Style parsing failed.");
        }
        // Position the map's camera near Sydney, Australia.
        googleMap.moveCamera(CameraUpdateFactory.newLatLng(new LatLng(-34, 151)));
    }
}

Xác định tài nguyên chuỗi trong /res/values/style_strings.xml chứa nội dung khai báo kiểu JSON sau để ẩn các địa điểm kinh doanh yêu thích (POI). Trong tệp này, bạn cần sử dụng dấu gạch chéo ngược để thoát khỏi dấu ngoặc kép:

Nội dung khai báo kiểu sau đây ẩn các địa điểm yêu thích (POI) của doanh nghiệp và biểu tượng phương tiện công cộng:

Bố cục (activity_maps.xml) sẽ có dạng như sau:

Khai báo kiểu JSON

Bản đồ đã tạo kiểu sử dụng hai khái niệm để áp dụng màu và các thay đổi khác về kiểu cho bản đồ:

  • Trình chọn chỉ định các thành phần địa lý mà bạn có thể định kiểu trên bản đồ. Các địa điểm này bao gồm đường, công viên, vùng nước và các địa điểm khác, cũng như nhãn của các vùng nước đó. Các bộ chọn bao gồm tính năngphần tử, được chỉ định dưới dạng thuộc tính featureTypeelementType.
  • Trình tạo kiểu là các thuộc tính màu và chế độ hiển thị mà bạn có thể áp dụng cho các phần tử bản đồ. Các thuộc tính này xác định màu hiển thị thông qua tổ hợp các giá trị sắc độ, màu sắc và độ sáng/gamma.

Hãy xem tài liệu tham khảo về kiểu để biết nội dung mô tả chi tiết về các tuỳ chọn định kiểu JSON.

Trình hướng dẫn tạo kiểu cho Nền tảng Maps

Sử dụng Trình hướng dẫn định kiểu trên Nền tảng Maps để tạo đối tượng định kiểu JSON một cách nhanh chóng. SDK Maps dành cho Android hỗ trợ các nội dung khai báo kiểu giống như API JavaScript của Maps.

Mã mẫu đầy đủ

Kho lưu trữ Apidemos trên GitHub bao gồm các mẫu minh hoạ việc sử dụng định kiểu.