הסתרת תכונות המפה באמצעות שינוי הסגנון

בחירת פלטפורמה: 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). בקובץ הזה צריך להשתמש בקו נטוי הפוך כדי לסמן בתו בריחה (escape) מירכאות:

הצהרת הסגנון הבאה מסתירה את הנקודות העסקיות של סמלים של תחומי עניין (POI) ותחבורה ציבורית:

הפריסה (activity_maps.xml) נראית כך:

הצהרות בסגנון JSON

במפות מעוצבות נעשה שימוש בשני מושגים כדי להחיל צבעים ושינויי סגנון אחרים מפה:

  • סלקטורים מציינים את הרכיבים הגיאוגרפיים שאפשר להשתמש בהם סגנון המפה. למשל כבישים, פארקים, גופי מים נוספים, וגם את התוויות שלהן. הבוררים כוללים תכונות ורכיבים, שצוינו כ-featureType וגם elementType נכסים.
  • סטיילרים הם מאפייני צבע וחשיפה שאפשר להשתמש בהם להחיל על רכיבי מפה. הם מגדירים את הצבע המוצג באמצעות שילוב של ערכי גוון, צבע ובהירות/גמא.

בחומר העזר בנושא סגנון תוכלו למצוא תיאור מפורט של אפשרויות עיצוב JSON.

אשף העיצוב בפלטפורמה של מפות Google

אפשר להשתמש באשף עיצוב הפלטפורמה של מפות Google כדי ליצור במהירות אובייקט של עיצוב JSON. ה-SDK של מפות Google ל-Android תומך באותו סגנון הצהרות כ-Maps JavaScript API.

דוגמאות קוד מלא

מאגר ApiDemos ב-GitHub כולל דוגמאות שמדגימות את השימוש בעיצוב.