Solved

Show/hide markers on google maps

Posted on 2015-02-18
5
150 Views
Last Modified: 2015-02-19
Hi
I have got the following code and I would like to show/hide each of the markers, separately, without having to reload the page.  

var venues = [["Rifle Place, Avondale", 51.509224, -0.216016, 15],
                ["Binfield Road, Stockwell", 51.47250956, -0.122831913, 0],
                ["Tallis Street, Temple", 51.511891, -0.107349, 10],
                ["Hurlingham Park, Parsons Green", 51.470131, -0.20464, 8],
                ["Little Brook Green, Brook Green", 51.496664, -0.223868, 6],
                ["Abyssinia Close, Clapham Junction", 51.460333, -0.167029, 22],
                ["Limburg Road, Clapham Common", 51.46192307, -0.165297857, 09]];

var map = new google.maps.Map(document.getElementById('map'), {
                    zoom: 14,
                    mapTypeId: google.maps.MapTypeId.ROADMAP
                });

                var center;
                navigator.geolocation && navigator.geolocation.getCurrentPosition(showPosition, showError) == undefined && showError();

                var infowindow = new google.maps.InfoWindow();
                var directionsService = new google.maps.DirectionsService();
                var directionsDisplay = new google.maps.DirectionsRenderer();
                directionsDisplay.setMap(map);

                var marker, i;

				var image_loc = "/images/mapicons/", image_low = image_loc + "markerlow.png", image_med = image_loc + "markermed.png", image_high = image_loc + "markerhigh.png", bike = image_loc + "bike.png";


                for (i = 0; i < venues.length; i++) {

                        marker = new google.maps.Marker({
                        position: new google.maps.LatLng(venues[i][1], venues[i][2]),
                        map: map,
                        icon: Math.round(venues[i][3]) == 0 ? bike : ((Math.round(venues[i][3]) < 5) ? image_low : ((Math.round(venues[i][3]) > 10) ? image_high : image_med))

                                                         });

                        google.maps.event.addListener(marker, 'mouseover', (function (marker, i) {
                        return function () {
                        infowindow.setContent(venues[i][0] + '<br/><a href="javascript:calcRoute(' + i + ')">How do I get there?</a><br>View on <a href="http://maps.google.com/maps?q=' + venues[i][1] + ',' + venues[i][2] + '">Google Maps</a>');
                        infowindow.open(map, marker);
                                            }
                                                                                                })
                        (marker, i));


                }

Open in new window


Please can someone help me out?

thanks
0
Comment
Question by:lz7cjc
  • 3
  • 2
5 Comments
 
LVL 38

Expert Comment

by:Tom Beck
ID: 40617066
What would trigger the markers to show and hide?
0
 

Author Comment

by:lz7cjc
ID: 40617154
i would be looking to put a button or link - at this stage don't mind.

In my failed attempt I used this:
// Sets the map on all markers in the array.
function setAllMap(map) {
  for (var i = 0; i < venues.length; i++) {
    venues[i].setMap(map);
  }
}

// Removes the markers from the map, but keeps them in the array.
function clearMarkers() {
  setAllMap(null);
}

// Shows any markers currently in the array.
function showMarkers() {
  setAllMap(map);
}

// Deletes all markers in the array by removing references to them.
function deleteMarkers() {
  clearMarkers();
  venues = [];
}

</script>
<div id="panel">
      <input onclick="clearMarkers();" type=button value="Hide Markers">
      <input onclick="showMarkers();" type=button value="Show All Markers">
      <input onclick="deleteMarkers();" type=button value="Delete Markers">
    </div>
  </section>
-->

Open in new window

0
 
LVL 38

Accepted Solution

by:
Tom Beck earned 500 total points
ID: 40617322
Here's a simplified version of your map with buttons dynamically added (based on the array of locations) to show and hide individual markers or all at once.

http://jsfiddle.net/tommyBoy/oyax5x2j/
0
 

Author Comment

by:lz7cjc
ID: 40617531
that's awesome ... thank you... I will try to integrate it in the morning and post a new question if I get stuck

thanks
0
 

Author Comment

by:lz7cjc
ID: 40619083
am stuck! I thought I would be able to hack this into what I wanted but no joy. Any chance you can help me out with this question:
http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28620282.html
thanks
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
In this tutorial viewers will learn how to position items using CSS's three positioning types Create a new HTML document with an internal stylesheet.: Create another div in CSS and name it Absolute : Type "position:absolute;" and "top:10px; left:50p…
In this tutorial viewers will learn how to embed an audio file in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: : The declaration should display (CODE) HTML5 is supported by the most recent versions of all major browsers…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now