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 /// Gets the list of available map service providers.
62 /// <since_tizen> 3 </since_tizen>
63 /// <value>The list of map service providers.</value>
64 /// <privilege>http://tizen.org/privilege/mapservice</privilege>
65 /// <exception cref="System.NotSupportedException">Thrown when the required feature is not supported.</exception>
66 /// <exception cref="System.UnauthorizedAccessException">Thrown when application does not have privilege to access this property.</exception>
67 public static IEnumerable<string> Providers
71 if (s_providers != null) return s_providers;
73 s_providers = new List<string>();
74 Interop.ServiceHandle.ForeachProvider(provider => s_providers.Add(provider));
80 /// Gets the name of the map service provider.
82 /// <since_tizen> 3 </since_tizen>
83 public string Provider { get { return _serviceProvider; } }
86 /// Gets a user consent for the map service provider
88 /// <since_tizen> 3 </since_tizen>
89 /// <privilege>http://tizen.org/privilege/mapservice</privilege>
90 public bool UserConsented
94 return handle.UserConsented;
99 /// Gets and sets a string representing keys for the map service provider.
101 /// <since_tizen> 3 </since_tizen>
102 /// <remarks>Typically, the provider key is issued by each maps provider after signing up for a plan in the website.
103 /// Depending on the plan and its provider which you have signed, you might have to pay for the network traffic.</remarks>
104 public string ProviderKey
108 return handle.ProviderKey;
112 handle.ProviderKey = value;
117 /// Gets and sets a filter used for the place search result.
119 /// <since_tizen> 3 </since_tizen>
120 public PlaceFilter PlaceSearchFilter
136 /// Gets the search preferences used for <see cref="GeocodeRequest"/> or <see cref="ReverseGeocodeRequest"/>.
138 /// <since_tizen> 3 </since_tizen>
139 public IGeocodePreference GeocodePreferences
143 return Preferences as IGeocodePreference;
148 /// Gets the search preferences used for <see cref="PlaceSearchRequest"/>.
150 /// <since_tizen> 3 </since_tizen>
151 public IPlaceSearchPreference PlaceSearchPreferences
155 return Preferences as IPlaceSearchPreference;
160 /// Gets the search preferences used for <see cref="RouteSearchRequest"/>.
162 /// <since_tizen> 3 </since_tizen>
163 public IRouteSearchPreference RouteSearchPreferences
167 return Preferences as IRouteSearchPreference;
172 /// Gets and sets the search preferences.
174 /// <since_tizen> 3 </since_tizen>
175 public SearchPreference Preferences
179 if (_searchPreference == null)
181 _searchPreference = new SearchPreference(handle.Preferences);
183 return _searchPreference;
189 handle.Preferences = value.handle;
190 _searchPreference = value;
196 /// Gets the user's consent to use maps data.
198 /// <since_tizen> 3 </since_tizen>
199 /// <returns>Returns true if user agreed that the application can use maps data, otherwise false.</returns>
200 /// <privilege>http://tizen.org/privilege/mapservice</privilege>
201 /// <exception cref="System.NotSupportedException">Thrown when the required feature is not supported.</exception>
202 /// <exception cref="System.UnauthorizedAccessException">Thrown when application does not have some privilege to access this method.</exception>
203 public async Task<bool> RequestUserConsent()
205 TaskCompletionSource<bool> tcs = new TaskCompletionSource<bool>();
206 Interop.RequestUserConsentwithHandleCallback cb = (consented, userData) =>
208 tcs.TrySetResult(consented);
211 var err = handle.RequestUserConsent(cb, IntPtr.Zero);
214 tcs.TrySetException(err.GetException("Failed to get user consent"));
216 return await tcs.Task.ConfigureAwait(false);
220 /// Checks if the maps service supports the given request.
222 /// <since_tizen> 3 </since_tizen>
223 /// <param name="type">Request type to check</param>
224 /// <returns>Returns true if the maps service supports a request, otherwise false.</returns>
225 /// <privilege>http://tizen.org/privilege/mapservice</privilege>
226 /// <exception cref="System.NotSupportedException">Thrown when the required feature is not supported.</exception>
227 /// <exception cref="System.UnauthorizedAccessException">Thrown when application does not have some privilege to access this method.</exception>
228 public bool IsSupported(ServiceRequestType type)
231 var err = handle.IsServiceSupported((Interop.ServiceType)type, out result);
232 err.WarnIfFailed($"Failed to get if {type} is supported");
233 return (err.IsSuccess()) ? result : false;
237 /// Checks if the maps service supports a given data feature.
239 /// <since_tizen> 3 </since_tizen>
240 /// <param name="data">Data feature to check.</param>
241 /// <returns>Returns true if the maps service supports a data feature, otherwise false.</returns>
242 /// <privilege>http://tizen.org/privilege/mapservice</privilege>
243 /// <exception cref="System.NotSupportedException">Thrown when the required feature is not supported.</exception>
244 /// <exception cref="System.UnauthorizedAccessException">Thrown when application does not have some privilege to access this method.</exception>
245 public bool IsSupported(ServiceData data)
248 var err = handle.IsDataSupported((Interop.ServiceData)data, out result);
249 err.WarnIfFailed($"Failed to get if {data} data is supported");
250 return (err.IsSuccess()) ? result : false;
254 /// Creates a geocode search request for the given free-formed address string.
256 /// <since_tizen> 3 </since_tizen>
257 /// <param name="address">A string representing free-formed address.</param>
258 /// <returns>Returns a GeocodeRequest object created with an address string.</returns>
259 public GeocodeRequest CreateGeocodeRequest(string address)
261 return new GeocodeRequest(this, address);
265 /// Creates a geocode search request for the given free-formed address string, within the specified boundary.
267 /// <since_tizen> 3 </since_tizen>
268 /// <param name="address">A string representing the free-formed address.</param>
269 /// <param name="boundary">An instance of Area object representing the interested area.</param>
270 /// <seealso cref="Area"/>
271 /// <returns>Returns a GeocodeRequest object created with an address string and a specified boundary.</returns>
272 public GeocodeRequest CreateGeocodeRequest(string address, Area boundary)
274 return new GeocodeRequest(this, address, boundary);
278 /// Creates a geocode search request for the given structured address.
280 /// <since_tizen> 3 </since_tizen>
281 /// <param name="address">A string representing the address of interest.</param>
282 /// <returns>Returns a GeocodeRequest object created with a structured address.</returns>
283 public GeocodeRequest CreateGeocodeRequest(PlaceAddress address)
285 return new GeocodeRequest(this, address);
289 /// Creates a reverse geocode search request for the given latitude and longitude.
291 /// <since_tizen> 3 </since_tizen>
292 /// <param name="latitude">Latitude of the interested place.</param>
293 /// <param name="longitude">Longitude of the interested place.</param>
294 /// <returns>Returns a ReverseGeocodeRequest object created with the location coordinates.</returns>
295 public ReverseGeocodeRequest CreateReverseGeocodeRequest(double latitude, double longitude)
297 return new ReverseGeocodeRequest(this, latitude, longitude);
301 /// Creates a reverse geocode search request for the given position coordinates list.
303 /// <since_tizen> 3 </since_tizen>
304 /// <param name="coordinates">Coordinates list with [2 ~ 100] coordinates.</param>
305 /// <returns>Returns a MultiReverseGeocodeRequest object created with a list of location coordinates.</returns>
306 public MultiReverseGeocodeRequest CreateMultiReverseGeocodeRequest(IEnumerable<Geocoordinates> coordinates)
308 return new MultiReverseGeocodeRequest(this, coordinates);
312 /// Creates a route search request for the origin and destination points.
314 /// <since_tizen> 3 </since_tizen>
315 /// <param name="from">Starting point.</param>
316 /// <param name="to">Destination.</param>
317 /// <returns>Returns a RouteSearchRequest object created with the origin and destination coordinates.</returns>
318 public RouteSearchRequest CreateRouteSearchRequest(Geocoordinates from, Geocoordinates to)
320 return new RouteSearchRequest(this, from, to);
324 /// Creates a place search request for a specified search radius around a given coordinates position.
326 /// <since_tizen> 3 </since_tizen>
327 /// <param name="coordinates">Geographical coordinates of the center.</param>
328 /// <param name="distance">A double value representing the radius of an area to search places.</param>
329 /// <returns>Returns a PlaceSearchRequest object created with the location coordinates and search radius.</returns>
330 public PlaceSearchRequest CreatePlaceSearchRequest(Geocoordinates coordinates, int distance)
332 return new PlaceSearchRequest(this, coordinates, distance);
336 /// Creates a place search request for places within a specified boundary.
338 /// <since_tizen> 3 </since_tizen>
339 /// <param name="boundary">An instance of Area object representing and area to search interested places.</param>
340 /// <returns>Returns a PlaceSearchRequest object created with a specified boundary.</returns>
341 public PlaceSearchRequest CreatePlaceSearchRequest(Area boundary)
343 return new PlaceSearchRequest(this, boundary);
347 /// Creates a place search request for a free-formed address within the boundary.
349 /// <since_tizen> 3 </since_tizen>
350 /// <param name="address">A string which represents a free-formed address.</param>
351 /// <param name="boundary">An instance of area object representing an area to search interested places.</param>
352 /// <returns>Returns a PlaceSearchRequest object created with an address string and a specified boundary.</returns>
353 public PlaceSearchRequest CreatePlaceSearchRequest(string address, Area boundary)
355 return new PlaceSearchRequest(this, address, boundary);
358 #region IDisposable Support
359 private bool _disposedValue = false;
360 protected virtual void Dispose(bool disposing)
367 _searchPreference.Dispose();
370 _disposedValue = true;
375 /// Releases all the resources used by this object.
377 /// <since_tizen> 3 </since_tizen>
378 public void Dispose()
381 GC.SuppressFinalize(this);