Release 4.0.0-preview1-00051
[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     /// <since_tizen>3</since_tizen>
26     public class PlaceSearchRequest : MapServiceRequest<Place>
27     {
28         private Interop.SearchPlaceCallback _placeCallback;
29         private List<Place> _placeList = new List<Place>();
30
31         internal PlaceSearchRequest(MapService service, Geocoordinates position, int distance) : this(service, ServiceRequestType.SearchPlace)
32         {
33             startExecutionAction = new Action(() =>
34             {
35                 int requestID;
36                 errMessage = $"Failed to get place list for given co-ordinate {position} and distance {distance}";
37                 errorCode = _service.handle.SearchPlace(position.handle, distance, _service.PlaceSearchFilter.handle, _service.Preferences.handle, _placeCallback, IntPtr.Zero, out requestID);
38                 if (errorCode.IsFailed() && errorCode != Interop.ErrorCode.Canceled)
39                 {
40                     _requestTask?.TrySetException(errorCode.GetException(errMessage));
41                 }
42                 _requestID = requestID;
43             });
44         }
45
46         internal PlaceSearchRequest(MapService service, Area boundary) : this(service, ServiceRequestType.SearchPlaceByArea)
47         {
48             startExecutionAction = new Action(() =>
49             {
50                 int requestID;
51                 errMessage = $"Failed to get place list for given boundary";
52                 errorCode = _service.handle.SearchPlaceByArea(boundary.handle, _service.PlaceSearchFilter.handle, _service.Preferences.handle, _placeCallback, IntPtr.Zero, out requestID);
53                 if (errorCode.IsFailed() && errorCode != Interop.ErrorCode.Canceled)
54                 {
55                     _requestTask?.TrySetException(errorCode.GetException(errMessage));
56                 }
57                 _requestID = requestID;
58             });
59         }
60
61         internal PlaceSearchRequest(MapService service, string address, Area boundary) : this(service, ServiceRequestType.SearchPlaceByAddress)
62         {
63             startExecutionAction = new Action(() =>
64             {
65                 int requestID;
66                 errMessage = $"Failed to get coordinates for address {address} in given boundary";
67                 errorCode = _service.handle.SearchPlaceByAddress(address, boundary.handle, _service.PlaceSearchFilter.handle, _service.Preferences.handle, _placeCallback, IntPtr.Zero, out requestID);
68                 if (errorCode.IsFailed() && errorCode != Interop.ErrorCode.Canceled)
69                 {
70                     _requestTask?.TrySetException(errorCode.GetException(errMessage));
71                 }
72                 _requestID = requestID;
73             });
74         }
75
76         private PlaceSearchRequest(MapService service, ServiceRequestType type) : base(service, type)
77         {
78             // The Maps Service invokes this callback while iterating through the set of obtained Place data.
79             _placeCallback = (result, id, index, total, place, userData) =>
80             {
81                 errorCode = result;
82                 if (result.IsSuccess())
83                 {
84                     // The parameter place must be released using maps_place_destroy().
85                     var placeHandle = new Interop.PlaceHandle(place, needToRelease: true);
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                     // If search is failed, the value of total is 0 and place is NULL
96                     _requestTask?.TrySetException(errorCode.GetException(errMessage));
97                     return false;
98                 }
99             };
100         }
101     }
102 }