Tizen.Maps Setup
[platform/core/csapi/tizenfx.git] / src / Tizen.Maps / Tizen.Maps / PlaceSearchRequest.cs
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 using System;
18 using System.Collections.Generic;
19
20 namespace Tizen.Maps
21 {
22     /// <summary>
23     /// Place search request for Tizen map service
24     /// </summary>
25     public class PlaceSearchRequest : MapServiceRequest<Place>
26     {
27         private Interop.Service.SearchPlaceCallback _responseCallback;
28         private List<Place> _placeList = new List<Place>();
29         private PlaceFilter _filter;
30         private PlaceSearchPreference _preferences;
31
32         internal PlaceSearchRequest(MapService service, Geocoordinates position, int distance) : this(service, ServiceRequestType.SearchPlace)
33         {
34             startExecutionAction = new Action(() =>
35             {
36                 int requestID;
37                 errMessage = string.Format("Failed to get place list for given co-ordinate {0} and distance {1}", position, distance);
38                 errorCode = Interop.Service.SearchPlace(_service, position.handle, distance, _filter.handle, _preferences.handle, _responseCallback, IntPtr.Zero, out requestID);
39                 if (errorCode.IsFailed() && errorCode != Interop.ErrorCode.Canceled)
40                 {
41                     _requestTask?.TrySetException(errorCode.GetException(errMessage));
42                 }
43                 _requestID = requestID;
44             });
45         }
46
47         internal PlaceSearchRequest(MapService service, Area boundary) : this(service, ServiceRequestType.SearchPlaceByArea)
48         {
49             startExecutionAction = new Action(() =>
50             {
51                 int requestID;
52                 errMessage = string.Format("Failed to get place list for given boundary {0}", boundary);
53                 errorCode = Interop.Service.SearchPlaceByArea(_service, boundary.handle, _filter.handle, _preferences.handle, _responseCallback, IntPtr.Zero, out requestID);
54                 if (errorCode.IsFailed() && errorCode != Interop.ErrorCode.Canceled)
55                 {
56                     _requestTask?.TrySetException(errorCode.GetException(errMessage));
57                 }
58                 _requestID = requestID;
59             });
60         }
61
62         internal PlaceSearchRequest(MapService service, string address, Area boundary) : this(service, ServiceRequestType.SearchPlaceByAddress)
63         {
64             startExecutionAction = new Action(() =>
65             {
66                 int requestID;
67                 errMessage = string.Format("Failed to get co-ordinates for given address {0} and boundary {1}", address, boundary);
68                 errorCode = Interop.Service.SearchPlaceByAddress(_service, address, boundary.handle, _filter.handle, _preferences.handle, _responseCallback, IntPtr.Zero, out requestID);
69                 if (errorCode.IsFailed() && errorCode != Interop.ErrorCode.Canceled)
70                 {
71                     _requestTask?.TrySetException(errorCode.GetException(errMessage));
72                 }
73                 _requestID = requestID;
74             });
75         }
76
77         private PlaceSearchRequest(MapService service, ServiceRequestType type) : base(service, type)
78         {
79             _filter = service._filter;
80             _preferences = service.PlaceSearchPreferences;
81             _responseCallback = (result, id, index, total, placeHandle, userData) =>
82             {
83                 errorCode = result;
84                 if (result.IsSuccess())
85                 {
86                     _placeList.Add(new Place(placeHandle));
87                     if (_placeList.Count == total)
88                     {
89                         _requestTask?.TrySetResult(_placeList);
90                     }
91                     return true;
92                 }
93                 else
94                 {
95                     _requestTask?.TrySetException(errorCode.GetException(errMessage));
96                     return false;
97                 }
98             };
99         }
100     }
101 }