Reputation: 987
I need to create a GMSMarker
for my Google Maps screen in an iOS app. I need the marker to be a combination of images, i.e. a Generic marker image and a User Image.
eg:
Inside this marker, I need to fit the User's image. I have both the images in my assets. it tried
func image(byDrawingImage image: UIImage, inRect rect: CGRect) -> UIImage! {
UIGraphicsBeginImageContext(size)
draw(in: CGRect(x: 0, y: 0, width: size.width, height: size.height))
image.draw(in: rect)
let result = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return result
}
But this returned an imperfect image. Is there an alternate solution? Note: the example marker image provided here is not the one I actually use. That is a rectangular marker.
Upvotes: 3
Views: 2210
Reputation: 1371
Here I am providing you some sample code, try it, it will work & customize/modify according to your requirement:
let marker = GMSMarker()
let lat = Double("13.063754")
let long = Double("80.24358699999993")
marker.position = CLLocationCoordinate2DMake(lat!,long!)
///Creating UIView for Custom Marker
let DynamicView=UIView(frame: CGRectMake(0, 0, 50, 50))
DynamicView.backgroundColor=UIColor.clearColor()
//Creating Marker Pin imageview for Custom Marker
var imageViewForPinMarker : UIImageView
imageViewForPinMarker = UIImageView(frame:CGRectMake(0, 0, 40, 50));
imageViewForPinMarker.image = UIImage(named:"LocationPin")
//Creating User Profile imageview
var imageViewForUserProfile : UIImageView
imageViewForUserProfile = UIImageView(frame:CGRectMake(0, 0, 35, 35));
imageViewForUserProfile.image = UIImage(named:"userprofile")
//Adding userprofile imageview inside Marker Pin Imageview
imageViewForPinMarker.addSubview(imageViewForUserProfile)
//Adding Marker Pin Imageview isdie view for Custom Marker
DynamicView.addSubview(imageViewForPinMarker)
//Converting dynamic uiview to get the image/marker icon.
UIGraphicsBeginImageContextWithOptions(DynamicView.frame.size, false, UIScreen.mainScreen().scale)
DynamicView.layer.renderInContext(UIGraphicsGetCurrentContext()!)
let imageConverted: UIImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
marker.icon = imageConverted
marker.map = self.mapView
Upvotes: 3