Hiding markers in googlemaps v3 in an object

91 views Asked by At

i would like to hide markers in my object called lastCoordinates which isn't an array so when i click a button my markers hide and i can re-appear my markers with a different button. also in my code i have the function that when to right click a marker it gets deleted and the line snaps to the marker placed before the marker deleted this is done by using data from a separate random php file which creates random coordinates. here is my html code:

<!DOCTYPE html>
<html>
  <head>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
    <meta charset="utf-8">
    <title>Simple markers</title>
    <style>
      html, body, #map-canvas {
        height: 100%;
        margin: 0px;
        padding: 0px
      }
      #panel {
        position: absolute;
        top: 5px;
        left: 50%;
        margin-left: -180px;
        z-index: 5;
        background-color: #fff;

      }
    </style>
    <script src="https://maps.googleapis.com/maps/api/js?v=3.exp&signed_in=true"></script>
    <script>
var map;
var stop = 0;
var markers = [];
window.onload=function ()
{
    var myLatlng = new google.maps.LatLng(0,0);
    var mapOptions = {
        zoom: 2,
        center: myLatlng
    }
    map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
    //google.maps.event.addDomListener(window, 'load', mapready);
    getdata();
    setInterval(function () {
        if(stop<3000)
            getdata();
        stop++;
    }, 2000);
}

function getdata() 
{
    if (window.XMLHttpRequest)
      {// code for IE7+, Firefox, Chrome, Opera, Safari
      xmlhttp=new XMLHttpRequest();
      }
    else
      {// code for IE6, IE5
      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
      //xmlhttp.open("GET","GetLocation.xml",true);
      xmlhttp.open("GET","data.php",true);
   xmlhttp.onreadystatechange=function () {gotdata()};
   xmlhttp.send();
}
var lastCoordinates = [];
var polyline = new google.maps.Polyline({
   strokeWeight: 6,
   strokeColor:"#0000FF",  // blue (RRGGBB, R=red, G=green, B=blue)
   strokeOpacity: 1      // opacity of line
}); // create the polyline (global)
var path = []; // global variable to hold all the past locations
function gotdata(){

    if (xmlhttp.readyState == 4){

        var d = xmlhttp.responseXML.documentElement 
            //innerHTML shouldn't work for XML-Nodes
            y = d.getElementsByTagName("y")[0].textContent,
            x = d.getElementsByTagName("x")[0].textContent,
            h = [x,y].join('_');
        if(lastCoordinates[h]){
          return;
        } 

        lastCoordinates[h]= new google.maps.Marker({
            position: new google.maps.LatLng(x,y),
            map: map,
            title: 'YAY'
        });
        rightclickableMarker(lastCoordinates[h],h);   
         path.push(lastCoordinates[h].getPosition());
         drawPath();
    }
}
function rightclickableMarker(marker, h) {
    google.maps.event.addListener(marker, 'rightclick', function(evt) {
        if(lastCoordinates[h] && lastCoordinates[h].setMap){
          lastCoordinates[h].setMap(null);
          delete marker;
          var idx = path.indexOf(lastCoordinates[h].getPosition())
          if (idx > -1) {
             path.splice(idx, 1);
            // removeLine();
             drawPath();
          }

        } 
    });
}


function drawPath(){
 polyline.setMap(map);
 polyline.setPath(path);

}
function setAllMap(map) {
  for (var i = 0; i < lastCoordinates.length; i++) {
    lastCoordinates[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);
}

    </script>
  </head>
  <body>
    <div id= "panel">
      <input onclick="clearMarkers();" type=button value="Hide Markers">
      <input onclick="showMarkers();" type=button value="Show All Markers">
    </div>
    <div id="map-canvas">
    </div>
  </body>
</html>

and my random php selector:

<?php 
header("Content-type: application/xml");
?>
<Location>
<x><?php echo rand(-85,85); ?></x>
<y><?php echo rand(-85,85); ?></y>
</Location>
2

There are 2 answers

0
so_jin_ee On

You cannot store your markers in lastCoordinates[h] and then expect to call them from var i=0 to i=lastCoordinates.length.....

function setAllMap(map) {
  for (var i = 0; i < lastCoordinates.length; i++) {
    lastCoordinates[i].setMap(map);
  }
}

My suggestion, store all h values in another array

var h_s=[];

then do

for(var i = 0; i < h_s.length; i++){
    lastCoordinates[h_s[i]].setMap(map);
}
0
GEOMAS On

Don't worry i figured out how to do it, what I needed to do was use the for in item to solve that problem

function rightclickableMarker(marker, h) {
    google.maps.event.addListener(marker, 'rightclick', function(evt) {
        if(lastCoordinates[h] && lastCoordinates[h].setMap){
         lastCoordinates[h].setMap(null);
          delete (lastCoordinates);
          var idx = path.indexOf(lastCoordinates[h].getPosition())
          if (idx > -1) {
             path.splice(idx, 1);
            // removeLine();
             drawPath();
          }

        } 
    });
}


function drawPath(){
 polyline.setMap(map);
 polyline.setPath(path);

}
function setAllMap(map) {

  for (var prop in lastCoordinates) {
    lastCoordinates[prop].setMap(map);  }
}