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 /// Geocode request for map service
25 /// <since_tizen>3</since_tizen>
26 public class GeocodeRequest : MapServiceRequest<Geocoordinates>
28 private Interop.GeocodeCallback _geocodeCallback;
29 private List<Geocoordinates> _coordinateList = new List<Geocoordinates>();
31 internal GeocodeRequest(MapService service, string address) : this(service, ServiceRequestType.Geocode)
33 startExecutionAction = new Action(() =>
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)
40 _requestTask?.TrySetException(errorCode.GetException(errMessage));
42 _requestID = requestID;
46 internal GeocodeRequest(MapService service, string address, Area boundry) : this(service, ServiceRequestType.GeocodeInsideArea)
48 startExecutionAction = new Action(() =>
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)
55 _requestTask?.TrySetException(errorCode.GetException(errMessage));
57 _requestID = requestID;
61 internal GeocodeRequest(MapService service, PlaceAddress address) : this(service, ServiceRequestType.GeocodeByStructuredAddress)
63 startExecutionAction = new Action(() =>
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)
70 _requestTask?.TrySetException(errorCode.GetException(errMessage));
72 _requestID = requestID;
76 private GeocodeRequest(MapService service, ServiceRequestType type) : base(service, type)
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) =>
82 if (result.IsSuccess())
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)
89 _requestTask?.TrySetResult(_coordinateList);
95 // If search is failed, the value of total is 0 and coordinates is NULL
96 _requestTask?.TrySetException(result.GetException(errMessage));