Select to view content in your preferred language

How do you activate a ARCGIS layer with a button?

4850
18
09-26-2023 09:52 AM
AidanAumell
Emerging Contributor

I am having trouble activating an ArcGIS layer with a button game object. 

 

I want the user to click on a button to activate an ArcGIS layer and make it visible. However, I cannot drag the ARCGIS layer on to the button component function. I have tried producing some code for the interaction, but no lucky yet. Does anyone know how to do this? Thanks  

0 Kudos
18 Replies
and1
by Esri Contributor
Esri Contributor

You can simply replace the service URL with your local file path such as "C:\\Data\\Mylayer.slpk"

0 Kudos
AidanAumell
Emerging Contributor

Thanks! The script works, but it cannot be used twice. I want the script to to show and hide differnet maps on different buttons.  It crashes my Unity Project if I use it twice.

0 Kudos
AidanAumell
Emerging Contributor

Like a tile package from a file on your local computer

0 Kudos
AidanAumell
Emerging Contributor

When I delete line 138 I get this error message.

 

AidanAumell_0-1696015573053.png

 

0 Kudos
and1
by Esri Contributor
Esri Contributor

That's strange you are having that error, it should have that namespace though. Is the SDK installed properly along with the sample packages?

I just tried to create a brand new scene and threw the same script there, I had no errors and everything was running fine.

On a side note, I'd also recommend you to have a read on the API tutorial to understand the detail step of the code I shared.

https://developers.arcgis.com/unity/maps/tutorials/display-a-map-api/

0 Kudos
AidanAumell
Emerging Contributor

I just got it to work! Thank you so much for your help.

The issue was I had an old ArcGIS Unity Plugin installed. I created a new scene with the most up to date ArcGIS Plugin.  

0 Kudos
AidanAumell
Emerging Contributor

This script does not work if you want to use it on more than one button.

0 Kudos
and1
by Esri Contributor
Esri Contributor

You can just add another button onclick event to the script if you want to add another button.

0 Kudos
AidanAumell
Emerging Contributor

Unfortanely, that does not work. 

Simply adding another button in the script as show below. Results in ArcGIS blanking out. No error message is shown,  the plugin just stops working after I add a second.  

public Button addBtn;
public Button addBtn2;

addBtn.onClick.AddListener(delegate { map.Layers.Add(layer_1); });
 
addBtn2.onClick.AddListener(delegate { map.Layers.Add(layer_2); });
0 Kudos