Skip to main content

Google Maps Custom Annotation



        
        let latitude = (latArr.object(at: 0) as! NSString).doubleValue
        let longitude = (longArr.object(at: 0) as! NSString).doubleValue
        
        let camera = GMSCameraPosition.camera(withLatitude: latitude, longitude: longitude, zoom: 12)
        self.mapView.camera = camera
        
        mapView.isMyLocationEnabled = true
        mapView.settings.myLocationButton = true
        for (index, _) in latArr.enumerated() {
            
            let latitude = (latArr.object(at: index) as! NSString).doubleValue
            let longitude = (longArr.object(at: index) as! NSString).doubleValue
            state_marker.position = CLLocationCoordinate2D(latitude: latitude , longitude:longitude)
            let  userType =  UserDefaults.standard.value(forKey:"usertype") as! NSString
                state_marker.title = (AddreesArr.object(at: index) as! String)
                state_marker.snippet = (AddreesArr.object(at: index) as! String)
                state_marker.icon = createImage((AddreesArr.object(at: index) as! String) , "marker_orange")
            }
            state_marker.map = mapView
       

    
    func createImage(_ name: String , _ imageName : String) -> UIImage {
        //count is the integer that has to be shown on the marker
        let color = UIColor.white
        // select needed color
        let string = "\(name)"
        // the string to colorize
        let attrs = [NSAttributedString.Key.foregroundColor: color]
        let attrStr = NSAttributedString(string: string, attributes: attrs)
        // add Font according to your need
        let  userType =  UserDefaults.standard.value(forKey:"usertype") as! NSString
        
        if userType == "1"
        {
            let image = UIImage(named:imageName)!
            UIGraphicsBeginImageContext(image.size)
            image.draw(in: CGRect(x: CGFloat(0), y: CGFloat(0), width: CGFloat(image.size.width), height: CGFloat(image.size.height)))
            let rect = CGRect(x: CGFloat(5), y: CGFloat(5), width: CGFloat(image.size.width), height: CGFloat(image.size.height))
            
            attrStr.draw(in: rect)
      
        }
        else
        {
            
            let image = UIImage(named: imageName)!
            UIGraphicsBeginImageContext(image.size)
            image.draw(in: CGRect(x: CGFloat(0), y: CGFloat(0), width: CGFloat(image.size.width), height: CGFloat(image.size.height)))
            let rect = CGRect(x: CGFloat(5), y: CGFloat(5), width: CGFloat(image.size.width), height: CGFloat(image.size.height))
            
            attrStr.draw(in: rect)

        }
        // The image on which text has to be added
        let markerImage = UIGraphicsGetImageFromCurrentImageContext()!
        UIGraphicsEndImageContext()
        return markerImage
    }

Comments

Popular posts from this blog

API Calling Example

import  Foundation import  Alamofire class  HomeViewModel {           let  sharedInstance = Connection()      var  reloadHandler:  DataHandler  = { }      typealias  DataHandler = () ->  Void      init () { }           //--------------------------------------------------      // MarK : fetchInitialSetup    API Integration      //--------------------------------------------------      func  fetchInitialSetup(success: @escaping  ( GetLocationAndDevicesModel ) ->  Void , failure: @escaping  ( Error ) ->  Void )      {          let  URL = AddDeviceList().getUrlString(url: .CREATE_API)                   let  headers :...

TAGS in swift

github link : https://github.com/ElaWorkshop/TagListView Simple and highly customizable iOS tag list view, in Swift. Supports Storyboard, Auto Layout, and @IBDesignable. Usage The most convenient way is to use Storyboard. Drag a view to Storyboard and set Class to  TagListView  (if you use CocoaPods, also set Module to  TagListView ). Then you can play with the attributes in the right pane, and see the preview in real time thanks to  @IBDesignable . You can add tag to the tag list view, or set custom font and alignment through code: tagListView. textFont = UIFont. systemFontOfSize ( 24 ) tagListView. alignment = . Center // possible values are .Left, .Center, and .Right tagListView. addTag ( " TagListView " ) tagListView. addTags ([ " Add " , " two " , " tags " ]) tagListView. insertTag ( " This should be the second tag " , at : 1 ) tagListView. setTitle ( " New Title " , at : 6 ) // to repla...