Release 4.0.0-preview1-00051
[platform/core/csapi/tizenfx.git] / src / Tizen.Maps / Tizen.Maps / GeocodeRequest.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     /// Geocode request for map service
24     /// </summary>
25     /// <since_tizen>3</since_tizen>
26     public class GeocodeRequest : MapServiceRequest<Geocoordinates>
27     {
28         private Interop.GeocodeCallback _geocodeCallback;
29         private List<Geocoordinates> _coordinateList = new List<Geocoordinates>();
30
31         internal GeocodeRequest(MapService service, string address) : this(service, ServiceRequestType.Geocode)
32         {
33             startExecutionAction = new Action(() =>
34             {
35                 int requestID;
36                 errMessage = $"Failed to get coordinates for given address {address}";
37                 errorCode = _service.handle.Geocode(address, _service.Preferences.handle, _geocodeCallback, 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 GeocodeRequest(MapService service, string address, Area boundry) : this(service, ServiceRequestType.GeocodeInsideArea)
47         {
48             startExecutionAction = new Action(() =>
49             {
50                 int requestID;
51                 errMessage = $"Failed to get coordinates for given address {address}";
52                 errorCode = _service.handle.GeocodeInsideArea(address, boundry.handle, _service.Preferences.handle, _geocodeCallback, 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 GeocodeRequest(MapService service, PlaceAddress address) : this(service, ServiceRequestType.GeocodeByStructuredAddress)
62         {
63             startExecutionAction = new Action(() =>
64             {
65                 int requestID;
66                 errMessage = $"Failed to get coordinates for given address {address}";
67                 errorCode = _service.handle.GeocodeByStructuredAddress(address.handle, _service.Preferences.handle, _geocodeCallback, 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 GeocodeRequest(MapService service, ServiceRequestType type) : base(service, type)
77         {
78             // The Maps Service invokes this callback while iterating through the list of obtained coordinates of the specified place.
79             _geocodeCallback = (result, id, index, total, coordinates, userData) =>
80             {
81                 errorCode = result;
82                 if (result.IsSuccess())
83                 {
84                     // The parameter coordinates must be released using maps_coordinates_destroy()
85                     var coordinatesHandle = new Interop.CoordinatesHandle(coordinates, needToRelease: true);
86                     _coordinateList.Add(new Geocoordinates(coordinatesHandle));
87                     if (_coordinateList.Count == total)
88                     {
89                         _requestTask?.TrySetResult(_coordinateList);
90                     }
91                     return true;
92                 }
93                 else
94                 {
95                     // If search is failed, the value of total is 0 and coordinates is NULL
96                     _requestTask?.TrySetException(result.GetException(errMessage));
97                     return false;
98                 }
99             };
100         }
101     }
102 }