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 :...

Common API Request Class

// //    Connection.swift //    Norwood // //    Created by Praveen Reddy on 10/01/19. //    Copyright © 2019 Apple. All rights reserved. // import  Foundation import  Alamofire import  SwiftyJSON class  Connection {           func  requestPOST( _  url:  String , params :  Parameters ?, headers :  HTTPHeaders ?, success: @escaping  ( Data ) ->  Void , failure: @escaping  ( Error ) ->  Void )      {                   print( "URL = " ,url)          print( "Parameter = " ,params!)                   //          if Connectivity.isConnectedToInternet() //          {              i...