Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Show/hide markers on google maps

Posted on 2015-02-18
5
Medium Priority
?
246 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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

The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Is your Office 365 signature not working the way you want it to? Are signature updates taking up too much of your time? Let's run through the most common problems that an IT administrator can encounter when dealing with Office 365 email signatures.
Finding original email is quite difficult due to their duplicates. From this article, you will come to know why multiple duplicates of same emails appear and how to delete duplicate emails from Outlook securely and instantly while vital emails remai…
In this tutorial viewers will learn how to embed Flash content in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <object> tag to embed Flash content.: To specify that the object is Flash content, d…
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.

688 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