Stilli Harita Ekle

Platform seçin: Android iOS JavaScript

Bu sayfa, örnek olarak gece modunu kullanarak haritanızın stilini ayarlamayla ilgili hızlı bir kılavuzdur.

Genel Bakış

Stil seçenekleriyle standart Google arama sonuçları sayfasının yol, park, güvercinlik gibi özelliklerin görsel görüntüsünü değiştirerek ve diğer önemli noktaları görebilirsiniz. Bu da ekip üyelerinizin haritanın belirli bileşenlerini yapabilir veya haritayı, işletmenizin tarzını tamamlayacak şekilde uygulamasını indirin.

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:

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 stil bildirimini yapıştırın:

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
      

Stil seçeneklerini tanımlamak için 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.

Sonraki adım

Stil ile haritadaki özellikleri nasıl gizleyebileceğinizi öğrenin.