เพิ่มแผนที่ที่มีการจัดรูปแบบ

เลือกแพลตฟอร์ม Android iOS JavaScript

หน้านี้เป็นคำแนะนำสั้นๆ เกี่ยวกับการจัดรูปแบบแผนที่โดยใช้โหมดกลางคืนเป็นตัวอย่าง

เพื่อหลีกเลี่ยงความขัดแย้งที่อาจเกิดขึ้น

ภาพรวม

ตัวเลือกสไตล์ช่วยให้คุณปรับแต่งการแสดงแผนที่ Google รูปแบบมาตรฐานได้ เช่น เปลี่ยนการแสดงภาพสถานที่ต่างๆ เช่น ถนน สวนสาธารณะ ธุรกิจ และจุดที่น่าสนใจอื่นๆ ซึ่งหมายความว่าคุณสามารถเน้นองค์ประกอบบางอย่างของแผนที่หรือทำให้แผนที่เสริมสไตล์ของแอปได้

การจัดรูปแบบใช้ได้กับแผนที่ประเภท kGMSTypeNormal เท่านั้น

การใช้รูปแบบกับแผนที่

หากต้องการใช้รูปแบบแผนที่ที่กำหนดเองกับแผนที่ ให้เรียกใช้ GMSMapStyle(...) เพื่อสร้างGMSMapStyleอินสแตนซ์ โดยส่ง URL ของไฟล์ JSON ในเครื่อง หรือสตริง JSON ที่มีคำจำกัดความของสไตล์ กําหนดอินสแตนซ์ GMSMapStyle ให้กับพร็อพเพอร์ตี้ mapStyle ของแผนที่

การใช้ไฟล์ JSON

ตัวอย่างต่อไปนี้แสดงการเรียก GMSMapStyle(...) และการส่ง URL ของไฟล์ในเครื่อง

SwiftObjective-C
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
  }
}
      
#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 ต่อไปนี้สำหรับการจัดสไตล์โหมดกลางคืน

[
  {
    "featureType": "all",
    "elementType": "geometry",
    "stylers": [
      {
        "color": "#242f3e"
      }
    ]
  },
  {
    "featureType": "all",
    "elementType": "labels.text.stroke",
    "stylers": [
      {
        "lightness": -80
      }
    ]
  },
  {
    "featureType": "administrative",
    "elementType": "labels.text.fill",
    "stylers": [
      {
        "color": "#746855"
      }
    ]
  },
  {
    "featureType": "administrative.locality",
    "elementType": "labels.text.fill",
    "stylers": [
      {
        "color": "#d59563"
      }
    ]
  },
  {
    "featureType": "poi",
    "elementType": "labels.text.fill",
    "stylers": [
      {
        "color": "#d59563"
      }
    ]
  },
  {
    "featureType": "poi.park",
    "elementType": "geometry",
    "stylers": [
      {
        "color": "#263c3f"
      }
    ]
  },
  {
    "featureType": "poi.park",
    "elementType": "labels.text.fill",
    "stylers": [
      {
        "color": "#6b9a76"
      }
    ]
  },
  {
    "featureType": "road",
    "elementType": "geometry.fill",
    "stylers": [
      {
        "color": "#2b3544"
      }
    ]
  },
  {
    "featureType": "road",
    "elementType": "labels.text.fill",
    "stylers": [
      {
        "color": "#9ca5b3"
      }
    ]
  },
  {
    "featureType": "road.arterial",
    "elementType": "geometry.fill",
    "stylers": [
      {
        "color": "#38414e"
      }
    ]
  },
  {
    "featureType": "road.arterial",
    "elementType": "geometry.stroke",
    "stylers": [
      {
        "color": "#212a37"
      }
    ]
  },
  {
    "featureType": "road.highway",
    "elementType": "geometry.fill",
    "stylers": [
      {
        "color": "#746855"
      }
    ]
  },
  {
    "featureType": "road.highway",
    "elementType": "geometry.stroke",
    "stylers": [
      {
        "color": "#1f2835"
      }
    ]
  },
  {
    "featureType": "road.highway",
    "elementType": "labels.text.fill",
    "stylers": [
      {
        "color": "#f3d19c"
      }
    ]
  },
  {
    "featureType": "road.local",
    "elementType": "geometry.fill",
    "stylers": [
      {
        "color": "#38414e"
      }
    ]
  },
  {
    "featureType": "road.local",
    "elementType": "geometry.stroke",
    "stylers": [
      {
        "color": "#212a37"
      }
    ]
  },
  {
    "featureType": "transit",
    "elementType": "geometry",
    "stylers": [
      {
        "color": "#2f3948"
      }
    ]
  },
  {
    "featureType": "transit.station",
    "elementType": "labels.text.fill",
    "stylers": [
      {
        "color": "#d59563"
      }
    ]
  },
  {
    "featureType": "water",
    "elementType": "geometry",
    "stylers": [
      {
        "color": "#17263c"
      }
    ]
  },
  {
    "featureType": "water",
    "elementType": "labels.text.fill",
    "stylers": [
      {
        "color": "#515c6d"
      }
    ]
  },
  {
    "featureType": "water",
    "elementType": "labels.text.stroke",
    "stylers": [
      {
        "lightness": -20
      }
    ]
  }
]

