Infrastructure Map Server Forum
Welcome to Autodesk’s Infrastructure Map Server Forums. Share your knowledge, ask questions, and explore popular Infrastructure Map Server topics.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

turning on/off layers in Mapguide Enterprise

8 REPLIES 8
Reply
Message 1 of 9
scluna
1063 Views, 8 Replies

turning on/off layers in Mapguide Enterprise

How do I turn on or off a layer. Could anyone provide a code snippet?

Thanks
SCLuna
8 REPLIES 8
Message 2 of 9
jumpinjackie
in reply to: scluna

get the MgLayer object in question from your MgMap object.

call SetVisible(false) on that layer object. Save your map object and do a refresh on the client side.

here's a php example:

$map = new MgMap();
$map->Open($resourceService, $mapName);

$layerName = "Parcels";
$visible = true; //or false

$mapLayers = $map->GetLayers();
$layer = null;
for($i = 0; $i < $mapLayers->GetCount(); $i++)
{
$currentLayer = $mapLayers->GetItem($i);
if($currentLayer->GetName() == $layerName)
{
$layer = $currentLayer;
}
}

if($layer != null)
{
$layer->SetVisible($visible);
$layer->ForceRefresh();
$map->Save($resourceService);
}
http://themapguyde.blogspot.com
http://au.linkedin.com/in/jackieng
Message 3 of 9
scluna
in reply to: scluna

thanks for the idea.
actually i have run may asp.net code and getting an MgUnclassified Exception. when I turn the layers on and off.
The error is in line 22 when i pass the sessionid.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: OSGeo.MapGuide.MgUnclassifiedException: An unclassified exception occurred.

Source Error:


Line 20: InitializeWebTier();
Line 21:
Line 22: MgUserInformation userInfo = new MgUserInformation(SessionId);
Line 23: MgSiteConnection siteConnection = new MgSiteConnection();
Line 24: siteConnection.Open(userInfo);

___________________________________________
MgLayerCollection LayerCol = map.GetLayers();
MgLayer layer = LayerCol.GetItem("Soils");
if (layer.IsVisible())
{
layer.SetVisible(false);
}
else
{
layer.SetVisible(true);
}

layer.ForceRefresh();
map.Save(resourceService);
SCLuna
Message 4 of 9
jumpinjackie
in reply to: scluna

Try the following:

- Check the value of sessionId, it's gotta look like a long string of hexadecimal numbers.

- Wrap that code in a try-catch block, catch MgException and call its GetDetails(). Hopefully, that can reveal more information.
http://themapguyde.blogspot.com
http://au.linkedin.com/in/jackieng
Message 5 of 9
SAustin
in reply to: scluna

I've gotten that error when trying to change the visibility of base map layers with AJAX viewer. Here is a quote from the Web API Reference doc that helped me:

the visibility of base map layers (those of type MgLayerType::BaseMap) cannot be changed. Attempting to do so will throw an MgInvalidOperationException

http://mapguide.osgeo.org/files/mapguide/docs/webapi/d6/de2/class_mg_layer_1970fbe700599c7fd6f8603708158486.html#1970fbe700599c7fd6f8603708158486
Message 6 of 9
scluna
in reply to: scluna

Thanks jackie/SAustin,

I already made a workaround on this and it's working now.
SCLuna
Message 7 of 9
ales6
in reply to: scluna

Hi SCLuna

Currently searching solution (workaround) form my .net sample
..did not found solution how to fix the problem.

I still get message:
Type 'OSGeo.MapGuide.MgUnclassifiedException' in Assembly 'MapGuideDotNetApi, Version=1.1.0.301, Culture=neutral, PublicKeyToken=null' is not marked as serializable.

Can you help me, please.

by ales6
Message 8 of 9
SAustin
in reply to: scluna

This is actually possible with base layers Here is some info that I used in my solution.
http://www.nabble.com/Fwd:-Swithing-base-MgLayerGroup-on-off-...-fails--td14454152.html
Message 9 of 9

Thanks for the code Jakie.

 

I added a snipet to the search.php to turn on a layer that was being searched if it was off by default.

below is the code I added an else statement at the end.  Seems to work ok.

 

 

try
{
$featureSrvc = $siteConnection->CreateService(MgServiceType::FeatureService);

//Create a temporary map runtime object, locate the layer
$map = new MgMap();
$map->Open($resourceService, $mapName);
$layers = $map->GetLayers();
$layer = null;
//fwrite($logHandle,"found layers:".$layers->GetCount()."\n");
for($i = 0; $i < $layers->GetCount(); $i++)
{
$layer = $layers->GetItem($i);
//fwrite($logHandle,"found layer:".$layer->GetName()."\n");
if($layer->GetName() == $layerName)

break;
}

if($layer == null)
{
trigger_error(FormatMessage("SEARCHLAYERNOTFOUND", $locale, array($layerName)));
}
else
{
$layer->SetVisible($visible);
$layer->ForceRefresh();
$map->Save($resourceService);
}

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report