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 public partial class MapService : IDisposable
28 internal Interop.ServiceHandle handle;
30 private PlaceFilter _filter;
31 private SearchPreference _searchPreference;
33 private static List<string> s_providers;
34 private string _serviceProvider;
38 /// Creates a new Maps Service object for given service provider.
40 /// <param name="serviceProvider">A string which representing name of map service provider</param>
41 /// <param name="serviceProviderKey">A string which representing certificate key to use the map service provider</param>
42 /// <privilege>http://tizen.org/privilege/mapservice</privilege>
43 /// <privilege>http://tizen.org/privilege/network.get</privilege>
44 /// <exception cref="System.NotSupportedException">Thrown when the required feature is not supported.</exception>
45 /// <exception cref="System.ArgumentException">Thrown when parameters are invalid.</exception>
46 /// <exception cref="System.InvalidOperationException">Thrown when native operation failed to allocate memory, connect to service.</exception>
47 /// <exception cref="System.UnauthorizedAccessException">Thrown when application does not have some privilege to access this method.</exception>
48 public MapService(string serviceProvider, string serviceProviderKey)
50 _serviceProvider = serviceProvider;
51 handle = new Interop.ServiceHandle(serviceProvider);
52 ProviderKey = serviceProviderKey;
53 PlaceSearchFilter = new PlaceFilter();
54 Preferences = new SearchPreference();
58 /// Gets list of available map service providers.
60 /// <value>The list of map service providers.</value>
61 /// <privilege>http://tizen.org/privilege/mapservice</privilege>
62 /// <exception cref="System.NotSupportedException">Thrown when the required feature is not supported.</exception>
63 /// <exception cref="System.UnauthorizedAccessException">Thrown when application does not have privileges to access this property.</exception>
64 public static IEnumerable<string> Providers
68 if (s_providers != null) return s_providers;
70 s_providers = new List<string>();
71 Interop.ServiceHandle.ForeachProvider(provider => s_providers.Add(provider));
77 /// Gets name of map service provider.
79 public string Provider { get { return _serviceProvider; } }
83 /// Gets and sets a string representing keys for map service provider
85 /// <remark>Regaularly, the provider key is issued by each maps provider, after signing up for a plan in the web site.
86 /// Depending on the plan and its provider which you have signed, you might pay for the network traffic.</remark>
87 public string ProviderKey
91 return handle.ProviderKey;
95 handle.ProviderKey = value;
100 /// Gets and sets a filter used for place search result.
102 public PlaceFilter PlaceSearchFilter
118 /// Gets search preferences used for <see cref="Geocode"/> or <see cref="ReverseGeocode"/> request.
120 public IGeocodePreference GeocodePreferences
124 return Preferences as IGeocodePreference;
129 /// Gets search preferences used for <see cref="Place"/> search request.
131 public IPlaceSearchPreference PlaceSearchPreferences
135 return Preferences as IPlaceSearchPreference;
140 /// Gets search preferences used for <see cref="Route"/> search request.
142 public IRouteSearchPreference RouteSearchPreferences
146 return Preferences as IRouteSearchPreference;
151 /// Gets and sets search preferences.
153 public SearchPreference Preferences
157 if (_searchPreference == null)
159 _searchPreference = new SearchPreference(handle.Preferences);
161 return _searchPreference;
167 handle.Preferences = value.handle;
168 _searchPreference = value;
174 /// Gets the user's consent to use maps data.
176 /// <param name="provider">A string which representing the name of maps provider</param>
177 /// <returns>Returns true if user agreed that the application can use maps data, otherwise false.</returns>
178 /// <privilege>http://tizen.org/privilege/mapservice</privilege>
179 /// <exception cref="System.NotSupportedException">Thrown when the required feature is not supported.</exception>
180 /// <exception cref="System.UnauthorizedAccessException">Thrown when application does not have some privilege to access this method.</exception>
181 public static async Task<bool> RequestUserConsent(string provider)
183 TaskCompletionSource<bool> tcs = new TaskCompletionSource<bool>();
184 Interop.ServiceHandle.RequestUserConsentCallback cb = (consented, serviceProvider, userData) =>
186 tcs.TrySetResult(consented);
189 var err = Interop.ServiceHandle.RequestUserConsent(provider, cb, IntPtr.Zero);
192 tcs.TrySetException(err.GetException("Failed to get user consent"));
194 return await tcs.Task.ConfigureAwait(false);
198 /// Checks if the Maps Service supports given request.
200 /// <param name="type">Request type to check</param>
201 /// <returns>Returns true if the Maps Service supports a request, otherwise false.</returns>
202 /// <privilege>http://tizen.org/privilege/mapservice</privilege>
203 /// <exception cref="System.NotSupportedException">Thrown when the required feature is not supported.</exception>
204 /// <exception cref="System.UnauthorizedAccessException">Thrown when application does not have some privilege to access this method.</exception>
205 public bool IsSupported(ServiceRequestType type)
208 var err = handle.IsServiceSupported((Interop.ServiceType)type, out result);
209 err.WarnIfFailed($"Failed to get if {type} is supported");
210 return (err.IsSuccess()) ? result : false;
214 /// Checks if the Maps Service supports given data feature.
216 /// <param name="data">Data feature to check</param>
217 /// <returns>Returns true if the Maps Service supports a data feature, otherwise false.</returns>
218 /// <privilege>http://tizen.org/privilege/mapservice</privilege>
219 /// <exception cref="System.NotSupportedException">Thrown when the required feature is not supported.</exception>
220 /// <exception cref="System.UnauthorizedAccessException">Thrown when application does not have some privilege to access this method.</exception>
221 public bool IsSupported(ServiceData data)
224 var err = handle.IsDataSupported((Interop.ServiceData)data, out result);
225 err.WarnIfFailed($"Failed to get if {data} data is supported");
226 return (err.IsSuccess()) ? result : false;
230 /// Creates geocode search request for given free-formed address string.
232 /// <param name="address">A string which representing free-formed address</param>
233 /// <returns>GeocodeRequest object created with address string</returns>
234 public GeocodeRequest CreateGeocodeRequest(string address)
236 return new GeocodeRequest(this, address);
240 /// Creates geocode search request for given free-formed address string, within the specified boundary.
242 /// <param name="address">A string which representing free-formed address</param>
243 /// <param name="boundary">An instance of Area object which representing interested area</param>
244 /// <seealso cref="Area">
245 /// <returns>GeocodeRequest object created with address string and specified boundary</returns>
246 public GeocodeRequest CreateGeocodeRequest(string address, Area boundary)
248 return new GeocodeRequest(this, address, boundary);
252 /// Creates geocode search request for given structured address.
254 /// <param name="address">A string which representing address of interest</param>
255 /// <returns>Returns GeocodeRequest object created with structured address</returns>
256 public GeocodeRequest CreateGeocodeRequest(PlaceAddress address)
258 return new GeocodeRequest(this, address);
262 /// Creates a reverse geocode search request for given latitude and longitude.
264 /// <param name="latitude">Latitude of interested place</param>
265 /// <param name="longitute">Longitude of interested place</param>
266 /// <returns>Returns ReverseGeocodeRequest object created with location coordinates</returns>
267 public ReverseGeocodeRequest CreateReverseGeocodeRequest(double latitude, double longitute)
269 return new ReverseGeocodeRequest(this, latitude, longitute);
273 /// Creates a reverse geocode search request for given position coordinates list.
275 /// <param name="coordinates">Coordinates list with [2 ~ 100] coordinates</param>
276 /// <returns>Returns MultiReverseGeocodeRequest object created with list of location coordinates</returns>
277 public MultiReverseGeocodeRequest CreateMultiReverseGeocodeRequest(IEnumerable<Geocoordinates> coordinates)
279 return new MultiReverseGeocodeRequest(this, coordinates);
283 /// Creates a route search request for origin and destination points.
285 /// <param name="from">Starting point</param>
286 /// <param name="to">Destination</param>
287 /// <returns>Returns RouteSearchRequest object created with origin and destination coordinates</returns>
288 public RouteSearchRequest CreateRouteSearchRequest(Geocoordinates from, Geocoordinates to)
290 return new RouteSearchRequest(this, from, to);
294 /// Creates a place search request for specified search radius around a given coordinates position.
296 /// <param name="coordinates">A geographical coordinates of center</param>
297 /// <param name="distance">A double value which representing radius of area to search places</param>
298 /// <returns>Returns PlaceSearchRequest object created with location coordinates and search radius</returns>
299 public PlaceSearchRequest CreatePlaceSearchRequest(Geocoordinates coordinates, int distance)
301 return new PlaceSearchRequest(this, coordinates, distance);
305 /// Creates a place search request for places within specified boundary.
307 /// <param name="boundary">An instance of Area object which representing area to search interested places</param>
308 /// <returns>Returns PlaceSearchRequest object created with specified boundary</returns>
309 public PlaceSearchRequest CreatePlaceSearchRequest(Area boundary)
311 return new PlaceSearchRequest(this, boundary);
315 /// Creates a place search request for free-formed address within boundary.
317 /// <param name="address">A string which representing free-formed address</param>
318 /// <param name="boundary">An instance of Area object which representing area to search interested places</param>
319 /// <returns>Returns PlaceSearchRequest object created with address string and specified boundary</returns>
320 public PlaceSearchRequest CreatePlaceSearchRequest(string address, Area boundary)
322 return new PlaceSearchRequest(this, address, boundary);
325 #region IDisposable Support
326 private bool _disposedValue = false;
327 protected virtual void Dispose(bool disposing)
334 _searchPreference.Dispose();
337 _disposedValue = true;
342 /// Releases all resources used by this object.
344 public void Dispose()
347 GC.SuppressFinalize(this);