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.
18 using System.Collections.Generic;
21 using Color = ElmSharp.Color;
26 /// Polygon map object
28 public class Polygon : MapObject
30 private List<Geocoordinates> _coordinateList;
33 /// Creates a polygon visual object
35 /// <param name="coordinates">list of geographical coordinates</param>
36 /// <param name="color">background color</param>
37 public Polygon(IEnumerable<Geocoordinates> coordinates, Color color) : base(CreateNativeHandle(coordinates, color))
39 var err = Interop.ErrorCode.InvalidParameter;
40 if (coordinates == null || coordinates.Count() < 3)
42 err.ThrowIfFailed("given coordinates list should contain at least 3 coordinates");
44 _coordinateList = coordinates.ToList();
47 internal Polygon(Interop.ViewObjectHandle nativeHandle) : base(nativeHandle)
52 /// List of geographical coordinates of polygon vertices
54 public IEnumerable<Geocoordinates> Coordinates
58 if (_coordinateList == null)
60 _coordinateList = new List<Geocoordinates>();
61 Interop.ViewObject.CoordinatesCallback callback = (index, nativeHandle, userData) =>
63 _coordinateList.Add(new Geocoordinates(nativeHandle));
66 Interop.ViewObject.PolygonForeachPoint(handle, callback, IntPtr.Zero);
68 return _coordinateList;
72 var coordinates = value.ToList();
74 var err = Interop.ErrorCode.InvalidParameter;
75 if (coordinates == null || coordinates.Count() < 3)
77 err.ThrowIfFailed("given coordinates list should contain at least 3 coordinates");
80 var coordinateList = new GeocoordinatesList(coordinates, false);
81 if (Interop.ViewObject.PolygonSetPolygon(handle, coordinateList.handle).IsSuccess())
83 _coordinateList = coordinates;
89 /// Background fill color
91 public Color FillColor
96 Interop.ViewObject.PolygonGetFillColor(handle, out r, out g, out b, out a);
97 return new Color(r, g, b, a);
101 Interop.ViewObject.PolygonSetFillColor(handle, (byte)value.R, (byte)value.G, (byte)value.B, (byte)value.A);
105 private static Interop.ViewObjectHandle CreateNativeHandle(IEnumerable<Geocoordinates> coordinates, Color color)
107 if (coordinates == null || coordinates.Count() < 3) return new Interop.ViewObjectHandle(IntPtr.Zero);
110 var geocoordinateList = new GeocoordinatesList(coordinates.ToList(), false);
111 var err = Interop.ViewObject.CreatePolygon(geocoordinateList.handle, (byte)color.R, (byte)color.G, (byte)color.B, (byte)color.A, out nativeHandle);
112 err.ThrowIfFailed("Failed to create native handle for polygon");
113 return new Interop.ViewObjectHandle(nativeHandle);