स्टाइलिंग की मदद से मैप की सुविधाएं छिपाना

प्लैटफ़ॉर्म चुनें: Android iOS JavaScript

मैप पर सुविधाओं की स्टाइल बदलने के साथ-साथ, उन्हें पूरी तरह से छिपाया भी जा सकता है. इस उदाहरण में, मैप पर लोकप्रिय जगहों और सार्वजनिक परिवहन के आइकॉन को छिपाने का तरीका बताया गया है.

स्टाइल करने की सुविधा सिर्फ़ normal तरह के मैप पर काम करती है. स्टाइल से, इनडोर मैप पर कोई असर नहीं पड़ता. इसलिए, सुविधाओं को छिपाने के लिए स्टाइल का इस्तेमाल करने से, इनडोर फ़्लोर प्लान को मैप पर दिखने से नहीं रोका जाता.

अपने मैप पर JSON स्टाइल ऑब्जेक्ट पास करें

अपने मैप को स्टाइल देने के लिए, ऐसा MapStyleOptions ऑब्जेक्ट पास करने के लिए, GoogleMap.setMapStyle() को कॉल करें जिसमें 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 स्टाइल के विकल्पों की ज़्यादा जानकारी के लिए, स्टाइल का रेफ़रंस देखें.

Maps Platform स्टाइलिंग विज़र्ड

JSON स्टाइलिंग ऑब्जेक्ट जनरेट करने के लिए, Maps Platform स्टाइलिंग विज़र्ड का इस्तेमाल फटाफट करें. Android के लिए Maps SDK टूल, Maps JavaScript API की तरह ही स्टाइल से जुड़े एलानों का इस्तेमाल करता है.

पूरे कोड के सैंपल

GitHub पर ApiDemos रिपॉज़िटरी में, ऐसे सैंपल शामिल हैं जो स्टाइलिंग के इस्तेमाल को दिखाते हैं.