Android API 为此提供了一个非常方便的类,IconGenerator http://googlemaps.github.io/android-maps-utils/javadoc/com/google/maps/android/ui/IconGenerator.html。使用IconGenerator
在我的 Android 应用程序中,我可以轻松地做一个标记:
- 是一个简单的矩形,颜色由我选择。
- 调整大小以容纳任意长度的文本。
- is NOT信息窗口 - 我希望标记本身包含 Android 版本中的文本,如下图所示。
// 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?
已经有一个最近发布 https://github.com/googlemaps/google-maps-ios-utils/releases/tag/v1.1.0允许“标记自定义”的 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
输出是
我的标记图像是
您可以根据需要更改颜色。另外,如果您想要矩形的东西,您可以创建一个简单的小矩形图像并像我上面那样使用它并更改您需要的颜色。
或者,如果您想要一个其中包含文本的矩形,您可以创建一个小的UIView
带有一些标签,然后将其转换UIView
in 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!
}
希望能帮助到你!!
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)