การซ่อนฟีเจอร์แผนที่ด้วยการจัดรูปแบบ

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

นอกจากการเปลี่ยนสไตล์ของฟีเจอร์บนแผนที่แล้ว คุณยังซ่อนฟีเจอร์เหล่านั้นทั้งหมดได้ด้วย ตัวอย่างนี้แสดงวิธีซ่อนจุดที่น่าสนใจ (POI) ของธุรกิจและไอคอนระบบขนส่งสาธารณะบนแผนที่

การจัดรูปแบบจะทำงานกับแผนที่ประเภท normal เท่านั้น การจัดรูปแบบจะไม่ส่งผลต่อ แผนที่ในอาคาร ดังนั้นการใช้การจัดรูปแบบเพื่อซ่อนฟีเจอร์จะไม่ป้องกันไม่ให้ ผังพื้นในอาคารปรากฏบนแผนที่

ส่งออบเจ็กต์สไตล์ JSON ไปยังแผนที่

หากต้องการจัดรูปแบบแผนที่ ให้เรียกใช้ GoogleMap.setMapStyle() โดยส่งออบเจ็กต์ MapStyleOptions ที่มีประกาศสไตล์ ในรูปแบบ JSON คุณสามารถโหลด JSON จากทรัพยากรดิบหรือสตริงได้ตามที่แสดงในตัวอย่างต่อไปนี้

ทรัพยากรดิบ

ตัวอย่างโค้ดต่อไปนี้จะถือว่าโปรเจ็กต์มีทรัพยากรดิบชื่อ 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)));
    }
}

กำหนดทรัพยากรดิบใน /res/raw/style_json.json ซึ่งมีการประกาศสไตล์ JSON ต่อไปนี้เพื่อซ่อนจุดที่น่าสนใจ (POI) ของธุรกิจ

การประกาศสไตล์ต่อไปนี้จะซ่อนจุดที่น่าสนใจ (POI) ของธุรกิจและไอคอนระบบขนส่งสาธารณะ

เลย์เอาต์ (activity_maps.xml) จะมีลักษณะดังนี้

ทรัพยากรสตริง

ตัวอย่างโค้ดต่อไปนี้จะถือว่าโปรเจ็กต์มีทรัพยากรสตริงชื่อ 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)));
    }
}

กำหนดทรัพยากรสตริงใน /res/values/style_strings.xml ซึ่งมีการประกาศสไตล์ JSON ต่อไปนี้เพื่อซ่อนจุดที่น่าสนใจ (POI) ของธุรกิจ ในไฟล์นี้ คุณต้องใช้แบ็กสแลชเพื่อหลีกเลี่ยงเครื่องหมายคำพูด

การประกาศสไตล์ต่อไปนี้จะซ่อนจุดที่น่าสนใจ (POI) ของธุรกิจและไอคอนระบบขนส่งสาธารณะ

เลย์เอาต์ (activity_maps.xml) จะมีลักษณะดังนี้

การประกาศสไตล์ JSON

แผนที่สไตล์คุณใช้แนวคิด 2 อย่างเพื่อใช้สีและการเปลี่ยนแปลงสไตล์อื่นๆ กับแผนที่

  • ตัวเลือก จะระบุคอมโพเนนต์ทางภูมิศาสตร์ที่คุณจัดรูปแบบบนแผนที่ได้ ซึ่งรวมถึงถนน สวนสาธารณะ แหล่งน้ำ และอื่นๆ รวมถึงป้ายกำกับของคอมโพเนนต์เหล่านั้น ตัวเลือกประกอบด้วย ฟีเจอร์ และ องค์ประกอบ ที่ระบุเป็น featureType และ elementType พร็อพเพอร์ตี้
  • ตัวจัดรูปแบบ คือพร็อพเพอร์ตี้สีและการมองเห็นที่คุณใช้กับองค์ประกอบแผนที่ได้ โดยจะกำหนดสีที่แสดงผ่านการผสมผสานระหว่างค่าเฉดสี สี และความสว่าง/แกมมา

ดูคำอธิบายโดยละเอียดเกี่ยวกับตัวเลือกการจัดรูปแบบ JSON ได้ที่ข้อมูลอ้างอิงสไตล์

วิซาร์ดการจัดรูปแบบ Maps Platform

ใช้วิซาร์ดการจัดรูปแบบ Maps Platform เพื่อสร้างออบเจ็กต์การจัดรูปแบบ JSON อย่างรวดเร็ว Maps SDK สำหรับ Android รองรับการประกาศสไตล์แบบเดียวกับ Maps JavaScript API

ตัวอย่างโค้ดแบบเต็ม

ที่เก็บ ApiDemos ใน GitHub มี ตัวอย่างที่แสดงการใช้การจัดรูปแบบ