地点图标

请选择平台: Android iOS JavaScript 网络服务

地点图标表示各种类型的地点(例如咖啡馆、图书馆和博物馆)。您可以使用当前地点地点详情请求来请求 PNG 格式的图标的网址,以及相应的图标背景颜色。

如需请求地点的图标图片和背景颜色,请在请求中添加以下字段:

  • GMSPlaceFieldIconImageURL
  • GMSPlaceFieldIconBackgroundColor

以下示例演示了如何使用“当前地点”或“地点详情”请求中的图标图片和背景颜色:

Places Swift SDK for iOS

// Initialize Places Swift client
let placesClient = PlacesClient.shared
      
// A hotel in Saigon with an attribution.
let placeID = "ChIJV4k8_9UodTERU5KXbkYpSYs"

// Specify the place data types to return.
let fetchPlaceRequest = FetchPlaceRequest(
  placeID: placeID,
  placeProperties: [.displayName, .iconMaskURL, .iconBackgroundColor]
)
      
Task {
      switch await placesClient.fetchPlace(with: fetchPlaceRequest) {
        case .success(let place):
        // Print displayName using String(describing:)
        print("The selected place is: \(String(describing: place.displayName))")

        // We primarily need the iconMaskURL to proceed with image loading.
        // iconBackgroundColor can be handled even if nil.
        guard let iconMaskURL = place.iconMaskURL else {
            print("Icon mask URL not available for this place. Cannot display icon.")
            return // Exit if iconMaskURL is missing
        }

        print("The icon mask URL is: \(iconMaskURL)")
        // Print iconBackgroundColor using String(describing:)
        print("The icon background color is: \(String(describing: place.iconBackgroundColor))")


        // Asynchronously load the icon image
        DispatchQueue.global().async {
            // The iconMaskURL from the new Places SDK is already a URL
            guard let imageData = try? Data(contentsOf: iconMaskURL) else {
                print("Could not download image data from URL: \(iconMaskURL)")
                return
            }

            DispatchQueue.main.async {
                guard let iconImage = UIImage(data: imageData) else {
                    print("Could not create UIImage from downloaded data.")
                    return
                }

                // --- Example of how you might use the icon and background color ---
                // Ensure you have an imageView outlet connected in your UI
                // For example:
                // @IBOutlet weak var myImageViewContainer: UIView!
                // @IBOutlet weak var myIconImageView: UIImageView!
                // For this example, we'll create them programmatically:

                let iconBackgroundView = UIView(frame: CGRect(x: 0, y: 0, width: 50, height: 50)) // Adjust frame as needed
                // Directly assign the optional UIColor. If nil, background will be clear.
                iconBackgroundView.backgroundColor = place.iconBackgroundColor

                let imageView = UIImageView() // Initialize an empty UIImageView
                imageView.frame = iconBackgroundView.bounds // Make icon view same size as background
                imageView.contentMode = .scaleAspectFit // Adjust content mode as needed

                // Tint the icon image (mask) to white
                // The icon from iconMaskURL is intended to be used as a mask.
                let templateImage = iconImage.withRenderingMode(.alwaysTemplate)
                imageView.image = templateImage
                imageView.tintColor = UIColor.white

                // Add the image view on top of the background view
                iconBackgroundView.addSubview(imageView)
        }
      }
  }
}

Swift

// Icon image URL
let url = URL(string: place.iconImageUrl)
DispatchQueue.global().async {
    guard let url = url,
        let imageData = try? Data(contentsOf: url) else {
      print("Could not get image")
      return
    }
    DispatchQueue.main.async {
      let iconImage = UIImage(data: iconImageData)

      // Icon image background color
      let iconBackgroundView = UIView(frame: .zero)
      iconBackgroundView.backgroundColor = place.iconBackgroundColor

      // Change icon image color to white
      let templateImage = iconImage.imageWithRenderingMode(UIImageRenderingModeAlwaysTemplate)
      imageView.image = templateImage
      imageView.tintColor = UIColor.white
    }
}

Objective-C

GMSPlace *place;

dispatch_async(dispatch_get_global_queue(0, 0), ^{
  // Icon image URL
  NSData * iconImageData = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString: place.iconImageUrl]];
      if (!iconImageData)
          return;
      dispatch_async(dispatch_get_main_queue(), ^{
        UIImage *iconImage = [UIImage imageWithData:iconImageData];

        // Icon image background color
        UIView *iconBackgroundView = [[UIView alloc] initWithFrame:CGRectZero];
        [iconBackgroundView setBackgroundColor:place.iconBackgroundColor];

        // Change icon image color to white
        iconImage = [iconImage imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
        [imageView setTintColor:[UIColor whiteColor]];
      });
});

字段

每个地点图标均包含以下字段:

  • iconImageUrl 会返回非彩色 PNG 图标的基础网址。
  • iconBackgroundColor 会返回地点类别的默认 UIExtendedSRGBColorSpace 颜色代码。

地点图标和背景颜色请求

下表按类别显示了所有可用的地点图标。默认情况下,这些图标以黑色字形显示。图标背景颜色取决于地点的类别。

地点类别:食品和饮料
(图标背景颜色 #FF9E67)
酒吧
酒吧、夜总会
咖啡馆
咖啡馆
餐馆
餐馆、糕点店
地点类别:零售
(图标背景颜色 #4B96F3)
书店
图书、服装、电子产品、珠宝、鞋类、购物中心/商场
便利店
便利店
杂货店
杂货店、超市
药店
药店
地点类别:服务
(图标背景颜色 #909CE1)
ATM
ATM
银行
银行
加油站
加油站
住宿
住宿
邮局
邮局
地点类别:娱乐
(图标背景颜色 #13B5C7)
水族馆、旅游
水族馆、旅游
高尔夫
高尔夫
历史古迹
历史古迹
电影院
电影院
博物馆
博物馆
剧院
剧院
地点类别:交通
(图标背景颜色 #10BDFF)
机场
机场
公交车
公交车、拼车、出租车
火车/轨道交通
火车/轨道交通
地点类别:市政/一般/宗教
(图标背景颜色 #7B9EB0)
墓地
墓地
市政建筑
市政建筑
图书馆
图书馆
纪念碑
纪念碑
停车场
停车场
学校(小学、中学、大学)
学校(小学、中学、大学)
宗教场所(基督教)
宗教场所(基督教)
宗教场所(印度教)
宗教场所(印度教)
宗教场所(伊斯兰教)
宗教场所(伊斯兰教)
宗教场所(耆那教)
宗教场所(耆那教)
宗教场所(犹太教)
宗教场所(犹太教)
宗教场所(锡克教)
宗教场所(锡克教)
一般业务
一般业务
地点类别:户外
(图标背景颜色 #4DB546)
乘船
乘船
野营
野营
公园
公园
体育馆
体育馆
动物园
动物园
地点类别:紧急状况
(图标背景颜色 #F88181)
医院
医院
警察局
警察局