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

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

Cũng như 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 chúng. Ví dụ này cho bạn biết cách ẩn các địa điểm yêu thích (POI) của doanh nghiệp và các biểu tượng phương tiện công cộng trên bản đồ.

Kiểu chỉ hoạt động trên loại bản đồ normal. Kiểu 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 tính nă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 vào 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 ở đị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ư minh hoạ 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 một 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 có chứa nội dung khai báo kiểu JSON sau đây để ẩn các địa điểm yêu thích (POI):

Nội dung khai báo kiểu sau đây sẽ ẩn các địa điểm yêu thích (POI) 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 đây để ẩn các địa điểm 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 sẽ ẩn các địa điểm yêu thích (POI) 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 đồ được tạo kiểu sử dụng 2 khái niệm để áp dụng màu sắc và các thay đổi khác về kiểu cho bản đồ:

  • Bộ chọn chỉ định các thành phần địa lý mà bạn có thể tạo kiểu trên bản đồ. Các thuộc tính này bao gồm đường phố, công viên, vùng nước, v.v., cũng như nhãn. Bộ chọn bao gồm các tính năngphần tử, được chỉ định dưới dạng thuộc tính featureTypeelementType.
  • Bộ định kiểu (Stylers) 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 đồ. Tuỳ chọn này xác định màu hiển thị thông qua tổ hợp các giá trị màu sắc, màu sắc và độ sáng/gamma.

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 trên Nền tảng Maps

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

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 kiểu.