谷歌地图iOS SDK:用作标记的自定义图标
Android API有一个非常方便的类IconGenerator
。 在我的Android应用程序中使用IconGenerator
,我可以很容易地创build一个标记:
- 是一个简单的矩形与我select的颜色。
- resize来保存任何长度的文本。
- 是不是一个信息窗口 – 我想标记本身包含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做这样简单的事情? 最近有一个允许“标记自定义”的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
输出是
而我的标记图像是
您可以根据需要更改颜色。 另外,如果你想要的东西在rectange中,你可以创build一个简单的小矩形图像,像上面那样使用它,并改变你需要的颜色。
或者如果你想要一个带有文本的矩形,你可以创build一个带有一些标签的小UIView
,然后在UIImage
转换UIView
,并且可以做同样的事情。
//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! }
希望能帮助到你!!
这是我为解决同样的问题而做的,你正在面对的。
我在图片资源中添加了以下图片,
现在我在我的代码中添加下面的方法:
-(UIImage*)drawText:(NSString*)text inImage:(UIImage*)image { UIFont *font = [UIFont boldSystemFontOfSize:11]; CGSize size = image.size; UIGraphicsBeginImageContextWithOptions(size, NO, 0.0f); [image drawInRect:CGRectMake(0, 0, size.width, size.height)]; CGRect rect = CGRectMake(0, 0, image.size.width, image.size.height); NSMutableParagraphStyle *paragraphStyle = [[NSParagraphStyle defaultParagraphStyle] mutableCopy]; paragraphStyle.alignment = NSTextAlignmentCenter; NSDictionary *attributes = @{ NSFontAttributeName : font, NSParagraphStyleAttributeName : paragraphStyle, NSForegroundColorAttributeName : [UIColor redColor] }; CGSize textSize = [text sizeWithAttributes:attributes]; CGRect textRect = CGRectMake((rect.size.width-textSize.width)/2, (rect.size.height-textSize.height)/2 - 2, textSize.width, textSize.height); [text drawInRect:CGRectIntegral(textRect) withAttributes:attributes]; UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return newImage; }
现在,我调用这个方法,同时给GMSMarker分配图标,就像这样:
marker.icon = [self drawText:@"$33.6" inImage:[UIImage imageNamed:@"icon-marker"]];
它会生成如下的图像图标:
在这里,我保持背景图像的大小固定,因为我需要。 您仍然可以自定义它以根据文本大小以及多行进行调整。
我试图重写Mehul Thakkar回答Swift 3.希望它能为你工作。 但Dari说,自定义视图确实更容易。
func drawText(text:NSString, inImage:UIImage) -> UIImage? { let font = UIFont.systemFont(ofSize: 11) let size = inImage.size UIGraphicsBeginImageContext(size) inImage.draw(in: CGRect(x: 0, y: 0, width: size.width, height: size.height)) let style : NSMutableParagraphStyle = NSMutableParagraphStyle.default.mutableCopy() as! NSMutableParagraphStyle style.alignment = .center let attributes:NSDictionary = [ NSFontAttributeName : font, NSParagraphStyleAttributeName : style, NSForegroundColorAttributeName : UIColor.red ] let textSize = text.size(attributes: attributes as? [String : Any]) let rect = CGRect(x: 0, y: 0, width: inImage.size.width, height: inImage.size.height) let textRect = CGRect(x: (rect.size.width - textSize.width)/2, y: (rect.size.height - textSize.height)/2 - 2, width: textSize.width, height: textSize.height) text.draw(in: textRect.integral, withAttributes: attributes as? [String : Any]) let resultImage = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() return resultImage }
您可以简单地在Google地图中添加自定义视图作为标记。
let marker = GMSMarker(position: coordinate) marker.iconView = view // Your Custom view here
您可以使用imageView(用于包含该橙色框)和标签(用于文本)在其上方
- 在swift中实现自定义markerInfoWindow不起作用
- 无法使用Xcode 7和带有Podfile的GoogleMaps将IOS应用上传到iTunes
- 错误使用未parsing的标识符GMSPlacePickerConfig&GooglePlacePicker? (Google Places 2.0)
- 从iPhone应用程序启动Google地图应用程序。
- 在Google地图中移动相机
- 更改Google地图的选定标记或更改标记的颜色?
- 谷歌地图V2 Android和iOS放置latlng小数位
- 例外情况:在使用之前,Google Maps SDK for iOS必须通过进行初始化
- 显示iphone sdk中的多个点之间的路线