Select to view content in your preferred language

How to create custom recenter button

2296
8
Jump to solution
03-04-2020 10:06 PM
rsharma
Frequent Contributor

Hi I am trying to get custom recenter button and basemap 3 custom button like below image. Is their any example

0 Kudos
1 Solution

Accepted Solutions
FC_Basson
MVP Regular Contributor

Use "dojo/dom-construct" in the require and define it in the function variables like domConstruct.

Then after you have initialised your view you can create the button.

   

// create DOM object
recenterBtn = domConstruct.toDom("<div class='map-button esri-component esri-locate esri-widget--button esri-widget' role='button' title='Recenter'><span aria-hidden='true' role='presentation' class='esri-icon esri-icon-globe'></span></div>");
// add to view
view.ui.add(recenterBtn, "bottom-right");
// add button click listener
recenterBtn.addEventListener('click', recenterFunction);‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

Use the MapView goTo() function to set the center view: MapView | ArcGIS API for JavaScript 4.14 

Add your own CSS to style it as you wish.

Also see the Custom Recenter Widget | ArcGIS API for JavaScript 4.14 example for the TypeScript version.

View solution in original post

8 Replies
FC_Basson
MVP Regular Contributor

What version of the API are you using? 3.x or 4.x?

0 Kudos
rsharma
Frequent Contributor

4.x

0 Kudos
FC_Basson
MVP Regular Contributor

Use "dojo/dom-construct" in the require and define it in the function variables like domConstruct.

Then after you have initialised your view you can create the button.

   

// create DOM object
recenterBtn = domConstruct.toDom("<div class='map-button esri-component esri-locate esri-widget--button esri-widget' role='button' title='Recenter'><span aria-hidden='true' role='presentation' class='esri-icon esri-icon-globe'></span></div>");
// add to view
view.ui.add(recenterBtn, "bottom-right");
// add button click listener
recenterBtn.addEventListener('click', recenterFunction);‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

Use the MapView goTo() function to set the center view: MapView | ArcGIS API for JavaScript 4.14 

Add your own CSS to style it as you wish.

Also see the Custom Recenter Widget | ArcGIS API for JavaScript 4.14 example for the TypeScript version.

FC_Basson
MVP Regular Contributor

You must still create the recenter function e.g.

function recenterView(){
  view.goTo({
    center: [-126, 49],
    zoom: 15
  });‍‍‍‍
}

which you then call with the listener

recenterBtn.addEventListener('click', recenterView);
rsharma
Frequent Contributor

OK now it is clickable, but it do only zoom where ever it is on world map, it do not goto to that exact location of x and y axis(lat,long)

view.when(function() {
          // Add the boundary polygon and new lot polygon graphics
          addGraphics();
          // create DOM object
          var recenterBtn = domConstruct.toDom("<div class='map-button esri-component esri-locate esri-widget--button esri-widget' role='button' title='Recenter'><span aria-hidden='true' role='presentation' class='esri-icon esri-icon-globe'></span></div>");

          // add to view
          view.ui.add(recenterBtn, "bottom-right");
          // add button click listener
          recenterBtn.addEventListener('click', recenterView);
             function recenterView(){
                view.goTo({
                center: ['<?=$map_xy_coordinate['x']?>', '<?=$map_xy_coordinate['y']?>'],
                  zoom: 15
                });
              }
         });

0 Kudos
rsharma
Frequent Contributor

Its done, it was some minor error, btw thanks alot for ur kind efforts.

So now i will create  basemap buttons with the same method now, but what method do i call on click to change basemap

0 Kudos
FC_Basson
MVP Regular Contributor

It depends on how many basemaps you want to use.

Have a look at the documentation for BasemapToggle | ArcGIS API for JavaScript 4.14 and the BasemapGallery | ArcGIS API for JavaScript 4.14 widgets.

Otherwise the function for switching basemaps can be simple as:

function setbasemap(basemap){
   map.basemap = basemap;
}‍‍‍

and attach the click event to the buttons

basemapBtn.addEventListener('click', function(){
  setbasemap('hybrid');
});
rsharma
Frequent Contributor

Thanks Basson it worked for me

0 Kudos