谷歌地图iOS SDK:用作标记的自定义图标 [英] google maps iOS SDK: custom icons to be used as markers
问题描述
Android API有一个非常方便的类,
// Android - 使用IconGenerator解决问题$ b $ IconGenerator iconGenerator = new IconGenerator(context);
iconGenerator.setStyle(IconGenerator.STYLE_GREEN); //或任何其他颜色
位图iconBitmap = iconGenerator.makeIcon(myString);
Marker m = new MarkerOptions()。icon(BitmapDescriptorFactory.fromBitmap(iconBitmap))
.position(myLatLng);
map.addMarker(m); // map是一个com.google.android.gms.maps.GoogleMap
有没有办法在使用Swift的 iOS中执行如此简单的操作?
已有
我的标记图像是
您可以根据需要更改颜色。同样,如果你想要一些rectange中的东西,你可以创建一个简单的小矩形图像,像上面那样使用它,并改变你需要的颜色。
或者如果你想要一个带有文本的矩形,你可以用一些标签创建一个小的 UIView
,然后将 UIView
转换成 UIImage
并且可以做同样的事情。
//函数转换给定的UIView到UIImage中
func imageWithView(view:UIView) - > UIImage {
UIGraphicsBeginImageContextWithOptions(view.bounds.size,false,0.0)
view.layer.render(in:UIGraphicsGetCurrentContext()!)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext ()
返回图片!
}
希望它有帮助!!
The Android API has a very convenient class for this, IconGenerator
. Using the IconGenerator
in my Android app, I can easily make a marker that:
- is a simple rectangle with the color of my choosing.
- resizes to hold text of any length.
- is NOT an info window - I'd like the marker itself to contain the text as shown in the image below from the android version.
// 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
Is there a way to do something as simple as this in iOS using Swift? There has been a recent release of the iOS api that allows "marker customization", but I don't see how to apply it to this use case.
// 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
Here is what I have done
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
The output is
And my marker image was
You can change your color as per your need. Also if you want something in rectange, you can just create a simple small rectangular image and use it like I did above and change the color of your need.
Or if you want a rectangle with text within it, you can just create a small UIView
with some label and then convert that UIView
in UIImage
and can do the same thing.
//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!
}
Hope it helps!!
这篇关于谷歌地图iOS SDK:用作标记的自定义图标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!