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;
19 using System.Threading.Tasks;
24 /// Map service class for service request.
26 /// <since_tizen> 3 </since_tizen>
27 public partial class MapService : IDisposable
29 internal Interop.ServiceHandle handle;
31 private PlaceFilter _filter;
32 private SearchPreference _searchPreference;
34 private static List<string> s_providers;
35 private string _serviceProvider;
39 /// Creates a new maps service object for given service provider.
41 /// <since_tizen> 3 </since_tizen>
42 /// <param name="serviceProvider">A string representing the name of the map service provider.</param>
43 /// <param name="serviceProviderKey">A string representing a certificate key to use the map service provider.</param>
44 /// <privilege>http://tizen.org/privilege/mapservice</privilege>
45 /// <privilege>http://tizen.org/privilege/network.get</privilege>
46 /// <exception cref="System.NotSupportedException">Thrown when the required feature is not supported.</exception>
47 /// <exception cref="System.ArgumentException">Thrown when parameters are invalid.</exception>
48 /// <exception cref="System.InvalidOperationException">Thrown when a native operation failed to allocate memory and connect to the service.</exception>
49 /// <exception cref="System.UnauthorizedAccessException">Thrown when application does not have some privilege to access this method.</exception>
50 public MapService(string serviceProvider, string serviceProviderKey)
52 _serviceProvider = serviceProvider;
53 handle = new Interop.ServiceHandle(serviceProvider);
54 ProviderKey = serviceProviderKey;
55 PlaceSearchFilter = new PlaceFilter();
56 Preferences = new SearchPreference();
60 /// Destroy the MapService object.
68 /// Gets the list of available map service providers.
70 /// <since_tizen> 3 </since_tizen>
71 /// <value>The list of map service providers.</value>
72 /// <privilege>http://tizen.org/privilege/mapservice</privilege>
73 /// <exception cref="System.NotSupportedException">Thrown when the required feature is not supported.</exception>
74 /// <exception cref="System.UnauthorizedAccessException">Thrown when application does not have privilege to access this property.</exception>
75 public static IEnumerable<string> Providers
79 if (s_providers != null) return s_providers;
81 s_providers = new List<string>();
82 Interop.ServiceHandle.ForeachProvider(provider => s_providers.Add(provider));
88 /// Gets the name of the map service provider.
90 /// <since_tizen> 3 </since_tizen>
91 public string Provider { get { return _serviceProvider; } }
94 /// Gets a user consent for the map service provider
96 /// <since_tizen> 4 </since_tizen>
97 /// <privilege>http://tizen.org/privilege/mapservice</privilege>
98 public bool UserConsented
102 return handle.UserConsented;
107 /// Gets and sets a string representing keys for the map service provider.
109 /// <since_tizen> 3 </since_tizen>
110 /// <remarks>Typically, the provider key is issued by each maps provider after signing up for a plan in the website.
111 /// Depending on the plan and its provider which you have signed, you might have to pay for the network traffic.</remarks>
112 public string ProviderKey
116 return handle.ProviderKey;
120 handle.ProviderKey = value;
125 /// Gets and sets a filter used for the place search result.
127 /// <since_tizen> 3 </since_tizen>
128 public PlaceFilter PlaceSearchFilter
144 /// Gets the search preferences used for <see cref="GeocodeRequest"/> or <see cref="ReverseGeocodeRequest"/>.
146 /// <since_tizen> 3 </since_tizen>
147 public IGeocodePreference GeocodePreferences
151 return Preferences as IGeocodePreference;
156 /// Gets the search preferences used for <see cref="PlaceSearchRequest"/>.
158 /// <since_tizen> 3 </since_tizen>
159 public IPlaceSearchPreference PlaceSearchPreferences
163 return Preferences as IPlaceSearchPreference;
168 /// Gets the search preferences used for <see cref="RouteSearchRequest"/>.
170 /// <since_tizen> 3 </since_tizen>
171 public IRouteSearchPreference RouteSearchPreferences
175 return Preferences as IRouteSearchPreference;
180 /// Gets and sets the search preferences.
182 /// <since_tizen> 3 </since_tizen>
183 public SearchPreference Preferences
187 if (_searchPreference == null)
189 _searchPreference = new SearchPreference(handle.Preferences);
191 return _searchPreference;
197 handle.Preferences = value.handle;
198 _searchPreference = value;
204 /// Gets the user's consent to use maps data.
206 /// <since_tizen> 4 </since_tizen>
207 /// <returns>Returns true if user agreed that the application can use maps data, otherwise false.</returns>
208 /// <privilege>http://tizen.org/privilege/mapservice</privilege>
209 /// <exception cref="System.NotSupportedException">Thrown when the required feature is not supported.</exception>
210 /// <exception cref="System.UnauthorizedAccessException">Thrown when application does not have some privilege to access this method.</exception>
211 public async Task<bool> RequestUserConsent()
213 TaskCompletionSource<bool> tcs = new TaskCompletionSource<bool>();
214 Interop.RequestUserConsentwithHandleCallback cb = (consented, userData) =>
216 tcs.TrySetResult(consented);
219 var err = handle.RequestUserConsent(cb, IntPtr.Zero);
222 tcs.TrySetException(err.GetException("Failed to get user consent"));
224 return await tcs.Task.ConfigureAwait(false);
228 /// Checks if the maps service supports the given request.
230 /// <since_tizen> 3 </since_tizen>
231 /// <param name="type">Request type to check</param>
232 /// <returns>Returns true if the maps service supports a request, otherwise false.</returns>
233 /// <privilege>http://tizen.org/privilege/mapservice</privilege>
234 /// <exception cref="System.NotSupportedException">Thrown when the required feature is not supported.</exception>
235 /// <exception cref="System.UnauthorizedAccessException">Thrown when application does not have some privilege to access this method.</exception>
236 public bool IsSupported(ServiceRequestType type)
239 var err = handle.IsServiceSupported((Interop.ServiceType)type, out result);
240 err.WarnIfFailed($"Failed to get if {type} is supported");
241 return (err.IsSuccess()) ? result : false;
245 /// Checks if the maps service supports a given data feature.
247 /// <since_tizen> 3 </since_tizen>
248 /// <param name="data">Data feature to check.</param>
249 /// <returns>Returns true if the maps service supports a data feature, otherwise false.</returns>
250 /// <privilege>http://tizen.org/privilege/mapservice</privilege>
251 /// <exception cref="System.NotSupportedException">Thrown when the required feature is not supported.</exception>
252 /// <exception cref="System.UnauthorizedAccessException">Thrown when application does not have some privilege to access this method.</exception>
253 public bool IsSupported(ServiceData data)
256 var err = handle.IsDataSupported((Interop.ServiceData)data, out result);
257 err.WarnIfFailed($"Failed to get if {data} data is supported");
258 return (err.IsSuccess()) ? result : false;
262 /// Creates a geocode search request for the given free-formed address string.
264 /// <since_tizen> 3 </since_tizen>
265 /// <param name="address">A string representing free-formed address.</param>
266 /// <returns>Returns a GeocodeRequest object created with an address string.</returns>
267 public GeocodeRequest CreateGeocodeRequest(string address)
269 return new GeocodeRequest(this, address);
273 /// Creates a geocode search request for the given free-formed address string, within the specified boundary.
275 /// <since_tizen> 3 </since_tizen>
276 /// <param name="address">A string representing the free-formed address.</param>
277 /// <param name="boundary">An instance of Area object representing the interested area.</param>
278 /// <seealso cref="Area"/>
279 /// <returns>Returns a GeocodeRequest object created with an address string and a specified boundary.</returns>
280 public GeocodeRequest CreateGeocodeRequest(string address, Area boundary)
282 return new GeocodeRequest(this, address, boundary);
286 /// Creates a geocode search request for the given structured address.
288 /// <since_tizen> 3 </since_tizen>
289 /// <param name="address">A string representing the address of interest.</param>
290 /// <returns>Returns a GeocodeRequest object created with a structured address.</returns>
291 public GeocodeRequest CreateGeocodeRequest(PlaceAddress address)
293 return new GeocodeRequest(this, address);
297 /// Creates a reverse geocode search request for the given latitude and longitude.
299 /// <since_tizen> 3 </since_tizen>
300 /// <param name="latitude">Latitude of the interested place.</param>
301 /// <param name="longitude">Longitude of the interested place.</param>
302 /// <returns>Returns a ReverseGeocodeRequest object created with the location coordinates.</returns>
303 public ReverseGeocodeRequest CreateReverseGeocodeRequest(double latitude, double longitude)
305 return new ReverseGeocodeRequest(this, latitude, longitude);
309 /// Creates a reverse geocode search request for the given position coordinates list.
311 /// <since_tizen> 3 </since_tizen>
312 /// <param name="coordinates">Coordinates list with [2 ~ 100] coordinates.</param>
313 /// <returns>Returns a MultiReverseGeocodeRequest object created with a list of location coordinates.</returns>
314 public MultiReverseGeocodeRequest CreateMultiReverseGeocodeRequest(IEnumerable<Geocoordinates> coordinates)
316 return new MultiReverseGeocodeRequest(this, coordinates);
320 /// Creates a route search request for the origin and destination points.
322 /// <since_tizen> 3 </since_tizen>
323 /// <param name="from">Starting point.</param>
324 /// <param name="to">Destination.</param>
325 /// <returns>Returns a RouteSearchRequest object created with the origin and destination coordinates.</returns>
326 public RouteSearchRequest CreateRouteSearchRequest(Geocoordinates from, Geocoordinates to)
328 return new RouteSearchRequest(this, from, to);
332 /// Creates a place search request for a specified search radius around a given coordinates position.
334 /// <since_tizen> 3 </since_tizen>
335 /// <param name="coordinates">Geographical coordinates of the center.</param>
336 /// <param name="distance">A double value representing the radius of an area to search places.</param>
337 /// <returns>Returns a PlaceSearchRequest object created with the location coordinates and search radius.</returns>
338 public PlaceSearchRequest CreatePlaceSearchRequest(Geocoordinates coordinates, int distance)
340 return new PlaceSearchRequest(this, coordinates, distance);
344 /// Creates a place search request for places within a specified boundary.
346 /// <since_tizen> 3 </since_tizen>
347 /// <param name="boundary">An instance of Area object representing and area to search interested places.</param>
348 /// <returns>Returns a PlaceSearchRequest object created with a specified boundary.</returns>
349 public PlaceSearchRequest CreatePlaceSearchRequest(Area boundary)
351 return new PlaceSearchRequest(this, boundary);
355 /// Creates a place search request for a free-formed address within the boundary.
357 /// <since_tizen> 3 </since_tizen>
358 /// <param name="address">A string which represents a free-formed address.</param>
359 /// <param name="boundary">An instance of area object representing an area to search interested places.</param>
360 /// <returns>Returns a PlaceSearchRequest object created with an address string and a specified boundary.</returns>
361 public PlaceSearchRequest CreatePlaceSearchRequest(string address, Area boundary)
363 return new PlaceSearchRequest(this, address, boundary);
366 #region IDisposable Support
367 private bool _disposedValue = false;
370 /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
372 /// <param name="disposing">If true, managed and unmanaged resources can be disposed, otherwise only unmanaged resources can be disposed.</param>
373 /// <since_tizen> 3 </since_tizen>
374 protected virtual void Dispose(bool disposing)
381 _searchPreference?.Dispose();
384 _disposedValue = true;
389 /// Releases all the resources used by this object.
391 /// <since_tizen> 3 </since_tizen>
392 public void Dispose()
395 GC.SuppressFinalize(this);