iOS开发:如何修改GMS Marker颜色并添加自定义标记?
替换GMS目的地标记为自定义样式并修改颜色为蓝色
方法一:修改默认标记的颜色
如果仅需将默认红色标记改为蓝色,无需替换自定义图标,直接设置标记的tintColor属性即可:
// MARK: Marker for destination location let destinationMarker = GMSMarker() destinationMarker.position = CLLocationCoordinate2D( latitude: selectedLocation!.latitude, longitude: selectedLocation!.longitude ) destinationMarker.title = "Destination" // 设置标记颜色为蓝色 destinationMarker.tintColor = .blue destinationMarker.map = self.mapView
方法二:使用自定义图片作为标记
如果需要完全替换为自定义设计的蓝色标记图标,通过设置icon属性实现:
// MARK: Marker for destination location let destinationMarker = GMSMarker() destinationMarker.position = CLLocationCoordinate2D( latitude: selectedLocation!.latitude, longitude: selectedLocation!.longitude ) destinationMarker.title = "Destination" // 加载自定义蓝色标记图片替换默认图标 if let customMarkerImage = UIImage(named: "custom_blue_marker") { destinationMarker.icon = customMarkerImage } destinationMarker.map = self.mapView
完整修改后的代码示例
let sourceMarker = GMSMarker() sourceMarker.position = CLLocationCoordinate2D( latitude: currentLocationCoordinates!.coordinate.latitude, longitude: currentLocationCoordinates!.coordinate.longitude ) sourceMarker.title = "Current Location" sourceMarker.map = self.mapView // MARK: Marker for destination location let destinationMarker = GMSMarker() destinationMarker.position = CLLocationCoordinate2D( latitude: selectedLocation!.latitude, longitude: selectedLocation!.longitude ) destinationMarker.title = "Destination" // 方式1:修改默认标记颜色为蓝色 destinationMarker.tintColor = .blue // 方式2:使用自定义图片(注释方式1,启用下方代码即可) // if let customMarkerImage = UIImage(named: "custom_blue_marker") { // destinationMarker.icon = customMarkerImage // } destinationMarker.map = self.mapView
内容的提问来源于stack exchange,提问作者Muhammad Mustafeez Ur Rehman
相关产品推荐
相关产品推荐

