Disable Map double-click zoom

6490
8
09-29-2010 10:03 AM
KevinSchumm
New Contributor
I'm hoping this is an easy one.

I would like to disable the zoom that occurs when you double click the map.

I have written some code to allow the users to double-click a feature to bring up the detailed information on the feature, but currently if the user double-clicks too fast, they get the detailed information and the map zooms just a little.

They still need to be able to zoom with the scroll wheel, so turning zoom factor to 0 isn't a great option.

Suggestions?
0 Kudos
8 Replies
dotMorten_esri
Esri Notable Contributor
Handle the MouseDown event and set e.Handled = true;
0 Kudos
KevinSchumm
New Contributor
Excellent. Works like a charm.

You're always very helpful, Morton.
0 Kudos
KevinSchumm
New Contributor
I apologize, Morton.

I thought this had fixed my problem and apparently I didn't test it to thoroughly.  It would appear that when I set e.handled = true on the Map.MouseLeftButtonDown, this also suppresses the ability to click and drag to pan the map.

Am I doing something wrong? All I really want is to disable the zoom on double click without affecting any other functions of the map.

Thanks
0 Kudos
JenniferNery
Esri Regular Contributor
Mark Map.MouseLeftButtonUp as handled instead.
0 Kudos
JeffJackson
New Contributor III
Handling MouseLeftButtonUp does not work. If the mouse moves slightly during a double-click then the map gets into a weird state where its panning even though the mouse button is no longer down.

Is there some other way to eliminate the double-click to zoom in behavior?

Thanks,
Jeff
0 Kudos
PatrickBrooke
New Contributor II
You can do what I did and create a custom double click handler...a piece of code that can identify double clicks.  Basically what I do is set a time up to time between click 400 ms or less between clicks equals a double click in my app. I checked to see if my double clik logic could be applied to help you, and my testing came out positive. Here is how it would be done to disable double click zoom. Here is the entire user control. I know it is a bit duct tapeish, but that is how most things work...


public partial class MainPage : UserControl
    {
        bool dblClick = false;
        DispatcherTimer TimeDblClick = new DispatcherTimer();

        public MainPage()
        {
            InitializeComponent();

            TimeDblClick.Tick += new EventHandler(DblClickTimer);
            TimeDblClick.Interval = TimeSpan.FromMilliseconds(400);
        }

        private void DblClickTimer(object sender, EventArgs args)
        {
            dblClick = false;
            TimeDblClick.Stop();
        }

        private void Map_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (dblClick)
            {
                //if it is a double click do this...

                e.Handled = true;
                dblClick = false;
                TimeDblClick.Stop();          
               
            }

            else //User has not clicked here before (a single click), start double click timer
            {
                //not a double click so don't handle the mousedown
                e.Handled = false;
                dblClick = true;
                //restart timer to count time between clicks
                TimeDblClick.Start();
                
            }

        }
    }

0 Kudos
SaiduganovArslan
New Contributor
Here is a simple static class, MouseButtonHelper, that offers a single method, IsDoubleClick,  to determine if a standard MouseLeftButtonDown or MouseLeftButtonUp event is a double click.  In the past I have used timers, and Triggers and Behaviors to accomplish the same thing, but this approach is less code, less XAML, and uses a lot less resources.

Usage is as follows:

    In XAML or in code behind, handle the standard MouseLeftButtonDown or MouseLeftButtonUp event for the object you wish to perform double click handling.
    Implement the handler function for the above event as follows:

private void MyMap_MouseClick(object sender, Map.MouseEventArgs e)
        {
            e.Handled = true;
            bool doubleClick = MouseButtonHelper.IsDoubleClick(sender, e);
            if (doubleClick)
                MessageBox.Show("Double click detected!", "Alert", MessageBoxButton.OK);
        }

The code below has been tested and used with Silverlight 4.

using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace Mapping.ArcGIS
{
    internal static class MouseButtonHelper
    {
        private const long k_DoubleClickSpeed = 500;
        private const double k_MaxMoveDistance = 10;

        private static long _LastClickTicks = 0;
        private static Point _LastPosition;
        private static WeakReference _LastSender;

        internal static bool IsDoubleClick(object sender, ESRI.ArcGIS.Client.Map.MouseEventArgs e)
        {
            Point position = e.ScreenPoint;
            long clickTicks = DateTime.Now.Ticks;
            long elapsedTicks = clickTicks - _LastClickTicks;
            long elapsedTime = elapsedTicks / TimeSpan.TicksPerMillisecond;
            bool quickClick = (elapsedTime <= k_DoubleClickSpeed);
            bool senderMatch = (_LastSender != null && sender.Equals(_LastSender.Target));

            if (senderMatch && quickClick && position.Distance(_LastPosition) <= k_MaxMoveDistance)
            {
                // Double click!
                _LastClickTicks = 0;
                _LastSender = null;
                return true;
            }

            // Not a double click
            _LastClickTicks = clickTicks;
            _LastPosition = position;
            if (!quickClick)
                _LastSender = new WeakReference(sender);
            return false;
        }

        private static double Distance(this Point pointA, Point pointB)
        {
            double x = pointA.X - pointB.X;
            double y = pointA.Y - pointB.Y;
            return Math.Sqrt(x * x + y * y);
        }
    }
}
0 Kudos
DimaShats1
New Contributor III

Hi,

Try:

MyMapView.InteractionOptions.ZoomOptions.IsDoubleTappedEnabled = false;

0 Kudos