การใช้ทรัพยากรสตริง

ตัวอย่างต่อไปนี้แสดงการเรียก GMSMapStyle(...) และการส่งทรัพยากรสตริง

SwiftObjective-C
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
  }
}
      
@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
      

หากต้องการกําหนดตัวเลือกสไตล์ ให้วางสตริงสไตล์ต่อไปนี้เป็นค่าของตัวแปร kMapStyle

@"["
@"  {"
@"    \"featureType\": \"all\","
@"    \"elementType\": \"geometry\","
@"    \"stylers\": ["
@"      {"
@"        \"color\": \"#242f3e\""
@"      }"
@"    ]"
@"  },"
@"  {"
@"    \"featureType\": \"all\","
@"    \"elementType\": \"labels.text.stroke\","
@"    \"stylers\": ["
@"      {"
@"        \"lightness\": -80"
@"      }"
@"    ]"
@"  },"
@"  {"
@"    \"featureType\": \"administrative\","
@"    \"elementType\": \"labels.text.fill\","
@"    \"stylers\": ["
@"      {"
@"        \"color\": \"#746855\""
@"      }"
@"    ]"
@"  },"
@"  {"
@"    \"featureType\": \"administrative.locality\","
@"    \"elementType\": \"labels.text.fill\","
@"    \"stylers\": ["
@"      {"
@"        \"color\": \"#d59563\""
@"      }"
@"    ]"
@"  },"
@"  {"
@"    \"featureType\": \"poi\","
@"    \"elementType\": \"labels.text.fill\","
@"    \"stylers\": ["
@"      {"
@"        \"color\": \"#d59563\""
@"      }"
@"    ]"
@"  },"
@"  {"
@"    \"featureType\": \"poi.park\","
@"    \"elementType\": \"geometry\","
@"    \"stylers\": ["
@"      {"
@"        \"color\": \"#263c3f\""
@"      }"
@"    ]"
@"  },"
@"  {"
@"    \"featureType\": \"poi.park\","
@"    \"elementType\": \"labels.text.fill\","
@"    \"stylers\": ["
@"      {"
@"        \"color\": \"#6b9a76\""
@"      }"
@"    ]"
@"  },"
@"  {"
@"    \"featureType\": \"road\","
@"    \"elementType\": \"geometry.fill\","
@"    \"stylers\": ["
@"      {"
@"        \"color\": \"#2b3544\""
@"      }"
@"    ]"
@"  },"
@"  {"
@"    \"featureType\": \"road\","
@"    \"elementType\": \"labels.text.fill\","
@"    \"stylers\": ["
@"      {"
@"        \"color\": \"#9ca5b3\""
@"      }"
@"    ]"
@"  },"
@"  {"
@"    \"featureType\": \"road.arterial\","
@"    \"elementType\": \"geometry.fill\","
@"    \"stylers\": ["
@"      {"
@"        \"color\": \"#38414e\""
@"      }"
@"    ]"
@"  },"
@"  {"
@"    \"featureType\": \"road.arterial\","
@"    \"elementType\": \"geometry.stroke\","
@"    \"stylers\": ["
@"      {"
@"        \"color\": \"#212a37\""
@"      }"
@"    ]"
@"  },"
@"  {"
@"    \"featureType\": \"road.highway\","
@"    \"elementType\": \"geometry.fill\","
@"    \"stylers\": ["
@"      {"
@"        \"color\": \"#746855\""
@"      }"
@"    ]"
@"  },"
@"  {"
@"    \"featureType\": \"road.highway\","
@"    \"elementType\": \"geometry.stroke\","
@"    \"stylers\": ["
@"      {"
@"        \"color\": \"#1f2835\""
@"      }"
@"    ]"
@"  },"
@"  {"
@"    \"featureType\": \"road.highway\","
@"    \"elementType\": \"labels.text.fill\","
@"    \"stylers\": ["
@"      {"
@"        \"color\": \"#f3d19c\""
@"      }"
@"    ]"
@"  },"
@"  {"
@"    \"featureType\": \"road.local\","
@"    \"elementType\": \"geometry.fill\","
@"    \"stylers\": ["
@"      {"
@"        \"color\": \"#38414e\""
@"      }"
@"    ]"
@"  },"
@"  {"
@"    \"featureType\": \"road.local\","
@"    \"elementType\": \"geometry.stroke\","
@"    \"stylers\": ["
@"      {"
@"        \"color\": \"#212a37\""
@"      }"
@"    ]"
@"  },"
@"  {"
@"    \"featureType\": \"transit\","
@"    \"elementType\": \"geometry\","
@"    \"stylers\": ["
@"      {"
@"        \"color\": \"#2f3948\""
@"      }"
@"    ]"
@"  },"
@"  {"
@"    \"featureType\": \"transit.station\","
@"    \"elementType\": \"labels.text.fill\","
@"    \"stylers\": ["
@"      {"
@"        \"color\": \"#d59563\""
@"      }"
@"    ]"
@"  },"
@"  {"
@"    \"featureType\": \"water\","
@"    \"elementType\": \"geometry\","
@"    \"stylers\": ["
@"      {"
@"        \"color\": \"#17263c\""
@"      }"
@"    ]"
@"  },"
@"  {"
@"    \"featureType\": \"water\","
@"    \"elementType\": \"labels.text.fill\","
@"    \"stylers\": ["
@"      {"
@"        \"color\": \"#515c6d\""
@"      }"
@"    ]"
@"  },"
@"  {"
@"    \"featureType\": \"water\","
@"    \"elementType\": \"labels.text.stroke\","
@"    \"stylers\": ["
@"      {"
@"        \"lightness\": -20"
@"      }"
@"    ]"
@"  }"
@"]"

