הסתרת תכונות המפה באמצעות שינוי הסגנון
קל לארגן דפים בעזרת אוספים
אפשר לשמור ולסווג תוכן על סמך ההעדפות שלך.
בנוסף לשינוי הסגנון של התכונות במפה, אפשר גם להסתיר אותן לגמרי. בדוגמה הזו אפשר לראות איך להסתיר במפה נקודות עניין (POI) של עסקים וסמלים של תחבורה ציבורית.
הסגנון פועל רק בסוג המפה kGMSTypeNormal
.
החלת סגנונות על המפה
כדי להחיל סגנונות מפה בהתאמה אישית על מפה, קוראים ל-GMSMapStyle(...)
כדי ליצור מופע של GMSMapStyle
, ומעבירים כתובת URL לקובץ JSON מקומי או מחרוזת JSON שמכילה הגדרות סגנון. מקצים את המופע GMSMapStyle
למאפיין mapStyle
של המפה.
שימוש בקובץ JSON
בדוגמאות הבאות מוצגת קריאה לפונקציה GMSMapStyle(...)
והעברת כתובת URL לקובץ מקומי:
בדוגמת הקוד הבאה מניחים שהפרויקט מכיל קובץ בשם style.json
:
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
כדי להגדיר את אפשרויות הסגנון, מוסיפים לפרויקט קובץ חדש בשם style.json
ומדביקים בו את הצהרת הסגנון הבאה בפורמט JSON כדי להסתיר נקודות עניין של עסקים וסמלים של תחבורה ציבורית:
הצגה או הסתרה של ה-JSON.
[
{
"featureType": "poi.business",
"elementType": "all",
"stylers": [
{
"visibility": "off"
}
]
},
{
"featureType": "transit",
"elementType": "labels.icon",
"stylers": [
{
"visibility": "off"
}
]
}
]
שימוש במשאב מחרוזת
בדוגמאות הבאות מוצגת קריאה ל-GMSMapStyle()
והעברה של משאב מחרוזת:
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
הצהרת הסגנון הבאה מסתירה נקודות עניין (POI) של עסקים וסמלים של תחבורה ציבורית. מדביקים את מחרוזת הסגנון הבאה כערך של המשתנה kMapStyle
:
הצגה או הסתרה של ה-JSON.
@"["
@" {"
@" \"featureType\": \"poi.business\","
@" \"elementType\": \"all\","
@" \"stylers\": ["
@" {"
@" \"visibility\": \"off\""
@" }"
@" ]"
@" },"
@" {"
@" \"featureType\": \"transit\","
@" \"elementType\": \"labels.icon\","
@" \"stylers\": ["
@" {"
@" \"visibility\": \"off\""
@" }"
@" ]"
@" }"
@"]"
הצהרות סגנון ב-JSON
במפות עם סגנון מוגדר נעשה שימוש בשני מושגים כדי להחיל צבעים ושינויים אחרים בסגנון על מפה:
- רכיבי הבחירה מציינים את הרכיבים הגיאוגרפיים שאפשר להגדיר להם סגנון במפה. הם כוללים כבישים, פארקים, מקווי מים ועוד, וגם את התוויות שלהם. הסלקטורים כוללים תכונות ורכיבים, שמצוינים כמאפיינים
featureType
ו-elementType
.
- סגנונות הם מאפייני צבע וגלויות שאפשר להחיל על רכיבי מפה. הם מגדירים את הצבע שמוצג באמצעות שילוב של גוון, צבע וערכי בהירות/גמא.
במאמר בנושא סגנונות מופיע תיאור מפורט של אפשרויות הסגנון ב-JSON.
אפשר להשתמש באשף הסגנונות של Maps Platform כדי ליצור במהירות אובייקט סגנון JSON. Maps SDK ל-iOS תומך באותן הצהרות סגנון כמו Maps JavaScript API.
דוגמאות קוד מלאות
במאגר ApiDemos ב-GitHub יש דוגמאות שממחישות את השימוש בסגנון.
אלא אם צוין אחרת, התוכן של דף זה הוא ברישיון Creative Commons Attribution 4.0 ודוגמאות הקוד הן ברישיון Apache 2.0. לפרטים, ניתן לעיין במדיניות האתר Google Developers. Java הוא סימן מסחרי רשום של חברת Oracle ו/או של השותפים העצמאיים שלה.
עדכון אחרון: 2025-08-31 (שעון UTC).
[null,null,["עדכון אחרון: 2025-08-31 (שעון UTC)."],[[["\u003cp\u003eLearn how to hide map features like business POIs and transit icons using the Google Maps SDK for iOS.\u003c/p\u003e\n"],["\u003cp\u003eApply custom styles by providing a local JSON file or a JSON string containing style definitions to the \u003ccode\u003emapStyle\u003c/code\u003e property of your map.\u003c/p\u003e\n"],["\u003cp\u003eUtilize the \u003ccode\u003efeatureType\u003c/code\u003e and \u003ccode\u003eelementType\u003c/code\u003e properties in your JSON to select the specific map components you want to style.\u003c/p\u003e\n"],["\u003cp\u003eControl the visibility and color of map elements through \u003ccode\u003estylers\u003c/code\u003e in your JSON style declaration.\u003c/p\u003e\n"],["\u003cp\u003eExplore the Maps Platform Styling Wizard for a user-friendly way to generate JSON styling objects for your map.\u003c/p\u003e\n"]]],["To style maps, you can hide features like business POIs and transit icons. Apply styles using `GMSMapStyle`, either with a local JSON file URL or a JSON string. Create a `style.json` file, and use selectors (features and elements) and stylers (visibility) to define map styles. Use the `mapStyle` property on the map to apply styles. Alternatively, you can define a JSON string with the same style options. You can utilize the [Maps Platform Styling Wizard](https://mapstyle.withgoogle.com) for an efficient JSON style object creation.\n"],null,["Select platform: [Android](/maps/documentation/android-sdk/hiding-features \"View this page for the Android platform docs.\") [iOS](/maps/documentation/ios-sdk/hiding-features \"View this page for the iOS platform docs.\") [JavaScript](/maps/documentation/javascript/json-styling-overview \"View this page for the JavaScript platform docs.\")\n\n\u003cbr /\u003e\n\nAs well as changing the style of features on the map, you can also hide them\nentirely. This example shows you how to hide business points of interest (POIs)\nand public transit icons on your map.\n\nStyling works only on the `kGMSTypeNormal` map type.\n\nApplying styles to your map\n\nTo apply custom map styles to a map, call `GMSMapStyle(...)` to create a\n`GMSMapStyle` instance, passing in a URL for a local JSON file, or a JSON\nstring containing style definitions. Assign the `GMSMapStyle` instance to the\n`mapStyle` property of the map.\n\nUsing a JSON file\n\nThe following examples show calling `GMSMapStyle(...)` and passing a URL for a\nlocal file:\n\nThe following code sample assumes your project contains a file named\n`style.json`:\n\n\nSwift \n\n```swift\nimport GoogleMaps\n\nclass MapStyling: UIViewController {\n\n // Set the status bar style to complement night-mode.\n override var preferredStatusBarStyle: UIStatusBarStyle {\n return .lightContent\n }\n\n override func loadView() {\n let camera = GMSCameraPosition.camera(withLatitude: -33.86, longitude: 151.20, zoom: 14.0)\n let mapView = GMSMapView.map(withFrame: CGRect.zero, camera: camera)\n\n do {\n // Set the map style by passing the URL of the local file.\n if let styleURL = Bundle.main.url(forResource: \"style\", withExtension: \"json\") {\n mapView.mapStyle = try GMSMapStyle(contentsOfFileURL: styleURL)\n } else {\n NSLog(\"Unable to find style.json\")\n }\n } catch {\n NSLog(\"One or more of the map styles failed to load. \\(error)\")\n }\n\n self.view = mapView\n }\n}\n \n```\n\nObjective-C \n\n```objective-c\n#import \"MapStyling.h\"\n@import GoogleMaps;\n\n@interface MapStyling ()\n\n@end\n\n@implementation MapStyling\n\n// Set the status bar style to complement night-mode.\n- (UIStatusBarStyle)preferredStatusBarStyle {\n return UIStatusBarStyleLightContent;\n}\n\n- (void)loadView {\n GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:-33.86\n longitude:151.20\n zoom:12];\n GMSMapView *mapView = [GMSMapView mapWithFrame:CGRectZero camera:camera];\n mapView.myLocationEnabled = YES;\n\n NSBundle *mainBundle = [NSBundle mainBundle];\n NSURL *styleUrl = [mainBundle URLForResource:@\"style\" withExtension:@\"json\"];\n NSError *error;\n\n // Set the map style by passing the URL for style.json.\n GMSMapStyle *style = [GMSMapStyle styleWithContentsOfFileURL:styleUrl error:&error];\n\n if (!style) {\n NSLog(@\"The style definition could not be loaded: %@\", error);\n }\n\n mapView.mapStyle = style;\n self.view = mapView;\n}\n\n@end\n \n```\n\n\u003cbr /\u003e\n\nTo define the style options, add a new file to your project named `style.json`,\nand paste the following JSON style declaration to hide business points of\ninterest (POIs) and public transit icons:\nShow/Hide the JSON. \n\n```text\n[\n {\n \"featureType\": \"poi.business\",\n \"elementType\": \"all\",\n \"stylers\": [\n {\n \"visibility\": \"off\"\n }\n ]\n },\n {\n \"featureType\": \"transit\",\n \"elementType\": \"labels.icon\",\n \"stylers\": [\n {\n \"visibility\": \"off\"\n }\n ]\n }\n]\n```\n\nUsing a string resource\n\nThe following examples show calling `GMSMapStyle()` and passing a string\nresource:\n\n\nSwift \n\n```swift\nclass MapStylingStringResource: UIViewController {\n\n let MapStyle = \"JSON_STYLE_GOES_HERE\"\n\n // Set the status bar style to complement night-mode.\n override var preferredStatusBarStyle: UIStatusBarStyle {\n return .lightContent\n }\n\n override func loadView() {\n let camera = GMSCameraPosition.camera(withLatitude: -33.86, longitude: 151.20, zoom: 14.0)\n let mapView = GMSMapView.map(withFrame: CGRect.zero, camera: camera)\n\n do {\n // Set the map style by passing a valid JSON string.\n mapView.mapStyle = try GMSMapStyle(jsonString: MapStyle)\n } catch {\n NSLog(\"One or more of the map styles failed to load. \\(error)\")\n }\n\n self.view = mapView\n }\n}\n \n```\n\nObjective-C \n\n```objective-c\n@implementation MapStylingStringResource\n\n// Paste the JSON string to use.\nstatic NSString *const kMapStyle = @\"JSON_STYLE_GOES_HERE\";\n\n// Set the status bar style to complement night-mode.\n- (UIStatusBarStyle)preferredStatusBarStyle {\n return UIStatusBarStyleLightContent;\n}\n\n- (void)loadView {\n GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:-33.86\n longitude:151.20\n zoom:12];\n GMSMapView *mapView = [GMSMapView mapWithFrame:CGRectZero camera:camera];\n mapView.myLocationEnabled = YES;\n\n NSError *error;\n\n // Set the map style by passing a valid JSON string.\n GMSMapStyle *style = [GMSMapStyle styleWithJSONString:kMapStyle error:&error];\n\n if (!style) {\n NSLog(@\"The style definition could not be loaded: %@\", error);\n }\n\n mapView.mapStyle = style;\n self.view = mapView;\n}\n\n@end\n \n```\n\n\u003cbr /\u003e\n\nThe following style declaration hides business points of interest (POIs) and\npublic transit icons. Paste the following style string as the value of\nthe `kMapStyle` variable:\nShow/Hide the JSON. \n\n```objective-c\n@\"[\"\n@\" {\"\n@\" \\\"featureType\\\": \\\"poi.business\\\",\"\n@\" \\\"elementType\\\": \\\"all\\\",\"\n@\" \\\"stylers\\\": [\"\n@\" {\"\n@\" \\\"visibility\\\": \\\"off\\\"\"\n@\" }\"\n@\" ]\"\n@\" },\"\n@\" {\"\n@\" \\\"featureType\\\": \\\"transit\\\",\"\n@\" \\\"elementType\\\": \\\"labels.icon\\\",\"\n@\" \\\"stylers\\\": [\"\n@\" {\"\n@\" \\\"visibility\\\": \\\"off\\\"\"\n@\" }\"\n@\" ]\"\n@\" }\"\n@\"]\"\n```\n\nJSON style declarations\n\nStyled maps use two concepts to apply colors and other style changes to a\nmap:\n\n- **Selectors** specify the geographic components that you can style on the map. These include roads, parks, bodies of water, and more, as well as their labels. The selectors include *features* and *elements* , specified as `featureType` and `elementType` properties.\n- **Stylers** are color and visibility properties that you can apply to map elements. They define the displayed color through a combination of hue, color, and lightness/gamma values.\n\nSee the [style reference](/maps/documentation/ios-sdk/style-reference) for a detailed description of the\nJSON styling options.\n\nMaps Platform Styling Wizard \n[](https://mapstyle.withgoogle.com) \n[](https://mapstyle.withgoogle.com) \n[](https://mapstyle.withgoogle.com)[](https://mapstyle.withgoogle.com/) \n[](https://mapstyle.withgoogle.com/) \n[](https://mapstyle.withgoogle.com/) \n[](https://mapstyle.withgoogle.com/) \n[](https://mapstyle.withgoogle.com/) \n[](https://mapstyle.withgoogle.com/) \n[Create\ncustom styles for the Google Maps Platform APIs](https://mapstyle.withgoogle.com)\n\nUse the [Maps Platform Styling Wizard](https://mapstyle.withgoogle.com) as a quick way\nto generate a JSON styling object. The Maps SDK for iOS supports the\nsame style declarations as the Maps JavaScript API.\n\nFull code samples\n\nThe [ApiDemos repository](https://github.com/googlemaps-samples/maps-sdk-for-ios-samples) on GitHub includes\nsamples that demonstrate the use of styling."]]