Stille Harita Özelliklerini Gizleme

Platform seçin: Android iOS JavaScript

Haritadaki özelliklerin stilini değiştirmenin yanı sıra, özellikleri gizlemek için kullanabiliyoruz. Bu örnekte, önemli işletme noktalarının (ÖY) nasıl gizleneceği gösterilmektedir ve toplu taşıma simgelerini görüntüleyebilirsiniz.

Stil yalnızca kGMSTypeNormal harita türünde çalışır.

Haritanıza stil uygulama

Bir haritaya özel harita stilleri uygulamak için GMSMapStyle(...) işlevini çağırarak Yerel bir JSON dosyası için URL veya bir JSON dosyası iletme, GMSMapStyle örneği Stil tanımlarını içeren dize. GMSMapStyle örneğini şuna atayın: Haritanın mapStyle özelliği.

JSON dosyası kullanma

Aşağıdaki örneklerde GMSMapStyle(...) çağrısının yapılması ve yerel dosya:

Aşağıdaki kod örneğinde, projenizin style.json:

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
      

Stil seçeneklerini tanımlamak için projenize style.json adlı yeni bir dosya ekleyin. ve aşağıdaki JSON stili beyanını yapıştırarak ilgi alanı (ÖY) ve toplu taşıma simgeleri:

Dize kaynağı kullanma

Aşağıdaki örneklerde GMSMapStyle() çağrısının yapılması ve bir dizenin iletilmesi gösterilmektedir kaynak:

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
      

Aşağıdaki stil beyanı, işletmenin önemli noktalarını (ÖY'ler) ve toplu taşıma simgeleri. Aşağıdaki stil dizesini kMapStyle değişkeni:

JSON stili bildirimleri

Stil özellikleri ayarlanmış haritalar, bir tarayıcıya renk ve diğer stil değişikliklerini uygulamak için harita:

  • Seçiciler, oluşturabileceğiniz coğrafi bileşenleri belirtir. stilini de değiştirebilirsiniz. Bunlar yollar, parklar, su kütleleri yanı sıra etiketleri de dahil. Seçiciler özellikler içerir ve öğeler, featureType ve elementType mülk.
  • Stilciler, kullanabileceğiniz renk ve görünürlük özellikleridir. eşleme öğelerine uygulanır. Görüntülenen rengi renk tonu, renk ve açıklık/gama değerlerinin birleşiminden oluşur.

Ayrıntılı açıklama için stil referansına JSON stil seçenekleri.

Haritalar Platformu Stil Sihirbazı

Hızlı bir yöntem olarak Haritalar Platformu Stil Sihirbazı'nı kullanın kullanarak bir JSON stil nesnesi oluşturun. iOS için Haritalar SDK'sı Maps JavaScript API ile aynı stil beyanlarını kullanın.

Tam kod örnekleri

GitHub'daki ApiDemos deposu şunları içerir: örnek olarak verilebilir.