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;
23 /// Place search request for Tizen map service.
25 /// <since_tizen> 3 </since_tizen>
26 public class PlaceSearchRequest : MapServiceRequest<Place>
28 private Interop.SearchPlaceCallback _placeCallback;
29 private List<Place> _placeList = new List<Place>();
31 internal PlaceSearchRequest(MapService service, Geocoordinates position, int distance) : this(service, ServiceRequestType.SearchPlace)
33 startExecutionAction = new Action(() =>
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)
40 _requestTask?.TrySetException(errorCode.GetException(errMessage));
42 _requestID = requestID;
46 internal PlaceSearchRequest(MapService service, Area boundary) : this(service, ServiceRequestType.SearchPlaceByArea)
48 startExecutionAction = new Action(() =>
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)
55 _requestTask?.TrySetException(errorCode.GetException(errMessage));
57 _requestID = requestID;
61 internal PlaceSearchRequest(MapService service, string address, Area boundary) : this(service, ServiceRequestType.SearchPlaceByAddress)
63 startExecutionAction = new Action(() =>
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)
70 _requestTask?.TrySetException(errorCode.GetException(errMessage));
72 _requestID = requestID;
76 private PlaceSearchRequest(MapService service, ServiceRequestType type) : base(service, type)
78 // The Maps Service invokes this callback while iterating through the set of obtained Place data.
79 _placeCallback = (result, id, index, total, place, userData) =>
82 if (result.IsSuccess())
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)
89 _requestTask?.TrySetResult(_placeList);
95 // If search is failed, the value of total is 0 and place is NULL
96 _requestTask?.TrySetException(errorCode.GetException(errMessage));