Stilli Harita Ekle

Platform seçin: Android iOS JavaScript

Bu sayfa, gece modunu örnek olarak kullanarak haritanızı şekillendirmeye yönelik hızlı bir kılavuzdur.

Genel bakış

Stil seçenekleriyle standart Google harita stillerinin sunumunu özelleştirebilir, yol, park, işletme ve diğer önemli yerler gibi özelliklerin görsel görünümünü değiştirebilirsiniz. Bu, haritanın belirli bileşenlerini vurgulayabileceğiniz veya haritanın, uygulamanızın stilini tamamlamasını sağlayabileceğiniz anlamına gelir.

Stil özelliği yalnızca kGMSTypeNormal harita türünde çalışır.

Stilleri haritanıza uygulama

Bir haritaya özel harita stilleri uygulamak için GMSMapStyle(...) yöntemini çağırarak GMSMapStyle örneği oluşturun, yerel JSON dosyası URL'sini iletin veya stil tanımlarını içeren bir JSON dizesi ekleyin. GMSMapStyle örneğini haritanın mapStyle özelliğine atayın.

JSON dosyası kullanma

Aşağıdaki örneklerde GMSMapStyle(...) çağrılması ve yerel bir dosya için URL geçirilmesi gösterilmektedir:

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 gece modu stili için aşağıdaki JSON stili bildirimini yapıştırın:

Dize kaynağı kullanma

Aşağıdaki örneklerde GMSMapStyle(...) çağrısının ve bir dize kaynağının iletilmesinin gösterilmektedir:

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
      

Stil seçeneklerini tanımlamak için aşağıdaki stil dizesini kMapStyle değişkeninin değeri olarak yapıştırın:

JSON stili bildirimleri

Stilli haritalar, bir haritaya renk ve diğer stil değişikliklerini uygulamak için iki kavramdan yararlanır:

  • Seçiciler, haritada biçimlendirebileceğiniz coğrafi bileşenleri belirtir. Bunlar arasında yollar, parklar, su kaynakları ve daha fazlası ile etiketleri bulunur. Seçiciler, featureType ve elementType özellikleri olarak belirtilen özellikler ile öğeleri içerir.
  • Stilleyiciler, harita öğelerine uygulayabileceğiniz renk ve görünürlük özellikleridir. Gösterilen rengi ton, renk ve açıklık/gama değerlerinin bir kombinasyonuyla tanımlarlar.

JSON stil seçeneklerinin ayrıntılı açıklaması için stil referansı bölümüne bakın.

Haritalar Platformu Stil Sihirbazı

JSON stil nesnesi oluşturmanın hızlı bir yolu olarak Haritalar Platformu Stil Sihirbazı'nı kullanın. iOS 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.

Sonraki adım

Stil ekleyerek haritadaki özelliklerin nasıl gizleneceğini öğrenin.