📜 ⬆️ ⬇️

Using Yandex MapKit with Pivot and Panorama controls

The main page of our application is built using the Pivot control, on one of the bookmarks of which it was necessary to place a map with information about the location of the car. Users of our application, it was decided to provide a choice between map services from Microsoft and Yandex.

Integration of the integrated map service was not difficult, since the control element for displaying maps is part of the operating system, and its integration with all standard controls has been worked out to the smallest detail.

However, with the integration of a control for displaying maps included in Yandex.Map MapKit from Yandex, unexpected difficulties arose. Attempts to manipulate the map in the horizontal plane led to the switching of the current tab of the Pivot control.
')
The study of the problem showed that in Windows Phone 8, gestures were optimized. As a result, user manipulation events can be intercepted by built-in controls, before passing them to child controls. The following control elements intercept events: DrawingSurface, DrawingSurfaceBackgroundGrid, ListBox, LongListSelector, Map, Panorama, Pivot, ScrollViewer, ViewportControl, WebBrowser.

Unfortunately, Yandex.Maps MapKit is no longer supported by Yandex, and the latest available version is designed for Windows Phone 7.5 in which these optimizations are missing.

Optimization of the operating system can be disabled by setting the control's UseOptimizedManipulationRouting property to false. But after setting the value of the property, the behavior of the application has not changed. The thing is that by including gesture processing for the control, you also need to handle the events: ManipulationStarted , ManipulationDelta, and ManipulationCompleted . We do not need to handle user gestures in these handlers, since the Map control is doing this; it is enough to set the Handled flag to true, informing the other controls that the event has already been processed.

After the described actions, the application starts to work as expected, namely when manipulating the map in all planes, the current tab of the Pivot control does not change.

Conducting these actions can be transferred to the declarative level by implementing the attached property and implementing all the above described in its handler.

using System.Windows; using Yandex.Maps; namespace YandexMapKit { public static class YandexMapHelper { public static readonly DependencyProperty FixManipulationProperty = DependencyProperty.RegisterAttached( "FixManipulation", typeof(bool), typeof(YandexMapHelper), new PropertyMetadata(OnFixManipulationChanged)); public static void SetFixManipulation(DependencyObject element, bool value) { element.SetValue(FixManipulationProperty, value); } public static bool GetFixManipulation(DependencyObject element) { return (bool) element.GetValue(FixManipulationProperty); } private static void OnFixManipulationChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { var map = d as Map; if (map == null) { return; } var fixManipulation = (bool?) e.NewValue; if (fixManipulation == true) { map.UseOptimizedManipulationRouting = false; map.ManipulationStarted += MapManipulationStarted; map.ManipulationCompleted += MapManipulationCompleted; map.ManipulationDelta += MapManipulationDelta; return; } fixManipulation = (bool?)e.OldValue; if (fixManipulation == true) { map.UseOptimizedManipulationRouting = true; map.ManipulationStarted -= MapManipulationStarted; map.ManipulationCompleted -= MapManipulationCompleted; map.ManipulationDelta -= MapManipulationDelta; } } private static void MapManipulationDelta(object sender, System.Windows.Input.ManipulationDeltaEventArgs e) { e.Handled = true; } private static void MapManipulationCompleted(object sender, System.Windows.Input.ManipulationCompletedEventArgs e) { e.Handled = true; } private static void MapManipulationStarted(object sender, System.Windows.Input.ManipulationStartedEventArgs e) { e.Handled = true; } } } 


Now you can set this property to true for the Map control in the markup of the Map control without writing extra code.

 <phone:Pivot Title="YANDEX MAPKIT TEST APP"> <phone:PivotItem Header=""> <StackPanel> <RichTextBox VerticalAlignment="Top" VerticalContentAlignment="Top"> <Paragraph> <Run Text="     Map    Yandex.Maps MapKit       Pivot"/> </Paragraph> </RichTextBox> <CheckBox x:Name="viewFixManipulation" Content=" " VerticalAlignment="Top" /> </StackPanel> </phone:PivotItem> <phone:PivotItem Header=""> <Grid> <yandexMaps:Map yandexMapKit:YandexMapHelper.FixManipulation="{Binding IsChecked, ElementName=viewFixManipulation}" /> </Grid> </phone:PivotItem> </phone:Pivot> 


All of the above is applicable to other controls for which horizontal user gestures matter, such as the Slider control. By slightly modifying the code of the attached property, you can make it universal and use it with any control that needs to process the user's horizontal gestures.

Test project available on GitHub

Source: https://habr.com/ru/post/221477/


All Articles