Thêm bản đồ đã tạo kiểu

Chọn nền tảng: Android iOS JavaScript

Trang này là hướng dẫn nhanh về cách tạo kiểu cho bản đồ của bạn, sử dụng chế độ ban đêm làm ví dụ.

Tổng quan

Với các lựa chọn về phong cách, bạn có thể tuỳ chỉnh bản trình bày của Google kiểu bản đồ, thay đổi cách hiển thị trực quan của các đối tượng như đường, công viên, doanh nghiệp và các địa điểm ưa thích khác. Điều này có nghĩa là bạn có thể nhấn mạnh thành phần cụ thể của bản đồ hoặc làm cho bản đồ bổ trợ cho phong cách của .

Kiểu chỉ hoạt động trên loại bản đồ kGMSTypeNormal.

Đang áp dụng kiểu cho bản đồ của bạn

Để áp dụng kiểu bản đồ tuỳ chỉnh cho một bản đồ, hãy gọi GMSMapStyle(...) để tạo một Thực thể GMSMapStyle, truyền một URL của một tệp JSON cục bộ hoặc một JSON chuỗi chứa định nghĩa kiểu. Chỉ định thực thể GMSMapStyle cho Thuộc tính mapStyle của bản đồ.

Sử dụng tệp JSON

Các ví dụ sau đây minh hoạ việc gọi GMSMapStyle(...) và truyền một URL cho tệp cục bộ:

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
      

Để xác định các lựa chọn kiểu, hãy thêm một tệp mới vào dự án có tên là style.json. rồi dán nội dung khai báo kiểu JSON sau đây để định kiểu ở chế độ ban đêm:

Sử dụng tài nguyên chuỗi

Các ví dụ sau đây cho thấy cách gọi GMSMapStyle(...) và truyền một chuỗi tài nguyên:

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
      

Để xác định các tuỳ chọn kiểu, hãy dán chuỗi kiểu sau làm giá trị của biến kMapStyle:

Khai báo kiểu JSON

Bản đồ được tạo kiểu sử dụng hai khái niệm để áp dụng màu sắc và các thay đổi khác về kiểu cho ánh xạ:

  • Bộ chọn chỉ định các thành phần địa lý mà bạn có thể kiểu trên bản đồ. Chúng bao gồm đường phố, công viên, sông nước và cũng như nhãn của chúng. Bộ chọn bao gồm các tính năngphần tử, được chỉ định là featureType và Tài sản elementType.
  • Bộ định kiểu (Stylers) là các thuộc tính màu và chế độ hiển thị mà bạn có thể áp dụng cho các yếu tố bản đồ. Mã này xác định màu hiển thị thông qua một tổ hợp các giá trị màu sắc, màu sắc và độ sáng/gamma.

Xem tài liệu tham khảo về kiểu để biết mô tả chi tiết về Tuỳ chọn định kiểu JSON.

Trình hướng dẫn tạo kiểu trên Nền tảng Maps

Sử dụng Trình hướng dẫn tạo kiểu trên nền tảng Maps để nhanh chóng tạo kiểu để tạo một đối tượng định kiểu JSON. SDK Bản đồ dành cho iOS hỗ trợ khai báo kiểu giống như API JavaScript của Maps.

Mã mẫu đầy đủ

Kho lưu trữ Apidemos trên GitHub bao gồm mẫu minh hoạ cách sử dụng kiểu.

Bước tiếp theo

Xem cách ẩn đối tượng trên bản đồ bằng cách tạo kiểu.