Na tej stronie znajdziesz krótki przewodnik po stylizowaniu mapy, w którym jako przykładu używamy trybu nocnego.
Przegląd
Opcje stylu umożliwiają dostosowanie prezentacji standardowych stylów map Google poprzez zmianę wizualnego wyświetlania elementów, takich jak drogi, parki, firmy i inne punkty orientacyjne. Oznacza to, że możesz wyróżnić określone komponenty mapy lub dopasować ją do stylu aplikacji.
Stylizacja działa tylko w przypadku typu mapy kGMSTypeNormal.
Stosowanie stylów do mapy
Aby zastosować do mapy niestandardowe style mapy, wywołaj funkcję GMSMapStyle(...), aby utworzyć instancję GMSMapStyle, przekazując adres URL lokalnego pliku JSON lub ciąg JSON zawierający definicje stylu. Przypisz instancję GMSMapStyle do właściwości mapStyle mapy.
Korzystanie z pliku JSON
W przykładach poniżej pokazujemy wywołanie funkcji GMSMapStyle(...) i przekazanie adresu URL pliku lokalnego:
Swift
importGoogleMapsclassMapStyling:UIViewController{// Set the status bar style to complement night-mode.overridevarpreferredStatusBarStyle:UIStatusBarStyle{return.lightContent}overridefuncloadView(){letcamera=GMSCameraPosition.camera(withLatitude:-33.86,longitude:151.20,zoom:14.0)letmapView=GMSMapView.map(withFrame:CGRect.zero,camera:camera)do{// Set the map style by passing the URL of the local file.ifletstyleURL=Bundle.main.url(forResource:"style",withExtension:"json"){mapView.mapStyle=tryGMSMapStyle(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"@importGoogleMaps;@interfaceMapStyling()@end@implementationMapStyling// Set the status bar style to complement night-mode.-(UIStatusBarStyle)preferredStatusBarStyle{returnUIStatusBarStyleLightContent;}-(void)loadView{GMSCameraPosition*camera=[GMSCameraPositioncameraWithLatitude:-33.86longitude:151.20zoom:12];GMSMapView*mapView=[GMSMapViewmapWithFrame:CGRectZerocamera:camera];mapView.myLocationEnabled=YES;NSBundle*mainBundle=[NSBundlemainBundle];NSURL*styleUrl=[mainBundleURLForResource:@"style"withExtension:@"json"];NSError*error;// Set the map style by passing the URL for style.json.GMSMapStyle*style=[GMSMapStylestyleWithContentsOfFileURL:styleUrlerror:&error];if(!style){NSLog(@"The style definition could not be loaded: %@",error);}mapView.mapStyle=style;self.view=mapView;}@end
Aby zdefiniować opcje stylu, dodaj do projektu nowy plik o nazwie style.json i wklej do niego tę deklarację stylu JSON dla stylu trybu nocnego:
Poniższe przykłady pokazują wywoływanie funkcji GMSMapStyle(...) i przekazywanie ciągu znaków z zasobu:
Swift
classMapStylingStringResource:UIViewController{letMapStyle="JSON_STYLE_GOES_HERE"// Set the status bar style to complement night-mode.overridevarpreferredStatusBarStyle:UIStatusBarStyle{return.lightContent}overridefuncloadView(){letcamera=GMSCameraPosition.camera(withLatitude:-33.86,longitude:151.20,zoom:14.0)letmapView=GMSMapView.map(withFrame:CGRect.zero,camera:camera)do{// Set the map style by passing a valid JSON string.mapView.mapStyle=tryGMSMapStyle(jsonString:MapStyle)}catch{NSLog("One or more of the map styles failed to load. \(error)")}self.view=mapView}}
Objective-C
@implementationMapStylingStringResource// Paste the JSON string to use.staticNSString*constkMapStyle=@"JSON_STYLE_GOES_HERE";// Set the status bar style to complement night-mode.-(UIStatusBarStyle)preferredStatusBarStyle{returnUIStatusBarStyleLightContent;}-(void)loadView{GMSCameraPosition*camera=[GMSCameraPositioncameraWithLatitude:-33.86longitude:151.20zoom:12];GMSMapView*mapView=[GMSMapViewmapWithFrame:CGRectZerocamera:camera];mapView.myLocationEnabled=YES;NSError*error;// Set the map style by passing a valid JSON string.GMSMapStyle*style=[GMSMapStylestyleWithJSONString:kMapStyleerror:&error];if(!style){NSLog(@"The style definition could not be loaded: %@",error);}mapView.mapStyle=style;self.view=mapView;}@end
Aby zdefiniować opcje stylu, wklej ten ciąg znaków jako wartość zmiennej kMapStyle:
Mapy ze stylami wykorzystują 2 koncepcje, aby zastosować kolory i inne zmiany stylu do mapy:
Selektory określają komponenty geograficzne, które możesz
stylizować na mapie. Obejmują one drogi, parki, zbiorniki wodne i inne obiekty, a także ich etykiety. Selektory obejmują funkcje i elementy określone jako właściwości featureType i elementType.
Style to właściwości koloru i widoczności, które możesz zastosować do elementów mapy. Określają one wyświetlany kolor za pomocą kombinacji wartości odcienia, koloru, jasności i gammy.
Użyj Kreatora stylów Platformy Map Google, aby szybko wygenerować obiekt stylów JSON. Pakiet Maps SDK na iOS obsługuje te same deklaracje stylu co interfejs Maps JavaScript API.
Pełne przykłady kodu
Repozytorium ApiDemos na GitHubie zawiera przykłady, które pokazują, jak używać stylów.
[[["Łatwo zrozumieć","easyToUnderstand","thumb-up"],["Rozwiązało to mój problem","solvedMyProblem","thumb-up"],["Inne","otherUp","thumb-up"]],[["Brak potrzebnych mi informacji","missingTheInformationINeed","thumb-down"],["Zbyt skomplikowane / zbyt wiele czynności do wykonania","tooComplicatedTooManySteps","thumb-down"],["Nieaktualne treści","outOfDate","thumb-down"],["Problem z tłumaczeniem","translationIssue","thumb-down"],["Problem z przykładami/kodem","samplesCodeIssue","thumb-down"],["Inne","otherDown","thumb-down"]],["Ostatnia aktualizacja: 2025-07-16 UTC."],[[["This guide provides instructions for styling Google Maps using JSON to customize the appearance of map elements."],["You can style your map using a local JSON file or a JSON string, applying it to the `mapStyle` property of the map."],["JSON style declarations consist of selectors (`featureType`, `elementType`) to target map components and stylers to define their visual properties."],["Leverage the Maps Platform Styling Wizard to easily create custom JSON styles and apply them to your maps."],["Styling is applicable only to the `kGMSTypeNormal` map type and offers flexibility in highlighting or blending map features with your application's design."]]],["To customize map appearance, apply styles to the `kGMSTypeNormal` map type. Utilize `GMSMapStyle` by passing a URL for a local JSON file or a JSON string to the `mapStyle` property. Define styles with selectors (features and elements) and stylers (color, visibility). Create a `style.json` file for night-mode styling with the provided JSON or use a JSON string directly. Consider cloud customization for uniform styling across multiple apps. The Maps Platform Styling Wizard can help generate JSON style objects. Avoid mixing cloud and hardcoded styles.\n"]]