การประกาศสไตล์ JSON

แผนที่ที่มีสไตล์ใช้แนวคิด 2 อย่างในการใช้สีและการเปลี่ยนแปลงสไตล์อื่นๆ กับแผนที่

  • ตัวเลือกจะระบุองค์ประกอบทางภูมิศาสตร์ที่คุณจัดรูปแบบบนแผนที่ได้ ซึ่งรวมถึงถนน สวนสาธารณะ แหล่งน้ำ และอื่นๆ รวมถึงป้ายกำกับ ตัวเลือกประกอบด้วยฟีเจอร์และองค์ประกอบ ซึ่งระบุเป็นพร็อพเพอร์ตี้ featureType และ elementType
  • สไตล์เลอร์คือคุณสมบัติสีและการมองเห็นที่คุณใช้กับองค์ประกอบแผนที่ได้ โดยค่าเหล่านี้จะกำหนดสีที่แสดงผ่านค่าความสว่าง/ค่าแกมมา เฉดสี และสี

ดูคำอธิบายโดยละเอียดของตัวเลือกการจัดรูปแบบ JSON ได้ที่ข้อมูลอ้างอิงเกี่ยวกับสไตล์

วิซาร์ดการจัดรูปแบบ Maps Platform

ใช้วิซาร์ดการจัดสไตล์แพลตฟอร์ม Maps เป็นวิธีที่รวดเร็วในการสร้างออบเจ็กต์การจัดสไตล์ JSON Maps SDK สำหรับ iOS รองรับการประกาศสไตล์แบบเดียวกับ Maps JavaScript API

ตัวอย่างโค้ดแบบเต็ม

ที่เก็บ ApiDemos ใน GitHub มีตัวอย่างที่แสดงการใช้การจัดรูปแบบ

ขั้นตอนถัดไป

ดูวิธีซ่อนองค์ประกอบบนแผนที่ด้วยการจัดสไตล์