Layer doesn't come in particular areas

2893
1
Jump to solution
02-21-2014 12:48 AM
TahaSözgen
New Contributor
Hi There;
I would like to salutate all users of arcgis in my first thread. I am implementing some codes of the Arcgis api for silverlight, and I've encountered a problem. In the project, map of USA appear on the screen. Assumed  scenerio is prints US state name when user clicks inside USA, prints coordinates when user clicks outside of the USA. However when I clicked outside of the country, there is no graphics comes. How can I handle the problem? Thanks in advance.

Here is the content of the xaml:
[HTML]
    <Grid x:Name="LayoutRoot">

        <Grid.Resources>
           
            <esri:SimpleRenderer x:Key="MySimpleRenderer">
               
                <esri:SimpleRenderer.Symbol>
                    <esri:SimpleFillSymbol Fill="#01FFFFFF" BorderBrush="#88000000" BorderThickness="2" />
                </esri:SimpleRenderer.Symbol>
            </esri:SimpleRenderer>
           
            <!--that DataTemplate shows outside of the USA -->
            <DataTemplate x:Key="LocationInfoWindowTemplate">
                <StackPanel Margin="2">
                    <TextBlock Text="Location:" />
                    <TextBlock Text="{Binding X, StringFormat=X\=\{0:0.000\}}" />
                    <TextBlock Text="{Binding Y, StringFormat=Y\=\{0:0.000\}}" />
                </StackPanel>
            </DataTemplate>

            <!--that DataTemplate shows US states. -->           
            <DataTemplate x:Key="MyFeatureLayerInfoWindowTemplate">
                <TextBlock Text="{Binding [STATE_NAME]}" Foreground="Black" FontSize="12" />
            </DataTemplate>
        </Grid.Resources>

        <esri:Map x:Name="MyMap" WrapAround="False" Extent="-15000000,2000000,-7000000,8000000" MouseClick="MyMap_MouseClick">
            <esri:ArcGISTiledMapServiceLayer ID="Street Map"
                    Url="http://services.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer"/>
           
            <esri:FeatureLayer ID="MyFeatureLayer"
                               Url="http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Demographics/ESRI_Census_USA/MapServer/5"
                               OutFields="STATE_NAME,POP2007"
                               Renderer="{StaticResource MySimpleRenderer}"
                                />
        </esri:Map>

        <!--prints information infowindow.-->
        <esri:InfoWindow x:Name="MyInfoWindow"
                         Padding="2"
                         CornerRadius="20"
                         Background="LightSalmon"
                         Map="{Binding ElementName=MyMap}"
                         ContentTemplate="{StaticResource MyFeatureLayerInfoWindowTemplate}"
                         MouseLeftButtonUp="MyInfoWindow_MouseLeftButtonUp" />
    </Grid>
[/HTML]

here is the code behind:

   private void MyMap_MouseClick(object sender, ESRI.ArcGIS.Client.Map.MouseEventArgs e)         {             FeatureLayer featureLayer = (FeatureLayer) MyMap.Layers["MyFeatureLayer"];             System.Windows.Point screenPnt = MyMap.MapToScreen(e.MapPoint);              GeneralTransform generalTransform = MyMap.TransformToVisual(Application.Current.RootVisual);              System.Windows.Point transformScreenPnt = generalTransform.Transform(screenPnt);              IEnumerable<Graphic> selected = featureLayer.FindGraphicsInHostCoordinates(transformScreenPnt);             //Here is the actual error point. When user clicks outside of the USA selected is null.             foreach (Graphic g in selected)             {                  MyInfoWindow.Anchor = e.MapPoint;                 MyInfoWindow.IsOpen = true;                 //Since a ContentTemplate is defined, Content will define the DataContext for the ContentTemplate                 MyInfoWindow.Content = g.Attributes;                 return;             }                      }
0 Kudos
1 Solution

Accepted Solutions
AhmedEl-Sisi
Occasional Contributor III
You should check graphics count and then switch between your Info Window content templates.

Something like this:

 
   private void MyMap_MouseClick(object sender, ESRI.ArcGIS.Client.Map.MouseEventArgs e)         {                          FeatureLayer featureLayer = (FeatureLayer)MyMap.Layers["MyFeatureLayer"];             System.Windows.Point screenPnt = MyMap.MapToScreen(e.MapPoint);              GeneralTransform generalTransform = MyMap.TransformToVisual(Application.Current.RootVisual);              System.Windows.Point transformScreenPnt = generalTransform.Transform(screenPnt);              IEnumerable<Graphic> selected = featureLayer.FindGraphicsInHostCoordinates(transformScreenPnt);              //Here is the actual error point. When user clicks outside of the USA selected is null.            var selectionEnum= selected.GetEnumerator();            if (!selectionEnum.MoveNext())            {                MyInfoWindow.ContentTemplate = LayoutRoot.Resources["LocationInfoWindowTemplate"] as DataTemplate;                 MyInfoWindow.Anchor = e.MapPoint;                 MyInfoWindow.IsOpen = true;                 //Since a ContentTemplate is defined, Content will define the DataContext for the ContentTemplate                 MyInfoWindow.Content = e.MapPoint;                 return;            }             foreach (Graphic g in selected)             {                 MyInfoWindow.ContentTemplate = LayoutRoot.Resources["MyFeatureLayerInfoWindowTemplate"] as DataTemplate;                 MyInfoWindow.Anchor = e.MapPoint;                 MyInfoWindow.IsOpen = true;                 //Since a ContentTemplate is defined, Content will define the DataContext for the ContentTemplate                 MyInfoWindow.Content = g.Attributes;                 return;             }          }

View solution in original post

1 Reply
AhmedEl-Sisi
Occasional Contributor III
You should check graphics count and then switch between your Info Window content templates.

Something like this:

 
   private void MyMap_MouseClick(object sender, ESRI.ArcGIS.Client.Map.MouseEventArgs e)         {                          FeatureLayer featureLayer = (FeatureLayer)MyMap.Layers["MyFeatureLayer"];             System.Windows.Point screenPnt = MyMap.MapToScreen(e.MapPoint);              GeneralTransform generalTransform = MyMap.TransformToVisual(Application.Current.RootVisual);              System.Windows.Point transformScreenPnt = generalTransform.Transform(screenPnt);              IEnumerable<Graphic> selected = featureLayer.FindGraphicsInHostCoordinates(transformScreenPnt);              //Here is the actual error point. When user clicks outside of the USA selected is null.            var selectionEnum= selected.GetEnumerator();            if (!selectionEnum.MoveNext())            {                MyInfoWindow.ContentTemplate = LayoutRoot.Resources["LocationInfoWindowTemplate"] as DataTemplate;                 MyInfoWindow.Anchor = e.MapPoint;                 MyInfoWindow.IsOpen = true;                 //Since a ContentTemplate is defined, Content will define the DataContext for the ContentTemplate                 MyInfoWindow.Content = e.MapPoint;                 return;            }             foreach (Graphic g in selected)             {                 MyInfoWindow.ContentTemplate = LayoutRoot.Resources["MyFeatureLayerInfoWindowTemplate"] as DataTemplate;                 MyInfoWindow.Anchor = e.MapPoint;                 MyInfoWindow.IsOpen = true;                 //Since a ContentTemplate is defined, Content will define the DataContext for the ContentTemplate                 MyInfoWindow.Content = g.Attributes;                 return;             }          }