Android - Removing markers when changing different maps












0















I have created an app that displays different tracks in a park. I have created two markers which represent the start and end of the track. I am getting a problem where when I change tracks, the marker from the previous track still shows. I tried map.clear() but that removed everything. I want to not show the markers from the previous track.



private void createMarker(double latitude, double longitude, String title) {
map.addMarker(new MarkerOptions()
.position(new LatLng(latitude, longitude))
.title(title)
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_YELLOW)));
}

private void updateMapTrack() {
switch (options.getTrack()) {
case TRACK1:
createMarker(-45.85696303760779, 170.5199563062967, "Start of track1.");
createMarker(-45.85808344124618, 170.5247490755895, "End of track1.");
map.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(-45.856895, 170.518673), (float) 17.8));
break;

case TRACK2:
createMarker(-45.85696303760779, 170.5199563062967, "Start of track2.");
createMarker(-45.85808344124618, 170.5247490755895, "End of track2.");
map.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(-45.857144, 170.522713), (float) 16.32));
break;

case TRACK3:
createMarker(-45.85714008365828, 170.5193834664067, "Start of track3.");
createMarker(-45.85751258570694, 170.526808129631, "End of track3.");
map.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(-45.858164, 170.523111), (float) 16));
break;
}
}









share|improve this question



























    0















    I have created an app that displays different tracks in a park. I have created two markers which represent the start and end of the track. I am getting a problem where when I change tracks, the marker from the previous track still shows. I tried map.clear() but that removed everything. I want to not show the markers from the previous track.



    private void createMarker(double latitude, double longitude, String title) {
    map.addMarker(new MarkerOptions()
    .position(new LatLng(latitude, longitude))
    .title(title)
    .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_YELLOW)));
    }

    private void updateMapTrack() {
    switch (options.getTrack()) {
    case TRACK1:
    createMarker(-45.85696303760779, 170.5199563062967, "Start of track1.");
    createMarker(-45.85808344124618, 170.5247490755895, "End of track1.");
    map.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(-45.856895, 170.518673), (float) 17.8));
    break;

    case TRACK2:
    createMarker(-45.85696303760779, 170.5199563062967, "Start of track2.");
    createMarker(-45.85808344124618, 170.5247490755895, "End of track2.");
    map.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(-45.857144, 170.522713), (float) 16.32));
    break;

    case TRACK3:
    createMarker(-45.85714008365828, 170.5193834664067, "Start of track3.");
    createMarker(-45.85751258570694, 170.526808129631, "End of track3.");
    map.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(-45.858164, 170.523111), (float) 16));
    break;
    }
    }









    share|improve this question

























      0












      0








      0








      I have created an app that displays different tracks in a park. I have created two markers which represent the start and end of the track. I am getting a problem where when I change tracks, the marker from the previous track still shows. I tried map.clear() but that removed everything. I want to not show the markers from the previous track.



      private void createMarker(double latitude, double longitude, String title) {
      map.addMarker(new MarkerOptions()
      .position(new LatLng(latitude, longitude))
      .title(title)
      .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_YELLOW)));
      }

      private void updateMapTrack() {
      switch (options.getTrack()) {
      case TRACK1:
      createMarker(-45.85696303760779, 170.5199563062967, "Start of track1.");
      createMarker(-45.85808344124618, 170.5247490755895, "End of track1.");
      map.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(-45.856895, 170.518673), (float) 17.8));
      break;

      case TRACK2:
      createMarker(-45.85696303760779, 170.5199563062967, "Start of track2.");
      createMarker(-45.85808344124618, 170.5247490755895, "End of track2.");
      map.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(-45.857144, 170.522713), (float) 16.32));
      break;

      case TRACK3:
      createMarker(-45.85714008365828, 170.5193834664067, "Start of track3.");
      createMarker(-45.85751258570694, 170.526808129631, "End of track3.");
      map.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(-45.858164, 170.523111), (float) 16));
      break;
      }
      }









      share|improve this question














      I have created an app that displays different tracks in a park. I have created two markers which represent the start and end of the track. I am getting a problem where when I change tracks, the marker from the previous track still shows. I tried map.clear() but that removed everything. I want to not show the markers from the previous track.



      private void createMarker(double latitude, double longitude, String title) {
      map.addMarker(new MarkerOptions()
      .position(new LatLng(latitude, longitude))
      .title(title)
      .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_YELLOW)));
      }

      private void updateMapTrack() {
      switch (options.getTrack()) {
      case TRACK1:
      createMarker(-45.85696303760779, 170.5199563062967, "Start of track1.");
      createMarker(-45.85808344124618, 170.5247490755895, "End of track1.");
      map.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(-45.856895, 170.518673), (float) 17.8));
      break;

      case TRACK2:
      createMarker(-45.85696303760779, 170.5199563062967, "Start of track2.");
      createMarker(-45.85808344124618, 170.5247490755895, "End of track2.");
      map.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(-45.857144, 170.522713), (float) 16.32));
      break;

      case TRACK3:
      createMarker(-45.85714008365828, 170.5193834664067, "Start of track3.");
      createMarker(-45.85751258570694, 170.526808129631, "End of track3.");
      map.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(-45.858164, 170.523111), (float) 16));
      break;
      }
      }






      android google-maps






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 15 '18 at 11:07









      User123User123

      667




      667
























          1 Answer
          1






          active

          oldest

          votes


















          1














          You will have to keep the reference to the Marker Object you created and then you have to call



          [Marker Reference].remove() method to remove it from the GoogleMap.
          for Example:



          Create Marker like this and save the reference:



          Marker startTrackMarker = createMarker(-45.85696303760779, 170.5199563062967, "Start of track1.");


          And that is how you will remove:



          startTrackMarker.remove()


          And your createMarker method will be changed like this:



          private Marker createMarker(double latitude, double longitude, String title) {
          return map.addMarker(new MarkerOptions()
          .position(new LatLng(latitude, longitude))
          .title(title)
          .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_YELLOW)));
          }


          Note:



          If you have multiple markers then you will need an array to hold all the references to the markets like this:



          ArrayList<Marker> markers = new ArrayList<>();


          and then to remove these markers you will have to iterate the array and call remove() on every marker object. (Otherwise, it all depends upon your logic).






          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%2f53318101%2fandroid-removing-markers-when-changing-different-maps%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














            You will have to keep the reference to the Marker Object you created and then you have to call



            [Marker Reference].remove() method to remove it from the GoogleMap.
            for Example:



            Create Marker like this and save the reference:



            Marker startTrackMarker = createMarker(-45.85696303760779, 170.5199563062967, "Start of track1.");


            And that is how you will remove:



            startTrackMarker.remove()


            And your createMarker method will be changed like this:



            private Marker createMarker(double latitude, double longitude, String title) {
            return map.addMarker(new MarkerOptions()
            .position(new LatLng(latitude, longitude))
            .title(title)
            .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_YELLOW)));
            }


            Note:



            If you have multiple markers then you will need an array to hold all the references to the markets like this:



            ArrayList<Marker> markers = new ArrayList<>();


            and then to remove these markers you will have to iterate the array and call remove() on every marker object. (Otherwise, it all depends upon your logic).






            share|improve this answer






























              1














              You will have to keep the reference to the Marker Object you created and then you have to call



              [Marker Reference].remove() method to remove it from the GoogleMap.
              for Example:



              Create Marker like this and save the reference:



              Marker startTrackMarker = createMarker(-45.85696303760779, 170.5199563062967, "Start of track1.");


              And that is how you will remove:



              startTrackMarker.remove()


              And your createMarker method will be changed like this:



              private Marker createMarker(double latitude, double longitude, String title) {
              return map.addMarker(new MarkerOptions()
              .position(new LatLng(latitude, longitude))
              .title(title)
              .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_YELLOW)));
              }


              Note:



              If you have multiple markers then you will need an array to hold all the references to the markets like this:



              ArrayList<Marker> markers = new ArrayList<>();


              and then to remove these markers you will have to iterate the array and call remove() on every marker object. (Otherwise, it all depends upon your logic).






              share|improve this answer




























                1












                1








                1







                You will have to keep the reference to the Marker Object you created and then you have to call



                [Marker Reference].remove() method to remove it from the GoogleMap.
                for Example:



                Create Marker like this and save the reference:



                Marker startTrackMarker = createMarker(-45.85696303760779, 170.5199563062967, "Start of track1.");


                And that is how you will remove:



                startTrackMarker.remove()


                And your createMarker method will be changed like this:



                private Marker createMarker(double latitude, double longitude, String title) {
                return map.addMarker(new MarkerOptions()
                .position(new LatLng(latitude, longitude))
                .title(title)
                .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_YELLOW)));
                }


                Note:



                If you have multiple markers then you will need an array to hold all the references to the markets like this:



                ArrayList<Marker> markers = new ArrayList<>();


                and then to remove these markers you will have to iterate the array and call remove() on every marker object. (Otherwise, it all depends upon your logic).






                share|improve this answer















                You will have to keep the reference to the Marker Object you created and then you have to call



                [Marker Reference].remove() method to remove it from the GoogleMap.
                for Example:



                Create Marker like this and save the reference:



                Marker startTrackMarker = createMarker(-45.85696303760779, 170.5199563062967, "Start of track1.");


                And that is how you will remove:



                startTrackMarker.remove()


                And your createMarker method will be changed like this:



                private Marker createMarker(double latitude, double longitude, String title) {
                return map.addMarker(new MarkerOptions()
                .position(new LatLng(latitude, longitude))
                .title(title)
                .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_YELLOW)));
                }


                Note:



                If you have multiple markers then you will need an array to hold all the references to the markets like this:



                ArrayList<Marker> markers = new ArrayList<>();


                and then to remove these markers you will have to iterate the array and call remove() on every marker object. (Otherwise, it all depends upon your logic).







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Nov 15 '18 at 11:25

























                answered Nov 15 '18 at 11:20









                Muhammad warisMuhammad waris

                16717




                16717
































                    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%2f53318101%2fandroid-removing-markers-when-changing-different-maps%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

                    Xamarin.iOS Cant Deploy on Iphone

                    Glorious Revolution

                    Dulmage-Mendelsohn matrix decomposition in Python