2 * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the License);
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an AS IS BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 using Layout = ElmSharp.Layout;
20 using EvasObject = ElmSharp.EvasObject;
21 using System.Collections.Generic;
25 public class MapView : Layout
27 internal Interop.ViewHandle handle;
28 private Interop.ServiceHandle _service;
30 private HashSet<MapObject> _children = new HashSet<MapObject>();
32 private Interop.View.ViewOnEventCallback _gestureEventCallback;
33 private Interop.View.ViewOnEventCallback _objectEventCallback;
34 private Interop.View.ViewOnEventCallback _viewReadyEventCallback;
36 private event EventHandler<MapGestureEventArgs> _scrolledEventHandler;
37 private event EventHandler<MapGestureEventArgs> _zoomedEventHandler;
38 private event EventHandler<MapGestureEventArgs> _tappedEventHandler;
39 private event EventHandler<MapGestureEventArgs> _doubleTappedEventHandler;
40 private event EventHandler<MapGestureEventArgs> _twoFingerTappedEventHandler;
41 private event EventHandler<MapGestureEventArgs> _rotatedEventHandler;
42 private event EventHandler<MapGestureEventArgs> _longPressedEventHandler;
43 private event EventHandler _viewReadyEventHandler;
45 public MapView(EvasObject parent, MapService service) : base(parent)
48 var err = Interop.View.Create(service.handle, this, out nativeHandle);
49 err.ThrowIfFailed("Failed to create native map view handle");
51 handle = new Interop.ViewHandle(nativeHandle);
52 _service = service.handle;
54 Console.WriteLine($"MapView Created: ServiceHandle: {(IntPtr)_service}, ViewHandle: {(IntPtr)handle}");
56 // We need to keep Gesture Tap event enabled for object event to work
57 Interop.View.SetGestureEnabled(handle, Interop.ViewGesture.Tap, true);
58 SetObjectEventCallback();
61 public event EventHandler<MapGestureEventArgs> Scrolled
65 SetGestureEventCallback();
66 Interop.View.SetGestureEnabled(handle, Interop.ViewGesture.Scroll, true);
67 _scrolledEventHandler += value;
71 _scrolledEventHandler -= value;
72 if (_scrolledEventHandler == null)
74 Interop.View.SetGestureEnabled(handle, Interop.ViewGesture.Scroll, false);
75 UnsetGestureEventCallback();
80 public event EventHandler<MapGestureEventArgs> ZoomChanged
84 SetGestureEventCallback();
85 Interop.View.SetGestureEnabled(handle, Interop.ViewGesture.Zoom, true);
86 _zoomedEventHandler += value;
90 _zoomedEventHandler -= value;
91 if (_zoomedEventHandler == null)
93 Interop.View.SetGestureEnabled(handle, Interop.ViewGesture.Zoom, false);
94 UnsetGestureEventCallback();
99 public event EventHandler<MapGestureEventArgs> Clicked
103 SetGestureEventCallback();
104 //Interop.View.SetGestureEnabled(handle, Interop.ViewGesture.Tap, true);
105 _tappedEventHandler += value;
109 _tappedEventHandler -= value;
110 if (_tappedEventHandler == null)
112 //Interop.View.SetGestureEnabled(handle, Interop.ViewGesture.Tap, false);
113 UnsetGestureEventCallback();
118 public event EventHandler<MapGestureEventArgs> DoubleClicked
122 SetGestureEventCallback();
123 Interop.View.SetGestureEnabled(handle, Interop.ViewGesture.DoubleTap, true);
124 _doubleTappedEventHandler += value;
128 _doubleTappedEventHandler -= value;
129 if (_doubleTappedEventHandler == null)
131 Interop.View.SetGestureEnabled(handle, Interop.ViewGesture.DoubleTap, false);
132 UnsetGestureEventCallback();
137 public event EventHandler<MapGestureEventArgs> TwoFingerPressed
141 SetGestureEventCallback();
142 Interop.View.SetGestureEnabled(handle, Interop.ViewGesture.TwoFingerTap, true);
143 _twoFingerTappedEventHandler += value;
147 _twoFingerTappedEventHandler -= value;
148 if (_twoFingerTappedEventHandler == null)
150 Interop.View.SetGestureEnabled(handle, Interop.ViewGesture.TwoFingerTap, false);
151 UnsetGestureEventCallback();
156 public event EventHandler<MapGestureEventArgs> Rotated
160 SetGestureEventCallback();
161 Interop.View.SetGestureEnabled(handle, Interop.ViewGesture.Rotate, true);
162 _rotatedEventHandler += value;
166 _rotatedEventHandler -= value;
167 if (_rotatedEventHandler == null)
169 Interop.View.SetGestureEnabled(handle, Interop.ViewGesture.Rotate, false);
170 UnsetGestureEventCallback();
175 public event EventHandler<MapGestureEventArgs> LongPressed
179 SetGestureEventCallback();
180 Interop.View.SetGestureEnabled(handle, Interop.ViewGesture.LongPress, true);
181 _longPressedEventHandler += value;
185 _longPressedEventHandler -= value;
186 if (_longPressedEventHandler == null)
188 Interop.View.SetGestureEnabled(handle, Interop.ViewGesture.LongPress, false);
189 UnsetGestureEventCallback();
194 public event EventHandler ViewReady
198 SetViewReadyEventCallback();
199 _viewReadyEventHandler += value;
203 _viewReadyEventHandler -= value;
204 UnsetGestureEventCallback();
213 Interop.View.GetZoomLevel(handle, out value);
218 Interop.View.SetZoomLevel(handle, value);
222 public int MinimumZoomLevel
227 Interop.View.GetMinZoomLevel(handle, out value);
232 Interop.View.SetMinZoomLevel(handle, value);
236 public int MaximumZoomLevel
241 Interop.View.GetMaxZoomLevel(handle, out value);
246 Interop.View.SetMaxZoomLevel(handle, value);
250 public double Orientation
255 Interop.View.GetOrientation(handle, out value);
260 Interop.View.SetOrientation(handle, value);
264 public MapTypes MapType
268 Interop.ViewType value;
269 Interop.View.GetType(handle, out value);
270 return (MapTypes)value;
274 Interop.View.SetType(handle, (Interop.ViewType)value);
278 public bool BuildingsEnabled
283 Interop.View.GetBuildingsEnabled(handle, out value);
288 Interop.View.SetBuildingsEnabled(handle, value);
292 public bool TrafficEnabled
297 Interop.View.GetTrafficEnabled(handle, out value);
302 Interop.View.SetTrafficEnabled(handle, value);
306 public bool PublicTransitEnabled
311 Interop.View.GetPublicTransitEnabled(handle, out value);
316 Interop.View.SetPublicTransitEnabled(handle, value);
320 public bool ScalebarEnabled
325 Interop.View.GetScalebarEnabled(handle, out value);
330 Interop.View.SetScalebarEnabled(handle, value);
334 public string Language
339 Interop.View.GetLanguage(handle, out value);
344 Interop.View.SetLanguage(handle, value);
348 public Geocoordinates Center
352 IntPtr coordinateHandle;
353 Interop.View.GetCenter(handle, out coordinateHandle);
354 return new Geocoordinates(coordinateHandle);
358 Geocoordinates geocoordinate = value;
359 Interop.View.SetCenter(handle, geocoordinate.handle);
363 public IEnumerable<MapObject> Children
371 public Point GeolocationToScreen(Geocoordinates coordinates)
373 Point screenCoordinates = new Point();
374 Geocoordinates geocoordinate = coordinates;
375 Interop.View.GeolocationToScreen(handle, geocoordinate.handle, out screenCoordinates.X, out screenCoordinates.Y);
376 return screenCoordinates;
379 public Geocoordinates ScreenToGeolocation(Point screenCoordinates)
381 IntPtr coordinateHandle;
382 Interop.View.ScreenToGeolocation(handle, screenCoordinates.X, screenCoordinates.Y, out coordinateHandle);
383 return new Geocoordinates(coordinateHandle);
386 public void Add(MapObject child)
388 child.handle.ReleaseOwnership();
389 _children.Add(child);
390 child.AddToMapObjectTable();
391 Interop.View.AddObject(handle, child.handle);
394 public void Remove(MapObject child)
396 _children.Remove(child);
397 child.RemoveFromMapObjectTable();
398 Interop.View.RemoveObject(handle, child.handle);
401 public void RemoveAll()
403 foreach (var child in _children)
405 child.RemoveFromMapObjectTable();
408 Interop.View.RemoveAllObjects(handle);
411 public void CaptureSnapshot(SnapshotType type, int quality, string path)
413 var err = Interop.ViewSnapshot.ViewCaptureSnapshot(handle, (Interop.ViewSnapshotFormatType)type, quality, path);
414 err.ThrowIfFailed("Failed to create snapshot for the view");
417 private void SetGestureEventCallback()
419 if (_gestureEventCallback == null)
421 _gestureEventCallback = (type, eventData, userData) =>
423 if (type != Interop.ViewEventType.Gesture) return;
424 var eventArg = MapGestureEventArgs.Create(eventData);
425 switch (eventArg.GestureType)
427 case GestureType.Scroll: _scrolledEventHandler?.Invoke(this, eventArg); break;
428 case GestureType.Zoom: _zoomedEventHandler?.Invoke(this, eventArg); break;
429 case GestureType.Tap: _tappedEventHandler?.Invoke(this, eventArg); break;
430 case GestureType.DoubleTap: _doubleTappedEventHandler?.Invoke(this, eventArg); break;
431 case GestureType.TwoFingerTap: _twoFingerTappedEventHandler?.Invoke(this, eventArg); break;
432 case GestureType.Rotate: _rotatedEventHandler?.Invoke(this, eventArg); break;
433 case GestureType.LongPress: _longPressedEventHandler?.Invoke(this, eventArg); break;
436 Interop.View.SetEventCb(handle, Interop.ViewEventType.Gesture, _gestureEventCallback, IntPtr.Zero);
440 private void UnsetGestureEventCallback()
442 if (_scrolledEventHandler != null || _zoomedEventHandler != null
443 || _tappedEventHandler != null || _doubleTappedEventHandler != null
444 || _twoFingerTappedEventHandler != null || _rotatedEventHandler != null
445 || _longPressedEventHandler != null)
450 Interop.View.UnsetEventCb(handle, Interop.ViewEventType.Gesture);
451 _gestureEventCallback = null;
454 private void SetObjectEventCallback()
456 if (_objectEventCallback == null)
458 _objectEventCallback = (type, eventData, userData) =>
460 if (type != Interop.ViewEventType.Object) return;
461 var eventArg = MapObjectEventArgs.Create(eventData);
462 switch (eventArg.GestureType)
464 case GestureType.Tap: eventArg.ViewObject.HandleClickedEvent(); break;
467 Interop.View.SetEventCb(handle, Interop.ViewEventType.Object, _objectEventCallback, IntPtr.Zero);
471 private void SetViewReadyEventCallback()
473 if (_viewReadyEventCallback == null)
475 _viewReadyEventCallback = (type, eventData, userData) =>
477 _viewReadyEventHandler?.Invoke(this, EventArgs.Empty);
479 Interop.View.SetEventCb(handle, Interop.ViewEventType.Ready, _viewReadyEventCallback, IntPtr.Zero);
483 private void UnsetViewReadyEventCallback()
485 if (_viewReadyEventHandler == null)
487 Interop.View.UnsetEventCb(handle, Interop.ViewEventType.Ready);
488 _viewReadyEventCallback = null;