Stille Harita Özelliklerini Gizleme

Platform seçin: Android iOS JavaScript

Haritadaki özelliklerin stilini değiştirmenin yanı sıra bunları tamamen gizleyebilirsiniz. Bu örnekte, haritanızda işletme önemli yerlerinin (ÖY) ve toplu taşıma simgelerinin nasıl gizleneceği gösterilmektedir.

Stil oluşturma yalnızca normal harita türünde çalışır. Stil, iç mekan haritalarını etkilemez. Bu nedenle, özellikleri gizlemek için stil kullanmak, iç mekan kat planlarının haritada görünmesini engellemez.

Haritanıza JSON stil nesnesi iletme

Haritanızı stillendirmek için JSON biçimindeki stil bildirimlerinizi içeren bir MapStyleOptions nesnesi ileterek GoogleMap.setMapStyle() yöntemini çağırın. JSON'u ham bir kaynaktan veya dizeden yükleyebilirsiniz. Aşağıdaki örneklerde bu işlem gösterilmektedir:

Ham kaynak

Aşağıdaki kod örneğinde, projenizin style_json adlı bir ham kaynak içerdiği varsayılmaktadır:

// 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 içinde, önemli yerleri (ÖY) gizlemek için aşağıdaki JSON stil beyanını içeren bir ham kaynak tanımlayın:

Aşağıdaki stil beyanı, işletme önemli yerlerini (ÖY) ve toplu taşıma simgelerini gizler:

Düzen (activity_maps.xml) aşağıdaki gibi görünür:

Dize kaynağı

Aşağıdaki kod örneğinde, projenizin style_json adlı bir dize kaynağı içerdiği varsayılmaktadır:

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)));
    }
}

İşletmelerle ilgili önemli yerleri (ÖY) gizlemek için aşağıdaki JSON stil beyanını içeren bir dize kaynağını /res/values/style_strings.xml içinde tanımlayın. Bu dosyada, tırnak işaretlerinden çıkmak için ters eğik çizgi kullanmanız gerekir:

Aşağıdaki stil beyanı, işletme önemli yerlerini (ÖY) ve toplu taşıma simgelerini gizler:

Düzen (activity_maps.xml) aşağıdaki gibi görünür:

JSON stil bildirimleri

Stil özellikli haritalar, haritaya renk ve diğer stil değişikliklerini uygulamak için iki kavram kullanır:

  • Seçiciler, haritada stilini belirleyebileceğiniz coğrafi bileşenleri belirtir. Bunlar arasında yollar, parklar, su kütleleri ve daha fazlasının yanı sıra bunların etiketleri de yer alır. Seçiciler, featureType ve elementType özellikleri olarak belirtilen özellikleri ve öğeleri içerir.
  • Stil oluşturucular, harita öğelerine uygulayabileceğiniz renk ve görünürlük özellikleridir. Bu değerler; ton, renk ve parlaklık/gama değerlerinin bir kombinasyonu aracılığıyla görüntülenen rengi tanımlar.

JSON stil seçeneklerinin ayrıntılı açıklaması için stil referansına bakın.

Haritalar Platformu Stil Kılavuzu

JSON stil nesnesi oluşturmanın hızlı bir yolu olarak Maps Platform Stil Kılavuzu'nu kullanın. Android için Haritalar SDK'sı, Maps JavaScript API ile aynı stil bildirimlerini destekler.

Tam kod örnekleri

GitHub'daki ApiDemos deposu, stil kullanımını gösteren örnekler içerir.