Select to view content in your preferred language

Adding click and zoom functionality

6445
2
Jump to solution
09-26-2015 07:23 AM
joepublic
Deactivated User

I have the following app (ICGC WEB PORTAL​)  Based on which button at the top clicked on, certain countries are highlighted, I  need help with the following:

 

I want to pan to(?)/center on the country clicked on as well as increase the zoom level.

This is my attempt

 

function getExtent(){

  var lat = map.getCenter().lat;

  var lng = map.getCenter().lng;

  var newCord = [lat, lng];

  return newCord;

}

 

function highlightFeature(e){

   var newCenter = getExtent();  <--

   var layer = e.target

  [stuff omitted]

}

 

function onEachFeature(feature, layer){

  layer.on({

    click: highlightFeature

  });

}

 

countries = L.geoJson(featureCollection, {

   onEachFeature: onEachFeature

}).addTo(map);

 

 

Script attached

Tags (1)
0 Kudos
1 Solution

Accepted Solutions
JohnGravois
Deactivated User

the easiest thing to do would just be to listen for the 'click' event of your featureLayer and use that opportunity to retrieve its bounds and pass them to the map.

featureLayer.on('click', function(evt) {
    // http://leafletjs.com/reference.html#featuregroup-getbounds
    var bounds = evt.layer.getBounds()
    // http://leafletjs.com/reference.html#map-fitbounds
    map.fitBounds(bounds);
});

this is possible because 'L.esri.FeatureLayer' extends 'L.geoJson' and 'L.geoJson' extends 'L.featureGroup', which has a method 'getBounds()'.  you can find a working example here.

between all the inheritance and the fact that the documentation for Esri Leaflet and Leaflet itself are in two different places, stuff like this can be confusing until you get the hang of it.

View solution in original post

2 Replies
JohnGravois
Deactivated User

the easiest thing to do would just be to listen for the 'click' event of your featureLayer and use that opportunity to retrieve its bounds and pass them to the map.

featureLayer.on('click', function(evt) {
    // http://leafletjs.com/reference.html#featuregroup-getbounds
    var bounds = evt.layer.getBounds()
    // http://leafletjs.com/reference.html#map-fitbounds
    map.fitBounds(bounds);
});

this is possible because 'L.esri.FeatureLayer' extends 'L.geoJson' and 'L.geoJson' extends 'L.featureGroup', which has a method 'getBounds()'.  you can find a working example here.

between all the inheritance and the fact that the documentation for Esri Leaflet and Leaflet itself are in two different places, stuff like this can be confusing until you get the hang of it.

joepublic
Deactivated User

John,

Thanks for the assist

Chris

0 Kudos