Search code examples
google-mapsgoogle-maps-api-3kml

How to zoom in to the location of the marker in kml


I am using kml file to display the location on google map. i want to zoom in to that marker location while click the marker. Here is my kml layer code ,

 function initMap() {
    map = new google.maps.Map(document.getElementById('map'), {
      center: new google.maps.LatLng(11.024747, 76.898037),
      zoom: 2,
      mapTypeId: 'terrain'
    });

    var kmlLayer = new google.maps.KmlLayer(src, {
      suppressInfoWindows: true,
      preserveViewport: false,
      map: map
    });



    kmlLayer.addListener('click', function(event) {
      var content = event.featureData.infoWindowHtml;
      var testimonial = document.getElementById('capture');
      testimonial.innerHTML = content;
     });
  }

Please Help me


Solution

  • you can zoom to clicked latLng using event.latLng and manually setting a zoom level.

      kmlLayer.addListener('click', function(event) {
    
         map.setCenter(event.latLng);
         map.setZoom(20)
    
         var content = event.featureData.infoWindowHtml;
         var testimonial = document.getElementById('capture');
         testimonial.innerHTML = content;
      });
    

    Please find this fiddle -> https://jsfiddle.net/Rohith_KP/mcwzh4an/1/