Questa pagina è una guida rapida per applicare uno stile alla mappa, utilizzando la modalità notturna come esempio.
Panoramica
Con le opzioni di stile puoi personalizzare la presentazione degli stili standard di Google Maps, modificando la visualizzazione visiva di elementi quali strade, parchi, attività commerciali e altri punti d'interesse. Ciò significa che puoi enfatizzare componenti particolari della mappa o fare in modo che la mappa si adatti allo stile della tua app.
Gli stili possono essere applicati solo sul tipo di mappa kGMSTypeNormal.
Applicare uno stile alla mappa
Per applicare stili mappa personalizzati a una mappa, chiama GMSMapStyle(...) per creare un'istanza GMSMapStyle, trasmettendo un URL per un file JSON locale o una stringa JSON contenente definizioni di stile. Assegna l'istanza GMSMapStyle alla proprietà mapStyle della mappa.
Utilizzando un file JSON
Gli esempi seguenti mostrano come chiamare GMSMapStyle(...) e passare un URL per un
file locale:
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
Per definire le opzioni di stile, aggiungi un nuovo file al progetto denominato style.json, quindi incolla la seguente dichiarazione di stile JSON per lo stile in modalità notturna:
Gli esempi seguenti mostrano come chiamare GMSMapStyle(...) e passare una risorsa stringa:
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
Per definire le opzioni di stile, incolla la seguente stringa di stile come valore della variabile kMapStyle:
Le mappe con stile utilizzano due concetti per applicare colori e altre modifiche di stile a una mappa:
I selettori specificano i componenti geografici che puoi modificare nella mappa. tra cui strade, parchi, specchi d'acqua e altro, con le relative etichette. I selettori includono funzionalità
ed elementi, specificati come proprietà featureType e
elementType.
Gli Styler sono proprietà di colore e visibilità che puoi applicare agli elementi della mappa. Definiscono il colore visualizzato tramite una combinazione di valori di tonalità, colore e luminosità/gamma.