高级标记使用两个类来定义标记:GMSAdvancedMarker
类提供默认标记功能,GMSPinImageOptions
类包含进一步自定义的选项。本页介绍了如何通过以下方式自定义标记:
- 更改背景颜色
- 更改边框颜色
- 更改字形颜色
- 更改字形文本
- 通过 iconView 属性支持自定义视图和动画
![高级标记的组成部分](https://developers-dot-devsite-v2-prod.appspot.com/static/maps/documentation/ios-sdk/images/marker_parts.png?authuser=8&hl=zh-cn)
更改背景颜色
使用 GMSPinImageOptions.backgroundColor
选项可更改标记的背景颜色。
Swift
//... let options = GMSPinImageOptions() options.backgroundColor = .blue let pinImage = GMSPinImage(options: options) advancedMarker.icon = pinImage advancedMarker.map = mapView
Objective-C
//... GMSPinImageOptions *options = [[GMSPinImageOptions alloc] init]; options.backgroundColor = [UIColor blueColor]; GMSPinImage *pin = [GMSPinImage pinImageWithOptions:options]; customTextMarker.icon = pin; customTextMarker.map = mapView;
更改边框颜色
使用 GMSPinImageOptions.borderColor
选项可更改标记的背景颜色。
Swift
//... let options = GMSPinImageOptions() options.borderColor = .blue let pinImage = GMSPinImage(options: options) advancedMarker.icon = pinImage advancedMarker.map = mapView
Objective-C
//... GMSPinImageOptions *options = [[GMSPinImageOptions alloc] init]; options.backgroundColor = [UIColor blueColor]; GMSPinImage *pin = [GMSPinImage pinImageWithOptions:options]; advancedMarker.icon = pin; advancedMarker.map = mapView;
更改字形颜色
使用 GMSPinImageGlyph.glyphColor
可更改标记的背景颜色。
Swift
//... let options = GMSPinImageOptions() let glyph = GMSPinImageGlyph(glyphColor: .yellow) options.glyph = glyph let glyphColor = GMSPinImage(options: options) advancedMarker.icon = glyphColor advancedMarker.map = mapView
Objective-C
//... GMSPinImageOptions *options = [[GMSPinImageOptions alloc] init]; options.glyph = [[GMSPinImageGlyph alloc] initWithGlyphColor:[UIColor yellowColor]]; GMSPinImage *glyphColor = [GMSPinImage pinImageWithOptions:options]; advancedMarker.icon = glyphColor; advancedMarker.map = mapView;
更改字形文本
使用 GMSPinImageGlyph
可更改标记的字形文本。
Swift
//... let options = GMSPinImageOptions() let glyph = GMSPinImageGlyph(text: "ABC", textColor: .white) options.glyph = glyph let pinImage = GMSPinImage(options: options) advancedMarker.icon = pinImage advancedMarker.map = mapView
Objective-C
//... GMSPinImageOptions *options = [[GMSPinImageOptions alloc] init]; options.glyph = [[GMSPinImageGlyph alloc] initWithText:@"ABC" textColor:[UIColor whiteColor]]; GMSPinImage *pin = [GMSPinImage pinImageWithOptions:options]; customTextMarker.icon = pin; customTextMarker.map = mapView;
通过 iconView
属性支持自定义视图和动画
与 GMSMarker
类似,GMSAdvancedMarker
也支持带有 iconView
的标记。iconView
属性支持具有 UIView
所有可添加动画效果的属性(帧和中心除外)的动画。它不支持在同一地图上显示 iconViews
和 icons
的标记。
Swift
//... let advancedMarker = GMSAdvancedMarker(position: coordinate) advancedMarker.iconView = customView() advancedMarker.map = mapView func customView() -> UIView { // return your custom UIView. }
Objective-C
//... GMSAdvancedMarker *advancedMarker = [GMSAdvancedMarker markerWithPosition:kCoordinate]; advancedMarker.iconView = [self customView]; advancedMarker.map = self.mapView; - (UIView *)customView { // return custom view }
布局约束条件
GMSAdvancedMarker
不直接支持对其 iconView
应用布局约束条件。不过,您可以在 iconView
中为界面元素设置布局约束条件。创建视图后,应将对象 frame
或 size
传递给标记。
Swift
//do not set advancedMarker.iconView.translatesAutoresizingMaskIntoConstraints = false let advancedMarker = GMSAdvancedMarker(position: coordinate) let customView = customView() //set frame customView.frame = CGRect(0, 0, width, height) advancedMarker.iconView = customView
Objective-C
//do not set advancedMarker.iconView.translatesAutoresizingMaskIntoConstraints = NO; GMSAdvancedMarker *advancedMarker = [GMSAdvancedMarker markerWithPosition:kCoordinate]; CustomView *customView = [self customView]; //set frame customView.frame = CGRectMake(0, 0, width, height); advancedMarker.iconView = customView;