Select to view content in your preferred language

Zoom to a feature on a mouse click event

7364
7
Jump to solution
05-20-2016 01:31 PM
RoxanneWilson
Emerging Contributor

So, working on my first app and I have a point layer with a few locations in it.  I'm trying to create an event where, when the user clicks on a location, the map extent zooms to that location.  This is what I have so far and it doesn't work (not a shocker)....

layerName.on("click", function (zoom) {

 

  map.zoom (zoom.layer.setZoom(4));

});

0 Kudos
1 Solution

Accepted Solutions
PanagiotisPapadopoulos
Esri Regular Contributor

on this function

layerName.on("click", function (event) {

take the selected geometry event.graphic

and zoom on the map

map.centerAndZoom(event.graphic.geometry, 4);

 

});

View solution in original post

7 Replies
RickeyFight
MVP Regular Contributor

Are you using a sample?

Can you post more of your code?

0 Kudos
TomSellsted
MVP Regular Contributor

Roxanne,

Zoom is not a method of map.  When a graphic in a feature layer is clicked, it will return the graphic,  You can then use the graphic geometry or the event mapPoint to zoom to that location.  Here is a further explanation:

FeatureLayer | API Reference | ArcGIS API for JavaScript

It should look something like this:

layerName.on("click", function (event) {

    map.centerAndZoom(event.mapPoint, 4);

});

Regards,

Tom

0 Kudos
RoxanneWilson
Emerging Contributor

I've added that into my code, but nothing is happening yet.

Do I need to add something else?  I've been reading about event listeners, but I don't understand a lot of that quite yet.

0 Kudos
FC_Basson
MVP Regular Contributor

The click tolerance might cause that you are just missing the features with the click event and therefore nothing is happening.  Try setting the click event on the map object.

0 Kudos
PanagiotisPapadopoulos
Esri Regular Contributor

on this function

layerName.on("click", function (event) {

take the selected geometry event.graphic

and zoom on the map

map.centerAndZoom(event.graphic.geometry, 4);

 

});

RoxanneWilson
Emerging Contributor

This doesn't appear to do anything either.

layer.on("click", function (event) {
 
  map.centerAndZoom(event.graphic.geometry, 4);

});

0 Kudos
RoxanneWilson
Emerging Contributor

Using developer tools, I found there was an error with another part of my code.

0 Kudos