google maps iOS SDK:用作标记的自定义图标
Android API为此提供了一个非常方便的类。在我的Android应用程序中使用google maps iOS SDK:用作标记的自定义图标,ios,swift,google-maps,google-maps-markers,google-maps-sdk-ios,Ios,Swift,Google Maps,Google Maps Markers,Google Maps Sdk Ios,Android API为此提供了一个非常方便的类。在我的Android应用程序中使用IconGenerator,我可以轻松制作一个标记: 是一个简单的矩形,颜色由我选择 调整大小以容纳任意长度的文本 不是一个信息窗口-我希望标记本身包含android版本的文本,如下图所示 //Android-使用IconGenerator解决问题 IconGenerator IconGenerator=新IconGenerator(上下文); iconGenerator.setStyle(iconGenera
IconGenerator
,我可以轻松制作一个标记:
//Android-使用IconGenerator解决问题
IconGenerator IconGenerator=新IconGenerator(上下文);
iconGenerator.setStyle(iconGenerator.STYLE_绿色);//或任何其他颜色
位图iconBitmap=iconGenerator.makeIcon(myString);
标记m=新标记选项().icon(BitmapDescriptorFactory.fromBitmap(iconBitmap))
.位置(myLatLng);
地图。添加标记(m);//地图是一个com.google.android.gms.maps.GoogleMap
有没有一种方法可以在iOS中使用Swift做像这样简单的事情?
iosapi已经有了一个允许“标记定制”的版本,但我不知道如何将它应用到这个用例中
//iOS(Swift)-我不知道如何创建上面代码中的图标
让标记器=GMSMarker(位置:myLatLng)
marker.icon=//如何设置为具有所选颜色/文本的矩形?
marker.map=map//map是一个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中找到一些东西,你可以创建一个简单的小矩形图像,像我上面所做的那样使用它,并根据你的需要改变颜色
或者,如果您想要一个包含文本的矩形,您可以创建一个带有一些标签的小型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;
}
现在,我调用了这个方法,同时将icon分配给GMSMarker,如下所示:
marker.icon = [self drawText:@"$33.6" inImage:[UIImage imageNamed:@"icon-marker"]];
它将生成如下所示的图像图标:
在这里,我保持了背景图像的大小固定,因为我需要。您仍然可以自定义它以根据文本大小以及多行进行调整
更新
Swift中的更新代码:
func drawText(text:NSString, inImage:UIImage) -> UIImage? {
let font = UIFont.systemFont(ofSize: 11)
let size = inImage.size
//UIGraphicsBeginImageContext(size)
let scale = UIScreen.main.scale
UIGraphicsBeginImageContextWithOptions(inImage.size, false, scale)
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 = [ NSAttributedString.Key.font : font, NSAttributedString.Key.paragraphStyle : style, NSAttributedString.Key.foregroundColor : UIColor.black ]
let textSize = text.size(withAttributes: attributes as? [NSAttributedString.Key : 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? [NSAttributedString.Key : Any])
let resultImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return resultImage
}
您只需在谷歌地图中添加一个自定义视图作为标记
let marker = GMSMarker(position: coordinate)
marker.iconView = view // Your Custom view here
您可以使用imageView(用于包含橙色框)和标签(用于文本)我试图将答案改写为Swift 3。希望它对你有用。但如前所述,定制视图确实更容易
如果您只有一张图像,最简单的实现方法是:
marker.icon = #imageLiteral(resourceName: "fault_marker")
1) 在最新的XCode中写入marker.icon=“imageLiteral”
2) 双击刚刚出现的虚拟图像图标
3) 选择所需图像。这里是Swift 5版本的答案的Swift转换
func drawTextT(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 = [ NSAttributedString.Key.font : font, NSAttributedString.Key.paragraphStyle : style, NSAttributedString.Key.foregroundColor : UIColor.red ]
//let textSize = text.size(attributes: attributes as? [String : Any])
let textSize = text.size(withAttributes: attributes as? [NSAttributedString.Key : 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? [NSAttributedString.Key : Any] )
let resultImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return resultImage
}
更改图标的简单而简单的方法。只需将这3个图标(default marker.png)替换为您的图标(1x、2x、3x) 在Google集群中,标记(图标)更改出现问题
这就是我提到的,创建一个与android相同的
UIView
,它包含了你的$price,然后从中创建一个UIImage
,我提到了它的相同功能。在看到你更新的答案之前,我编辑了我的问题。我明天会实施这个。同时,请您更具体地说明UIVIew/label代码是什么样子的。好的,我将尝试根据您的需要创建一个类似的视图。它只是一个将被转换为图像的视图。您可以使用BezierPath实现类似于视图的向下箭头点形状。另外,如果您不想看到我在标记上所做的操作,只需删除mapView.selectedMarker=marker
行,它将是一个简单的标记。您可以编辑该标记以使用swift吗?如果这是在swift中,并且包含了将图像调整为最多可容纳70个字符,并将图像缩小为最多可容纳3个字符的代码,那么我会选择它。你能为最后一段提供链接吗?@kelalaka:我已经完成了我自己的编程。。如何提供代码的链接?如果您需要任何其他帮助,请告诉我您是否可以在swift 4或4.2中更新您的代码???@BhaveshM.Sarsawa:现在检查我已尝试使用imageWithView
和Let label=UILabel()
和label.widthAnchor.constraintEqualToConstant(50.0).active=true
但由于UIGraphicsGetCurrentContext()而崩溃
insideimageWithView
为零。如何设置中心标题和片段?这似乎是最快捷的方法。
func drawTextT(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 = [ NSAttributedString.Key.font : font, NSAttributedString.Key.paragraphStyle : style, NSAttributedString.Key.foregroundColor : UIColor.red ]
//let textSize = text.size(attributes: attributes as? [String : Any])
let textSize = text.size(withAttributes: attributes as? [NSAttributedString.Key : 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? [NSAttributedString.Key : Any] )
let resultImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return resultImage
}
//func to get Image view
// Url String :- Your image coming from server
//image :- Background image
func drawImageWithProfilePic(urlString:String, image: UIImage) -> UIImageView {
let imgView = UIImageView(image: image)
imgView.frame = CGRect(x: 0, y: 0, width: 90, height: 90)
let picImgView = UIImageView()
picImgView.sd_setImage(with:URL(string: urlString))
picImgView.frame = CGRect(x: 0, y: 0, width: 40, height: 40)
imgView.addSubview(picImgView)
picImgView.center.x = imgView.center.x
picImgView.center.y = imgView.center.y-10
picImgView.layer.cornerRadius = picImgView.frame.width/2
picImgView.clipsToBounds = true
imgView.setNeedsLayout()
picImgView.setNeedsLayout()
// let newImage = imageWithView(view: imgView)
// return newImage
return imgView
}
//SHOW ON MAP
let marker = GMSMarker()
marker.position = CLLocationCoordinate2D(latitude: Double(lat)!, longitude: Double(long)!)
marker.iconView = self.drawImageWithProfilePic(urlString:getProviderImage,image: UIImage.init(named: "red")!)