Button is Showing GMSMarkers but isn't Hiding it, How do I get it to do bothHow to round the corners of a buttonHow to hide UINavigationBar 1px bottom lineHow to create a button programmatically?How do I get a reference to the app delegate in Swift?How can I make a button have a rounded border in Swift?How to rotate GMSMarker to show user moving direction?How to hide and show UIViewController button in AppDelegate? - SwiftMultiple GMSMarker show/hide consumes timeHow do I get an image to show up on my custom button?

When using the Proficiency Dice optional rule, how should they be used in determining a character's Spell Save DC?

Is there a general term for the items in a directory?

What's "halachic" about "Esav hates Ya'akov"?

What is a Written Word™?

Has J.J.Jameson ever found out that Peter Parker is Spider-Man?

Is there a difference between `board[x, y]` and `board[x][y]` in Python?

Formal mathematical definition of renormalization group flow

Which one is more important between endgame studies and tactics?

How to win against ants

Vectorised way to calculate mean of left and right neighbours in a vector

How do I know when and if a character requires a backstory?

…down the primrose path

How does Rust's 128-bit integer `i128` work on a 64-bit system?

How long should I wait to plug in my refrigerator after unplugging it?

Is there any difference between "result in" and "end up with"?

Based on what criteria do you add/not add icons to labels within a toolbar?

Magento 2 Is it possible to use same event in multiple Modules?

What license to choose for my PhD thesis?

How do people drown while wearing a life jacket?

The warlock of firetop mountain, what's the deal with reference 192?

In MTG, was there ever a five-color deck that worked well?

Why is the Vasa Museum in Stockholm so Popular?

Is it okay to use different fingers every time while playing a song on keyboard? Is it considered a bad practice?

A Checkmate of Dubious Legality



Button is Showing GMSMarkers but isn't Hiding it, How do I get it to do both


How to round the corners of a buttonHow to hide UINavigationBar 1px bottom lineHow to create a button programmatically?How do I get a reference to the app delegate in Swift?How can I make a button have a rounded border in Swift?How to rotate GMSMarker to show user moving direction?How to hide and show UIViewController button in AppDelegate? - SwiftMultiple GMSMarker show/hide consumes timeHow do I get an image to show up on my custom button?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








0















Im creating an app that shows different locations. Currently when a button is pressed the car locations pop up on the map, however I want to then hide those shown markers if that same button is pressed again.



This is the function that takes a snapshot of my database from firebase, it then inserts the GMSMarker into the location.



 func showCarIcon() 

ref = Database.database().reference()

ref.child("location").observe(.childAdded) (snapshot:DataSnapshot) in
if let dict = snapshot.value as? [String:AnyObject]
if dict["Activity"] as! String == "Car"
let longitude = dict["Longitude"] as! String
let lattitude = dict["Lattitude"] as! String
let title = dict["Title"] as! String
self.carIconArray.insert(coordinate(carLat: lattitude, carLng: longitude), at: 0)
let n = self.carIconArray.count
let heightWidth = self.mapView.frame.height
for marker in 1...n
let carMarker = GMSMarker()
let carIconView = UIImage(named: "carPin")
let image = carIconView
let location = CLLocationCoordinate2D(latitude: Double(lattitude)!, longitude: Double(longitude)!)
carMarker.position = location
carMarker.icon = image
carMarker.title = title
carMarker.icon = self.image(image!, scaledToSize: CGSize(width: heightWidth/6, height: heightWidth/6))
func displayIt()
if self.carNumber == "1"
carMarker.map = self.mapView
else
carMarker.map = nil



displayIt()







So this is the action function for when button is pressed.



var carNumber = String()

@IBAction func showCar(_ sender: Any)
if motorisedVehicleButtonActive
motorisedVehicleButton.setImage(UIImage(named: "carO"), for: .normal)
carNumber = "1"
else
motorisedVehicleButton.setImage(UIImage(named: "car"), for: .normal)
carNumber = "0"

print(carNumber)
motorisedVehicleButtonActive = !motorisedVehicleButtonActive
showCarIcon()










