إخفاء ميزات الخريطة باستخدام التصميم

اختَر النظام الأساسي: 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 التالي لإخفاء نقاط الاهتمام للنشاط التجاري:

يخفي إعلان النمط التالي نقاط الاهتمام ورموز النقل العام:

يظهر التنسيق (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 التالي لإخفاء نقاط الاهتمام الخاصة بالنشاط التجاري. في هذا الملف، تحتاج إلى استخدام شرطة مائلة للخلف لتجنُّب علامات الاقتباس:

يخفي إعلان النمط التالي نقاط الاهتمام ورموز النقل العام:

يظهر التنسيق (activity_maps.xml) على النحو التالي:

نماذج تعريف نمط JSON

تستخدم الخرائط ذات الأنماط مفهومين لتطبيق الألوان وتغييرات الأنماط الأخرى على خريطة:

  • تُحدِّد أدوات الاختيار المكوّنات الجغرافية التي يمكنك نمطها على الخريطة. وتشمل هذه الطرق والمنتزهات والمسطحات المائية وغير ذلك، بالإضافة إلى تسمياتها. تشتمل أدوات الاختيار على ميزات وعناصر، يتم تحديدها كسمتَين featureType وelementType.
  • الأنماط هي خصائص اللون والرؤية التي يمكنك تطبيقها على عناصر الخريطة. وهي تحدد اللون المعروض من خلال مزيج من قيم تدرج اللون واللون والإضاءة/غاما.

راجِع مرجع النمط للحصول على وصف مفصّل لخيارات تصميم JSON.

معالج تصميم منصة "خرائط Google"

استخدِم معالج تصميم النظام الأساسي للخرائط كطريقة سريعة لإنشاء كائن نمط JSON. تدعم حزمة تطوير البرامج بالاستناد إلى بيانات "خرائط Google" لنظام التشغيل Android إعلانات الأنماط نفسها التي تستخدمها واجهة برمجة تطبيقات JavaScript للخرائط.

عيّنات التعليمات البرمجية بالكامل

يتضمن مستودع ApiDemos على GitHub نماذج توضح استخدام النمط.