Ẩ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 dáng của các đối tượng trên bản đồ, bạn cũng có thể ẩn chúng hoàn toàn. 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à biểu tượng phương tiện công cộng trên bản đồ của mình.

Kiểu chỉ hoạt động trên loại bản đồ normal. Kiểu không ảnh hưởng bản đồ trong nhà, vì vậy việc sử dụng kiểu để ẩn các đối tượng sẽ không ngăn chặ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 đồ của bạn, hãy gọi GoogleMap.setMapStyle() truyền một tham số Đối tượng MapStyleOptions chứa kiểu của bạn nội dung khai báo ở định dạng JSON. Bạn có thể tải JSON từ một tài nguyên thô hoặc như được thể hiện 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, chứa nội dung khai báo kiểu JSON sau đây để ẩn các địa điểm yêu thích của doanh nghiệp (POI):

Nội dung khai báo kiểu sau đây ẩn các điểm kinh doanh của biểu tượng địa điểm yêu thích (POI) và 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 có chứa một tài nguyên chuỗi có tên là 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 một 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 điểm kinh doanh của sở 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 dấu ngoặc kép:

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

  • Bộ chọn chỉ định các thành phần địa lý mà bạn có thể kiểu trên bản đồ. Chúng bao gồm đường phố, công viên, sông nước và cũng như nhãn của chúng. Bộ chọn bao gồm các tính năngphần tử, được chỉ định là featureType và Tài sản elementType.
  • 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 yếu tố bản đồ. Mã này xác định màu hiển thị thông qua một 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 mô tả chi tiết về 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 trên nền tảng Maps như một cách nhanh chóng để tạo đối tượng định kiểu JSON. SDK bản đồ dành cho Android hỗ trợ cùng một kiểu khai báo dưới dạng API JavaScript của Maps.

Mã mẫu đầy đủ

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