share|improve this question






























    0















    Im creating an app that shows different locations. Currently when a button is pressed the car locations pop up on the map, however I want to then hide those shown markers if that same button is pressed again.



    This is the function that takes a snapshot of my database from firebase, it then inserts the GMSMarker into the location.



     func showCarIcon() 

    ref = Database.database().reference()

    ref.child("location").observe(.childAdded) (snapshot:DataSnapshot) in
    if let dict = snapshot.value as? [String:AnyObject]
    if dict["Activity"] as! String == "Car"
    let longitude = dict["Longitude"] as! String
    let lattitude = dict["Lattitude"] as! String
    let title = dict["Title"] as! String
    self.carIconArray.insert(coordinate(carLat: lattitude, carLng: longitude), at: 0)
    let n = self.carIconArray.count
    let heightWidth = self.mapView.frame.height
    for marker in 1...n
    let carMarker = GMSMarker()
    let carIconView = UIImage(named: "carPin")
    let image = carIconView
    let location = CLLocationCoordinate2D(latitude: Double(lattitude)!, longitude: Double(longitude)!)
    carMarker.position = location
    carMarker.icon = image
    carMarker.title = title
    carMarker.icon = self.image(image!, scaledToSize: CGSize(width: heightWidth/6, height: heightWidth/6))
    func displayIt()
    if self.carNumber == "1"
    carMarker.map = self.mapView
    else
    carMarker.map = nil



    displayIt()







    So this is the action function for when button is pressed.



    var carNumber = String()

    @IBAction func showCar(_ sender: Any)
    if motorisedVehicleButtonActive
    motorisedVehicleButton.setImage(UIImage(named: "carO"), for: .normal)
    carNumber = "1"
    else
    motorisedVehicleButton.setImage(UIImage(named: "car"), for: .normal)
    carNumber = "0"

    print(carNumber)
    motorisedVehicleButtonActive = !motorisedVehicleButtonActive
    showCarIcon()










    share|improve this question


























      0












      0








      0








      Im creating an app that shows different locations. Currently when a button is pressed the car locations pop up on the map, however I want to then hide those shown markers if that same button is pressed again.



      This is the function that takes a snapshot of my database from firebase, it then inserts the GMSMarker into the location.



       func showCarIcon() 

      ref = Database.database().reference()

      ref.child("location").observe(.childAdded) (snapshot:DataSnapshot) in
      if let dict = snapshot.value as? [String:AnyObject]
      if dict["Activity"] as! String == "Car"
      let longitude = dict["Longitude"] as! String
      let lattitude = dict["Lattitude"] as! String
      let title = dict["Title"] as! String
      self.carIconArray.insert(coordinate(carLat: lattitude, carLng: longitude), at: 0)
      let n = self.carIconArray.count
      let heightWidth = self.mapView.frame.height
      for marker in 1...n
      let carMarker = GMSMarker()
      let carIconView = UIImage(named: "carPin")
      let image = carIconView
      let location = CLLocationCoordinate2D(latitude: Double(lattitude)!, longitude: Double(longitude)!)
      carMarker.position = location
      carMarker.icon = image
      carMarker.title = title
      carMarker.icon = self.image(image!, scaledToSize: CGSize(width: heightWidth/6, height: heightWidth/6))
      func displayIt()
      if self.carNumber == "1"
      carMarker.map = self.mapView
      else
      carMarker.map = nil



      displayIt()







      So this is the action function for when button is pressed.



      var carNumber = String()

      @IBAction func showCar(_ sender: Any)
      if motorisedVehicleButtonActive
      motorisedVehicleButton.setImage(UIImage(named: "carO"), for: .normal)
      carNumber = "1"
      else
      motorisedVehicleButton.setImage(UIImage(named: "car"), for: .normal)
      carNumber = "0"

      print(carNumber)
      motorisedVehicleButtonActive = !motorisedVehicleButtonActive
      showCarIcon()










      share|improve this question














      Im creating an app that shows different locations. Currently when a button is pressed the car locations pop up on the map, however I want to then hide those shown markers if that same button is pressed again.



      This is the function that takes a snapshot of my database from firebase, it then inserts the GMSMarker into the location.



       func showCarIcon() 

      ref = Database.database().reference()

      ref.child("location").observe(.childAdded) (snapshot:DataSnapshot) in
      if let dict = snapshot.value as? [String:AnyObject]
      if dict["Activity"] as! String == "Car"
      let longitude = dict["Longitude"] as! String
      let lattitude = dict["Lattitude"] as! String
      let title = dict["Title"] as! String
      self.carIconArray.insert(coordinate(carLat: lattitude, carLng: longitude), at: 0)
      let n = self.carIconArray.count
      let heightWidth = self.mapView.frame.height
      for marker in 1...n
      let carMarker = GMSMarker()
      let carIconView = UIImage(named: "carPin")
      let image = carIconView
      let location = CLLocationCoordinate2D(latitude: Double(lattitude)!, longitude: Double(longitude)!)
      carMarker.position = location
      carMarker.icon = image
      carMarker.title = title
      carMarker.icon = self.image(image!, scaledToSize: CGSize(width: heightWidth/6, height: heightWidth/6))
      func displayIt()
      if self.carNumber == "1"
      carMarker.map = self.mapView
      else
      carMarker.map = nil



      displayIt()







      So this is the action function for when button is pressed.



      var carNumber = String()

      @IBAction func showCar(_ sender: Any)
      if motorisedVehicleButtonActive
      motorisedVehicleButton.setImage(UIImage(named: "carO"), for: .normal)
      carNumber = "1"
      else
      motorisedVehicleButton.setImage(UIImage(named: "car"), for: .normal)
      carNumber = "0"

      print(carNumber)
      motorisedVehicleButtonActive = !motorisedVehicleButtonActive
      showCarIcon()







      swift xcode uibutton gmsmapview






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Mar 27 at 2:34









      Tristan MellettTristan Mellett

      135 bronze badges




      135 bronze badges

























          1 Answer
          1






          active

          oldest

          votes


















          1














          Let me explain what is issue with your code.



          1. You are creating new marker every time when button press. So, new marker have different object id than older.


          2. When you try to remove it, it will not works just because of it's different marker than you placed on map.


          3. So you need to store marker in array and on remove time, get icon from array and remove it from map.


          First you need to create array of GMSMarker, because you have to store every marker which is placed on map.



          So, write following line of code at top of your class.



          var arrCarMarkers = [GMSMarker]()


          Then after, store every marker in this array which are you placing on map.



          So, update your code as follow:



          func displayIt() 
          if self.carNumber == "1"
          carMarker.map = self.mapView
          arrCarMarkers.append(carMarker) // Here is store marker in array
          else
          carMarker.map = nil




          Now, you have all marker which are placed on map. So when you want to remove these markers just update your code as follow:



          @IBAction func showCar(_ sender: Any) 
          if motorisedVehicleButtonActive
          motorisedVehicleButton.setImage(UIImage(named: "carO"), for: .normal)
          carNumber = "1"
          showCarIcon()
          else
          motorisedVehicleButton.setImage(UIImage(named: "car"), for: .normal)
          carNumber = "0"
          self.arrCarMarkers.forEach $0.map = nil

          print(carNumber)
          motorisedVehicleButtonActive = !motorisedVehicleButtonActive



          Above code will remove all markers from map.



          I hope this will works for you.






          share|improve this answer
























            Your Answer






            StackExchange.ifUsing("editor", function ()
            StackExchange.using("externalEditor", function ()
            StackExchange.using("snippets", function ()
            StackExchange.snippets.init();
            );
            );
            , "code-snippets");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "1"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader:
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            ,
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













            draft saved

            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55368935%2fbutton-is-showing-gmsmarkers-but-isnt-hiding-it-how-do-i-get-it-to-do-both%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            1














            Let me explain what is issue with your code.



            1. You are creating new marker every time when button press. So, new marker have different object id than older.


            2. When you try to remove it, it will not works just because of it's different marker than you placed on map.


            3. So you need to store marker in array and on remove time, get icon from array and remove it from map.


            First you need to create array of GMSMarker, because you have to store every marker which is placed on map.



            So, write following line of code at top of your class.



            var arrCarMarkers = [GMSMarker]()


            Then after, store every marker in this array which are you placing on map.



            So, update your code as follow:



            func displayIt() 
            if self.carNumber == "1"
            carMarker.map = self.mapView
            arrCarMarkers.append(carMarker) // Here is store marker in array
            else
            carMarker.map = nil




            Now, you have all marker which are placed on map. So when you want to remove these markers just update your code as follow:



            @IBAction func showCar(_ sender: Any) 
            if motorisedVehicleButtonActive
            motorisedVehicleButton.setImage(UIImage(named: "carO"), for: .normal)
            carNumber = "1"
            showCarIcon()
            else
            motorisedVehicleButton.setImage(UIImage(named: "car"), for: .normal)
            carNumber = "0"
            self.arrCarMarkers.forEach $0.map = nil

            print(carNumber)
            motorisedVehicleButtonActive = !motorisedVehicleButtonActive



            Above code will remove all markers from map.



            I hope this will works for you.






            share|improve this answer





























              1














              Let me explain what is issue with your code.



              1. You are creating new marker every time when button press. So, new marker have different object id than older.


              2. When you try to remove it, it will not works just because of it's different marker than you placed on map.


              3. So you need to store marker in array and on remove time, get icon from array and remove it from map.


              First you need to create array of GMSMarker, because you have to store every marker which is placed on map.



              So, write following line of code at top of your class.



              var arrCarMarkers = [GMSMarker]()


              Then after, store every marker in this array which are you placing on map.



              So, update your code as follow:



              func displayIt() 
              if self.carNumber == "1"
              carMarker.map = self.mapView
              arrCarMarkers.append(carMarker) // Here is store marker in array
              else
              carMarker.map = nil




              Now, you have all marker which are placed on map. So when you want to remove these markers just update your code as follow:



              @IBAction func showCar(_ sender: Any) 
              if motorisedVehicleButtonActive
              motorisedVehicleButton.setImage(UIImage(named: "carO"), for: .normal)
              carNumber = "1"
              showCarIcon()
              else
              motorisedVehicleButton.setImage(UIImage(named: "car"), for: .normal)
              carNumber = "0"
              self.arrCarMarkers.forEach $0.map = nil

              print(carNumber)
              motorisedVehicleButtonActive = !motorisedVehicleButtonActive



              Above code will remove all markers from map.



              I hope this will works for you.






              share|improve this answer



























                1












                1








                1







                Let me explain what is issue with your code.



                1. You are creating new marker every time when button press. So, new marker have different object id than older.


                2. When you try to remove it, it will not works just because of it's different marker than you placed on map.


                3. So you need to store marker in array and on remove time, get icon from array and remove it from map.


                First you need to create array of GMSMarker, because you have to store every marker which is placed on map.



                So, write following line of code at top of your class.



                var arrCarMarkers = [GMSMarker]()


                Then after, store every marker in this array which are you placing on map.



                So, update your code as follow:



                func displayIt() 
                if self.carNumber == "1"
                carMarker.map = self.mapView
                arrCarMarkers.append(carMarker) // Here is store marker in array
                else
                carMarker.map = nil




                Now, you have all marker which are placed on map. So when you want to remove these markers just update your code as follow:



                @IBAction func showCar(_ sender: Any) 
                if motorisedVehicleButtonActive
                motorisedVehicleButton.setImage(UIImage(named: "carO"), for: .normal)
                carNumber = "1"
                showCarIcon()
                else
                motorisedVehicleButton.setImage(UIImage(named: "car"), for: .normal)
                carNumber = "0"
                self.arrCarMarkers.forEach $0.map = nil

                print(carNumber)
                motorisedVehicleButtonActive = !motorisedVehicleButtonActive



                Above code will remove all markers from map.



                I hope this will works for you.






                share|improve this answer













                Let me explain what is issue with your code.



                1. You are creating new marker every time when button press. So, new marker have different object id than older.


                2. When you try to remove it, it will not works just because of it's different marker than you placed on map.


                3. So you need to store marker in array and on remove time, get icon from array and remove it from map.


                First you need to create array of GMSMarker, because you have to store every marker which is placed on map.



                So, write following line of code at top of your class.



                var arrCarMarkers = [GMSMarker]()


                Then after, store every marker in this array which are you placing on map.



                So, update your code as follow:



                func displayIt() 
                if self.carNumber == "1"
                carMarker.map = self.mapView
                arrCarMarkers.append(carMarker) // Here is store marker in array
                else
                carMarker.map = nil




                Now, you have all marker which are placed on map. So when you want to remove these markers just update your code as follow:



                @IBAction func showCar(_ sender: Any) 
                if motorisedVehicleButtonActive
                motorisedVehicleButton.setImage(UIImage(named: "carO"), for: .normal)
                carNumber = "1"
                showCarIcon()
                else
                motorisedVehicleButton.setImage(UIImage(named: "car"), for: .normal)
                carNumber = "0"
                self.arrCarMarkers.forEach $0.map = nil

                print(carNumber)
                motorisedVehicleButtonActive = !motorisedVehicleButtonActive



                Above code will remove all markers from map.



                I hope this will works for you.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Mar 27 at 5:18









                Sagar ChauhanSagar Chauhan

                2,8502 gold badges10 silver badges39 bronze badges




                2,8502 gold badges10 silver badges39 bronze badges





















                    Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.







                    Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.



















                    draft saved

                    draft discarded
















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid


                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.

                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55368935%2fbutton-is-showing-gmsmarkers-but-isnt-hiding-it-how-do-i-get-it-to-do-both%23new-answer', 'question_page');

                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

                    용인 삼성생명 블루밍스 목차 통계 역대 감독 선수단 응원단 경기장 같이 보기 외부 링크 둘러보기 메뉴samsungblueminx.comeh선수 명단용인 삼성생명 블루밍스용인 삼성생명 블루밍스ehsamsungblueminx.comeheheheh

                    155 수학 과학 기타 둘러보기 메뉴eh추가해eh문서를 완성해