Skip to content Skip to sidebar Skip to footer

Create Event Listener To Dynamically Created Google-map Marker

I am trying to create a page that I am going to integrate with excel with vba. . I want to add event Listener to marker when it is clicked. I can barely manage to create a 'click l

Solution 1:

  1. keep a reference to the panorama:
// Set the initial Street View camera to the center of the mapnew google.maps.StreetViewPanorama(document.getElementById('pano'), panoramaOptions);

should be:

// Set the initial Street View camera to the center of the map
panorama = new google.maps.StreetViewPanorama(document.getElementById('pano'), panoramaOptions);
  1. Change the location of the panorama in the marker click listener.
google.maps.event.addListener(marker,'click', function(e) {
   pano.setPosition(marker.getPosition());
});

proof of concept fiddle

code snippet:

var labels = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'; //mevar labelIndex = 0; //mevar map;
var panorama;

//this will be created from server sidevar markers = [{
  lat: 39.976784,
  lng: -75.234347,
  name: "marker 1"
}, {
  lat: 39.977043,
  lng: -75.235087,
  name: "marker 2"
}, {
  lat: 39.976097,
  lng: -75.234508,
  name: "marker 3"
}, {
  lat: 39.977059,
  lng: -75.233682,
  name: "marker 4"
}];
var myLatlng = new google.maps.LatLng(markers[0].lat, markers[0].lng);


functioninitialize() {
  //var sv = new google.maps.StreetViewService();
  panorama = new google.maps.StreetViewPanorama(document.getElementById('pano'));


  var mapOptions = {
    zoom: 16,
    center: myLatlng
  }
  map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
  var panoramaOptions = {

    navigationControl: true,
    position: myLatlng,
    //pov: {//heading: 34,//pitch: 10//}
  };
  // Set the initial Street View camera to the center of the map
  pano = new google.maps.StreetViewPanorama(document.getElementById('pano'),
    panoramaOptions);

  //this is the loop that will creat the markerfor (var index in markers) addMarker(markers[index]);

  functionaddMarker(data) {
    // Create the markervar marker = new google.maps.Marker({
      position: new google.maps.LatLng(data.lat, data.lng),
      map: map,
      label: labels[labelIndex++ % labels.length],
      title: data.name
    });
    google.maps.event.addListener(marker, 'click', function(e) {
      pano.setPosition(marker.getPosition());
    });
  }

}

google.maps.event.addDomListener(window, 'load', initialize);
html,
body,
#map-canvas {
  height: 100%;
  margin: 0;
  padding: 0;
}
<scriptsrc="https://maps.googleapis.com/maps/api/js"></script><divid="map-excel"style="width: 60%; height: 100%;float:left"></div><divid="pano"style="width: 40%; height: 50%;float:right"></div><divid="map-canvas"style="width: 40%; height: 50%;float:right"></div>

Solution 2:

In your function here (function(data){....})(data); Because you use array of marker.

functionaddMarker(data) {
  (function(data){   <<-- New line added 
  // Create the markervar marker = new google.maps.Marker({
    position: new google.maps.LatLng(data.lat, data.lng),
    map: map,
    label: labels[labelIndex++ % labels.length],
    title: data.name
  });

  google.maps.event.addListener(marker, 'click', function(){// <<---- New line added// Code event listener .... 
  });// <<---- New line added

  })(data); // <<---- New line added
}

view more example here: https://developers.google.com/maps/documentation/javascript/examples/infowindow-simple

Solution 3:

// Include this code to your loop

var infowindow = new google.maps.InfoWindow({
    content: 'info content'
});
google.maps.event.addListener(marker, 'click', function() {
    infowindow.open(map,marker);
});

Post a Comment for "Create Event Listener To Dynamically Created Google-map Marker"