This is a migrated thread and some comments may be shown as answers.

MouseEnter Event on MapPolyLine and MapPolygon

3 Answers 163 Views
Map
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Vidyadhar
Top achievements
Rank 1
Vidyadhar asked on 07 Feb 2012, 01:06 AM
Hi,

Am trying to change the cursor icon for the elements on the information layer of the RadMap once the user takes his mouse pointer over the element. Am importing the elements using the KML DataImport feature. And foreach element from the List of Framework Elements, am getting the type of the element, typecasting it to the respective type(MapPinPoint,MapPolyLine and the MapPolygon).
Now I was able to bind the MouseClickevents for the elements for all of them, but not the MouseEnterEvent, which works for the MapPinPoint, but not for the MapPolyLine and MapPolygon. Am not sure if I have to do anything else to get that done for the Polyline and Polygon.
Can you please suggest me anything?

Thanks,
Viddy.

3 Answers, 1 is accepted

Sort by
0
Andrey
Telerik team
answered on 09 Feb 2012, 09:46 AM
Hello Viddy,

I suppose that you attach events to the framework element directly without casting it to the MapShape type before. Correspondent code should looks like the following:

foreach (FrameworkElement item in eventArgs.Items)
{
    MapShape shape = item as MapShape;
    if (shape != null)
    {
        shape.MouseEnter += new MouseEventHandler(this.ShapeMouseEnter);
        shape.MouseLeave += new MouseEventHandler(this.ShapeMouseLeave);
    }
}


Regards,
Andrey Murzov
the Telerik team
Sharpen your .NET Ninja skills! Attend Q1 webinar week and get a chance to win a license! Book your seat now >>
0
Vidyadhar
Top achievements
Rank 1
answered on 21 Apr 2012, 01:20 AM
Hi Andrey,
I have kinda moved away of this for a while. So, I tried to cast the elements from the list into their respective types and attached the mouse enter event handler. And in the handler, I tried to change the cursor icon. But the icon doesn't change when the mouse enters the element.

Here's my XAML:

<UserControl x:Class="TestMouseCursorIcon.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="400">

    <Grid x:Name="LayoutRoot" Background="White">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
        <Button Content="Load Map" Height="50" Width="75" Click="Button_Click" />
        <telerik:RadMap Name="MyMap" Grid.Row="1">
            <telerik:InformationLayer Name="infolayer"/>
        </telerik:RadMap>
    </Grid>
    
</UserControl>


CodeBehind:

using System;
using System.Collections.Generic;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.IO;
using Telerik.Windows.Controls.Map;

namespace TestMouseCursorIcon
{
    public partial class MainPage : UserControl
    {
        List<FrameworkElement> Items = new List<FrameworkElement>();
        public MainPage()
        {
            InitializeComponent();
            this.MyMap.Provider = new BingMapProvider();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            WebClient webclient = new WebClient();
            webclient.OpenReadCompleted += new OpenReadCompletedEventHandler(webclient_OpenReadCompleted);
            webclient.OpenReadAsync(new Uri("/PhaseAngleOverlay.Kml", UriKind.Relative));
        }

        void webclient_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
        {
            StreamReader sr = new StreamReader(e.Result);
            Items = KmlReader.Read(sr.ReadToEnd());

            foreach (FrameworkElement item in Items)
            {
                if (item.GetType() == typeof(MapShape))
                {
                    var placemark = item as MapShape;
                    placemark.MouseEnter += new MouseEventHandler(placemark_MouseEnter);
                }
                if (item.GetType() == typeof(MapPinPoint))
                {
                    var placemark = item as MapPinPoint;
                    placemark.MouseEnter += new MouseEventHandler(placemark_MouseEnter);
                }
                if (item.GetType() == typeof(MapPolyline))
                {
                    var placemark = item as MapPolyline;
                    placemark.MouseEnter += new MouseEventHandler(placemark_MouseEnter);
                }
                if (item.GetType() == typeof(MapPolygon))
                {
                    var placemark = item as MapPolygon;
                    placemark.MouseEnter += new MouseEventHandler(placemark_MouseEnter);
                }
            }
            this.infolayer.ItemsSource = Items;
        }

        void placemark_MouseEnter(object sender, MouseEventArgs e)
        {
            if (sender.GetType() == typeof(MapShape))
            {
                MapShape shape = sender as MapShape;
                shape.Cursor = Cursors.Hand;
            }
            if (sender.GetType() == typeof(MapPinPoint))
            {
                MapPinPoint point = sender as MapPinPoint;
                point.Cursor = Cursors.Hand;
            }
            if (sender.GetType() == typeof(MapPolyline))
            {
                MapPolyline line = sender as MapPolyline;
                line.Cursor = Cursors.Hand;
            }
        }
    }
}

MyKML:

