Haritadaki özelliklerin stilini değiştirmenin yanı sıra bunları tamamen gizleyebilirsiniz. Bu örnekte, haritanızda işletme önemli yerlerinin (ÖY) ve toplu taşıma simgelerinin nasıl gizleneceği gösterilmektedir.
Stil oluşturma yalnızca kGMSTypeNormal harita türünde çalışır.
Haritanıza stiller uygulanıyor
Özel harita stillerini bir haritaya uygulamak için GMSMapStyle(...) yöntemini çağırarak bir GMSMapStyle örneği oluşturun ve yerel bir JSON dosyasının URL'sini veya stil tanımlarını içeren bir JSON dizesini iletin. GMSMapStyle örneğini haritanın mapStyle özelliğine atayın.
JSON dosyası kullanma
Aşağıdaki örneklerde, GMSMapStyle(...) işlevinin nasıl çağrılacağı ve yerel bir dosyanın URL'sinin nasıl iletileceği gösterilmektedir:
Aşağıdaki kod örneğinde, projenizin style.json adlı bir dosya içerdiği varsayılmaktadır:
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 options = GMSMapViewOptions() options.camera = camera let mapView = GMSMapView(options: options) 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]; GMSMapViewOptions *options = [[GMSMapViewOptions alloc] init]; options.camera = camera; GMSMapView *mapView = [[GMSMapView alloc] initWithOptions:options]; 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 işletme önemli yerlerini (ÖY) ve toplu taşıma simgelerini gizlemek için aşağıdaki JSON stil beyanını yapıştırın:
Dize kaynağı kullanma
Aşağıdaki örneklerde GMSMapStyle() yönteminin çağrılması ve bir dize kaynağının iletilmesi 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 options = GMSMapViewOptions() options.camera = camera let mapView = GMSMapView(options: options) 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]; GMSMapViewOptions *options = [[GMSMapViewOptions alloc] init]; options.camera = camera; GMSMapView *mapView = [[GMSMapView alloc] initWithOptions:options]; 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şletme önemli yerlerini (ÖY'ler) ve toplu taşıma simgelerini gizler. Aşağıdaki stil dizesini kMapStyle değişkeninin değeri olarak yapıştırın:
JSON stil bildirimleri
Stil özellikli haritalar, haritaya renk ve diğer stil değişikliklerini uygulamak için iki kavram kullanır:
- Seçiciler, haritada stilini belirleyebileceğiniz coğrafi bileşenleri belirtir. Bunlar arasında yollar, parklar, su kütleleri ve daha fazlasının yanı sıra bunların etiketleri de yer alır. Seçiciler,
featureTypeveelementTypeözellikleri olarak belirtilen özellikleri ve öğeleri içerir. - Stil oluşturucular, harita öğelerine uygulayabileceğiniz renk ve görünürlük özellikleridir. Bu değerler; ton, renk ve parlaklık/gama değerlerinin bir kombinasyonu aracılığıyla görüntülenen rengi tanımlar.
JSON stil seçeneklerinin ayrıntılı açıklaması için stil referansına bakın.
Haritalar Platformu Stil Kılavuzu
JSON stil nesnesi oluşturmanın hızlı bir yolu olarak Maps Platform 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.





