स्टाइल वाला मैप जोड़ना

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

यह पेज उदाहरण के तौर पर नाइट मोड का इस्तेमाल करके, आपके मैप को शैली में ढालने की क्विक गाइड है.

खास जानकारी

स्टाइल के विकल्पों की मदद से, Google Maps की स्टैंडर्ड स्टाइल के प्रज़ेंटेशन को अपनी पसंद के मुताबिक बनाया जा सकता है. इससे सड़कों, पार्क, कारोबारों, और लोकप्रिय जगहों जैसी सुविधाओं के विज़ुअल डिसप्ले को बदला जा सकता है. इसका मतलब है कि आपके पास मैप के कुछ कॉम्पोनेंट पर ज़ोर देने या मैप को ऐप्लिकेशन की स्टाइल के मुताबिक बनाने का विकल्प है.

स्टाइल करने की सुविधा सिर्फ़ kGMSTypeNormal तरह के मैप पर काम करती है.

मैप पर स्टाइल लागू की जा रही हैं

किसी मैप पर पसंद के मुताबिक मैप स्टाइल लागू करने के लिए, GMSMapStyle इंस्टेंस बनाने के लिए GMSMapStyle(...) को कॉल करें, स्थानीय JSON फ़ाइल के यूआरएल या स्टाइल की परिभाषाओं वाली JSON स्ट्रिंग पास करें. मैप की mapStyle प्रॉपर्टी को GMSMapStyle इंस्टेंस असाइन करें.

JSON फ़ाइल का इस्तेमाल किया जा रहा है

नीचे दिए गए उदाहरणों में, GMSMapStyle(...) को कॉल करके लोकल फ़ाइल के लिए यूआरएल पास करते हुए दिखाया गया है:

Swift

import GoogleMaps

class MapStyling: UIViewController {

  // Set the status bar style to complement night-mode.
  override var preferredStatusBarStyle: UIStatusBarStyle {
    return .lightContent
  }

  override func loadView() {
    let camera = GMSCameraPosition.camera(withLatitude: -33.86, longitude: 151.20, zoom: 14.0)
    let mapView = GMSMapView.map(withFrame: CGRect.zero, camera: camera)

    do {
      // Set the map style by passing the URL of the local file.
      if let styleURL = Bundle.main.url(forResource: "style", withExtension: "json") {
        mapView.mapStyle = try GMSMapStyle(contentsOfFileURL: styleURL)
      } else {
        NSLog("Unable to find style.json")
      }
    } catch {
      NSLog("One or more of the map styles failed to load. \(error)")
    }

    self.view = mapView
  }
}
      

Objective-C

#import "MapStyling.h"
@import GoogleMaps;

@interface MapStyling ()

@end

@implementation MapStyling

// Set the status bar style to complement night-mode.
- (UIStatusBarStyle)preferredStatusBarStyle {
  return UIStatusBarStyleLightContent;
}

- (void)loadView {
  GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:-33.86
                                                          longitude:151.20
                                                               zoom:12];
  GMSMapView *mapView = [GMSMapView mapWithFrame:CGRectZero camera:camera];
  mapView.myLocationEnabled = YES;

  NSBundle *mainBundle = [NSBundle mainBundle];
  NSURL *styleUrl = [mainBundle URLForResource:@"style" withExtension:@"json"];
  NSError *error;

  // Set the map style by passing the URL for style.json.
  GMSMapStyle *style = [GMSMapStyle styleWithContentsOfFileURL:styleUrl error:&error];

  if (!style) {
    NSLog(@"The style definition could not be loaded: %@", error);
  }

  mapView.mapStyle = style;
  self.view = mapView;
}

@end
      

स्टाइल के विकल्प तय करने के लिए, style.json नाम वाले अपने प्रोजेक्ट में एक नई फ़ाइल जोड़ें. साथ ही, नाइट-मोड स्टाइलिंग के लिए JSON स्टाइल के इस एलान को चिपकाएं:

किसी स्ट्रिंग रिसॉर्स का इस्तेमाल करना

ये उदाहरण, GMSMapStyle(...) को कॉल करने और स्ट्रिंग रिसॉर्स को पास करने के बारे में बताते हैं:

Swift

class MapStylingStringResource: UIViewController {

  let MapStyle = "JSON_STYLE_GOES_HERE"

  // Set the status bar style to complement night-mode.
  override var preferredStatusBarStyle: UIStatusBarStyle {
    return .lightContent
  }

  override func loadView() {
    let camera = GMSCameraPosition.camera(withLatitude: -33.86, longitude: 151.20, zoom: 14.0)
    let mapView = GMSMapView.map(withFrame: CGRect.zero, camera: camera)

    do {
      // Set the map style by passing a valid JSON string.
      mapView.mapStyle = try GMSMapStyle(jsonString: MapStyle)
    } catch {
      NSLog("One or more of the map styles failed to load. \(error)")
    }

    self.view = mapView
  }
}
      

Objective-C

@implementation MapStylingStringResource

// Paste the JSON string to use.
static NSString *const kMapStyle = @"JSON_STYLE_GOES_HERE";

// Set the status bar style to complement night-mode.
- (UIStatusBarStyle)preferredStatusBarStyle {
  return UIStatusBarStyleLightContent;
}

- (void)loadView {
  GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:-33.86
                                                          longitude:151.20
                                                               zoom:12];
  GMSMapView *mapView = [GMSMapView mapWithFrame:CGRectZero camera:camera];
  mapView.myLocationEnabled = YES;

  NSError *error;

  // Set the map style by passing a valid JSON string.
  GMSMapStyle *style = [GMSMapStyle styleWithJSONString:kMapStyle error:&error];

  if (!style) {
    NSLog(@"The style definition could not be loaded: %@", error);
  }

  mapView.mapStyle = style;
  self.view = mapView;
}

@end
      

स्टाइल के विकल्प तय करने के लिए, इस स्टाइल स्ट्रिंग को kMapStyle वैरिएबल की वैल्यू के तौर पर चिपकाएं:

JSON स्टाइल का एलान

किसी मैप में रंग और स्टाइल में किए जाने वाले दूसरे बदलाव लागू करने के लिए, स्टाइल वाले मैप दो सिद्धांतों का इस्तेमाल करते हैं:

  • सिलेक्टर उन भौगोलिक कॉम्पोनेंट के बारे में बताते हैं जिन्हें मैप पर स्टाइल में जोड़ा जा सकता है. इनमें सड़कें, पार्क, पानी के स्रोत, और उनके लेबल शामिल हैं. सिलेक्टर में सुविधाएं और एलिमेंट को शामिल किया जाता है, जिन्हें featureType और elementType प्रॉपर्टी के तौर पर बताया जाता है.
  • स्टाइलर, रंग और दिखने से जुड़ी ऐसी प्रॉपर्टी हैं जिन्हें मैप एलिमेंट पर लागू किया जा सकता है. वे रंग, रंग, और लाइटनेस/गामा वैल्यू को मिलाकर दिखाए गए रंग को तय करते हैं.

JSON स्टाइल के विकल्पों की ज़्यादा जानकारी के लिए, स्टाइल का रेफ़रंस देखें.

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

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

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

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

अगला कदम

शैली में मैप पर सुविधाएं छिपाने का तरीका जानें.