gpt4 book ai didi

ios - 谷歌地图 iOS SDK : custom icons to be used as markers

转载 作者:IT王子 更新时间:2023-10-29 05:24:47 32 4
gpt4 key购买 nike

Android API 有一个非常方便的类,IconGenerator .使用 IconGenerator 在我的 Android 应用中,我可以轻松地制作一个标记:

  1. 是一个带有我选择的颜色的简单矩形。
  2. 调整大小以容纳任何长度的文本。
  3. 不是信息窗口 - 我希望标记本身包含文本,如下图所示,来自 android 版本。

enter image description here

// Android - problem solved with IconGenerator
IconGenerator iconGenerator = new IconGenerator(context);
iconGenerator.setStyle(IconGenerator.STYLE_GREEN); // or any other color
Bitmap iconBitmap = iconGenerator.makeIcon(myString);
Marker m = new MarkerOptions().icon(BitmapDescriptorFactory.fromBitmap(iconBitmap))
.position(myLatLng);
map.addMarker(m); // map is a com.google.android.gms.maps.GoogleMap

有没有办法在 iOS 中使用 Swift 做这么简单的事情?有一个recent release允许“标记自定义”的 iOS API 的一部分,但我不知道如何将其应用于此用例。

// iOS (Swift) - I don't know how to create the icon as in code above
let marker = GMSMarker(position: myLatLng)
marker.icon = // How can I set to a rectangle with color/text of my choosing?
marker.map = map // map is a GMSMapView

最佳答案

这是我做的

let marker = GMSMarker()

// I have taken a pin image which is a custom image
let markerImage = UIImage(named: "mapMarker")!.withRenderingMode(.alwaysTemplate)

//creating a marker view
let markerView = UIImageView(image: markerImage)

//changing the tint color of the image
markerView.tintColor = UIColor.red

marker.position = CLLocationCoordinate2D(latitude: 28.7041, longitude: 77.1025)

marker.iconView = markerView
marker.title = "New Delhi"
marker.snippet = "India"
marker.map = mapView

//comment this line if you don't wish to put a callout bubble
mapView.selectedMarker = marker

输出是

enter image description here

我的标记图像是

enter image description here

您可以根据需要更改颜色。此外,如果您想要矩形的东西,您可以创建一个简单的小矩形图像并像我上面那样使用它并更改您需要的颜色。

或者,如果您想要一个包含文本的矩形,您可以只创建一个带有一些标签的小 UIView,然后将该 UIView 转换为 UIImage 并且可以做同样的事情。

//function to convert the given UIView into a UIImage
func imageWithView(view:UIView) -> UIImage {
UIGraphicsBeginImageContextWithOptions(view.bounds.size, false, 0.0)
view.layer.render(in: UIGraphicsGetCurrentContext()!)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image!
}

希望对您有所帮助!

关于ios - 谷歌地图 iOS SDK : custom icons to be used as markers,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40210145/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com