<?xml version="1.0" encoding="utf-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2" xmlns:gx="http://www.google.com/kml/ext/2.2" xmlns:kml="http://www.opengis.net/kml/2.2" xmlns:atom="http://www.w3.org/2005/Atom">
  <Document>
    <name>Phase Angle Vector KML</name>
    <Style id="sn_intersectionStyle">
      <LineStyle>
        <color>FFFFFFFF</color>
        <width>4</width>
      </LineStyle>
    </Style>
    <Style id="sn_vectorLineGreen">
      <LineStyle>
        <color>FF00FF00</color>
        <width>2</width>
      </LineStyle>
    </Style>
    <Style id="sn_vectorLineYellow">
      <LineStyle>
        <color>FF00FFFF</color>
        <width>2</width>
      </LineStyle>
    </Style>
    <Style id="sn_vectorLineRed">
      <LineStyle>
        <color>FF0000FF</color>
        <width>2</width>
      </LineStyle>
    </Style>
    <Placemark>
      <name>Element1</name>
      <description>JohnDavid</description>
      <styleUrl>#sn_intersectionStyle</styleUrl>
      <Polygon>
        <outerBoundaryIs>
          <LinearRing>
            <coordinates>-120.723791532451,45.7178720040713,0 -120.723042216871,45.7167065629117,0 -120.722148683575,45.7155914399742,0 -120.721117651737,45.7145353512768,0 -120.71995692431,45.7135466030573,0 -120.718675341561,45.7126330223076,0 -120.717282730733,45.7118018893886,0 -120.71578984388,45.7110598723788,0 -120.714208282746,45.7104129634789,0 -120.712550415568,45.709866416927,0 -120.710829281689,45.7094246987039,0 -120.709058478211,45.7090914343267,0 -120.707252055286,45.7088693685708,0 -120.705424382736,45.7087603355137,0 -120.703590024342,45.7087652389643,0 -120.701763607439,45.7088840378903,0 -120.699959685707,45.7091157483318,0 -120.698192606766,45.7094584574452,0 -120.696476384111,45.7099093425624,0 -120.694824574286,45.7104647031513,0 -120.693250160848,45.711120006123,0 -120.691765449556,45.7118699304845,0 -120.690381973793,45.7127084225077,0 -120.68911040771,45.713628759444,0 -120.687960497649,45.71462361141,0 -120.686940994711,45.7156851074371,0 -120.68605961108,45.7168049044219,0 -120.68532297283,45.7179742525761,0 -120.684736587706,45.7191840663949,0 -120.684304822755,45.7204249863583,0 -120.68403088533,45.7216874505123,0 -120.683916807235,45.7229617542546,0 -120.683963442314,45.7242381152096,0 -120.684170457127,45.7255067374552,0 -120.684536334482,45.7267578755837,0 -120.685058380442,45.7279818959484,0 -120.685732730552,45.7291693408886,0 -120.686554366431,45.7303109971588,0 -120.687517139168,45.7313979605626,0 -120.68861379747,45.7324217019272,0 -120.689836020404,45.7333741362784,0 -120.691174472166,45.7342476918544,0 -120.692618851299,45.7350353762671,0 -120.694157965234,45.7357308415157,0 -120.695779803522,45.736328446695,0 -120.697471635462,45.7368233180088,0 -120.699220108328,45.7372113968384,0 -120.701011357426,45.7374894879261,0 -120.70283113039,45.7376552924577,0 -120.704664915621,45.7377074371847,0 -120.706498070807,45.7376454908185,0 -120.708315959676,45.7374699661779,0 -120.710104086573,45.7371823166076,0 -120.711848227944,45.7367849187849,0 -120.713534559432,45.7362810441025,0 -120.715149777161,45.7356748226792,0 -120.716681206443,45.7349711974711,0 -120.718116907785,45.7341758718994,0 -120.719445758675,45.7332952519472,0 -120.720657539559,45.7323363824522,0 -120.721742999029,45.7313068813885,0 -120.722693909214,45.7302148752267,0 -120.723503108884,45.7290689270537,0 -120.72416454583,45.7278779721459,0 -120.724673300551,45.7266512464086,0 -120.725025611096,45.7253982253446,0 -120.725218880037,45.7241285529397,0 -120.725251695774,45.722851983515,0 -120.725123825168,45.7215783149032,0 -120.724836222619,45.7203173231011,0 -120.724391022506,45.7190787037061,0 -120.723791532451,45.7178720040713,0</coordinates>
          </LinearRing>
        </outerBoundaryIs>
      </Polygon>
    </Placemark>
    <Placemark>
      <name>Element2</name>
      <styleUrl>#sn_vectorLineYellow</styleUrl>
      <LineString>
        <coordinates>-120.695315545739,45.7361573784503,0 -119.029291010421,47.9537808164508,0</coordinates>
      </LineString>
    </Placemark>
    <Placemark>
      <name>30</name>
      <Point>
        <coordinates>-118.856,46.9539,0</coordinates>
      </Point>
    </Placemark>
    </Document>
</kml>

I can see the cursor changing for the "Point" but not for the Polyline and Polygon. Can you guide me where am I going wrong.

Thanks in advance,
Viddy.
0
Andrey
Telerik team
answered on 25 Apr 2012, 01:21 PM
Hello Vidyadhar,

The problem is in your placemark_MouseEnter method. You shouldn't set cursor for the map shape, but to the parent user control. For example:

private Cursor oldCursor;
 
private void ShapeMouseEnter(object sender, MouseEventArgs e)
{
    MapShape shape = sender as MapShape;
    if (shape != null)
    {
        oldCursor = this.Cursor;
        this.Cursor = Cursors.Hand;
    }
}
 
private void ShapeMouseLeave(object sender, MouseEventArgs e)
{
    MapShape shape = sender as MapShape;
    if (shape != null)
    {
        this.Cursor = oldCursor;
    }
}


Greetings,
Andrey Murzov
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

Tags
Map
Asked by
Vidyadhar
Top achievements
Rank 1
Answers by
Andrey
Telerik team
Vidyadhar
Top achievements
Rank 1
Share this question
or