fb73077e6fff8bf12bab0d05d7cba2d8e7f6a2f9
[platform/core/location/maps-plugin-here.git] / src / here_multirevgeocode.cpp
1 /*
2  * Copyright (c) 2014 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 #include "here_multirevgeocode.h"
18
19 HERE_PLUGIN_BEGIN_NAMESPACE
20
21 HereMultiRevGeocode::HereMultiRevGeocode(void* pCbFunc, void* pUserData, int nReqId)
22 : m_geoCoord(0,0,0)
23 {
24         m_pQuery = NULL;
25         m_pCbFunc = pCbFunc;
26         m_pUserData = pUserData;
27         m_nReqId = nReqId;
28 }
29
30 HereMultiRevGeocode::~HereMultiRevGeocode()
31 {
32         if (m_pQuery)
33         {
34                 delete m_pQuery;
35                 m_pQuery = NULL;
36         }
37 }
38
39 here_error_e HereMultiRevGeocode::PrepareQuery()
40 {
41         if (m_pQuery)
42                 return HERE_ERROR_PERMISSION_DENIED;
43
44         m_pQuery = new (std::nothrow) MultiReverseQuery();
45
46         if (!m_pQuery)
47                 return HERE_ERROR_OUT_OF_MEMORY;
48         else
49                 return HERE_ERROR_NONE;
50 }
51
52 static bool geocoder_coordinates_cb(int index, maps_coordinates_h coordinates, void *user_data)
53 {
54         double dLatitude = 0.0;
55         double dLongitude = 0.0;
56         GeoCoordinates hereCoord;
57         maps_coordinates_get_latitude(coordinates, &dLatitude);
58         maps_coordinates_get_longitude(coordinates, &dLongitude);
59         hereCoord = GeoCoordinates(dLatitude, dLongitude);
60
61         if (!HereUtils::IsValid(hereCoord))
62                 return HERE_ERROR_INVALID_PARAMETER;
63
64         GeoCoordinateList *pList =(GeoCoordinateList *) user_data;
65         pList->push_back(hereCoord);
66
67         return true;
68 }
69
70 here_error_e HereMultiRevGeocode::PreparePositionList(const maps_coordinates_list_h hGeocodeList)
71 {
72         if (!m_pQuery)
73                 return HERE_ERROR_OUT_OF_MEMORY;
74
75         if (!hGeocodeList)
76                 return HERE_ERROR_INVALID_PARAMETER;
77
78         GeoCoordinateList hereCoordList;
79         maps_coordinates_list_foreach(hGeocodeList, geocoder_coordinates_cb, &hereCoordList);
80
81         m_pQuery->SetMode(MultiReverseQuery::RM_RetrieveAddresses);
82         m_pQuery->SetGeocodeList(hereCoordList);
83         m_pQuery->SetMaxResults(1);
84         m_pQuery->SetGenParameter(8);
85
86         return HERE_ERROR_NONE;
87 }
88
89 here_error_e HereMultiRevGeocode::StartMultiReverse(maps_item_hashtable_h hPref)
90 {
91         if (!m_pQuery)
92                 return HERE_ERROR_OUT_OF_MEMORY;
93
94         m_nRestReqId = m_pQuery->Execute(*this, NULL);
95
96         return (m_nRestReqId > 0 ? HERE_ERROR_NONE : HERE_ERROR_CANCELED);
97 }
98
99 void HereMultiRevGeocode::OnMultiReverseReply(const MultiReverseReply& Reply)
100 {
101         if (m_bCanceled || !m_pCbFunc) // ignore call back
102         {
103                 delete this;
104                 return;
105         }
106
107         int nResults = Reply.GetNumResults();
108         Result* pResult;
109
110         maps_address_list_h address_list;
111         maps_address_list_create(&address_list);
112         maps_address_h pAddr = NULL;
113         String *additionalDataValue = NULL;
114
115         for (size_t i = 0; i < (size_t)nResults; i++)
116         {
117                 maps_error_e error = (maps_error_e)maps_address_create(&pAddr);
118
119                 if(error == MAPS_ERROR_NONE)
120                 {
121                         pResult = (Result*)Reply.GetResult(i);
122
123                         if (pResult)
124                         {
125                                 Address tmpAddr = (pResult->GetLocation()).GetAddress();
126
127                                 if(!tmpAddr.GetHouseNumber().empty())
128                                         maps_address_set_building_number(pAddr, tmpAddr.GetHouseNumber().c_str());
129
130                                 if(!tmpAddr.GetStreet().empty())
131                                         maps_address_set_street(pAddr, tmpAddr.GetStreet().c_str());
132
133                                 if(!tmpAddr.GetDistrict().empty())
134                                         maps_address_set_district(pAddr, tmpAddr.GetDistrict().c_str());
135
136                                 if(!tmpAddr.GetCity().empty())
137                                         maps_address_set_city(pAddr, tmpAddr.GetCity().c_str());
138
139                                 additionalDataValue = (String*)tmpAddr.GetAdditionalDataValue("CountyName");
140                                 if (additionalDataValue && !additionalDataValue->empty())
141                                         maps_address_set_county(pAddr, additionalDataValue->c_str());
142                                 else if (!tmpAddr.GetCounty().empty())
143                                         maps_address_set_county(pAddr, tmpAddr.GetCounty().c_str());
144
145                                 additionalDataValue = (String*)tmpAddr.GetAdditionalDataValue("StateName");
146                                 if (additionalDataValue && !additionalDataValue->empty())
147                                         maps_address_set_state(pAddr, additionalDataValue->c_str());
148                                 else if (!tmpAddr.GetState().empty())
149                                         maps_address_set_state(pAddr, tmpAddr.GetState().c_str());
150
151                                 additionalDataValue = (String*)tmpAddr.GetAdditionalDataValue("CountryName");
152                                 if (additionalDataValue && !additionalDataValue->empty())
153                                         maps_address_set_country(pAddr, additionalDataValue->c_str());
154                                 else if (!tmpAddr.GetCountry().empty())
155                                         maps_address_set_country(pAddr, tmpAddr.GetCountry().c_str());
156
157                                 if(!tmpAddr.GetCountry().empty())
158                                         maps_address_set_country_code(pAddr, tmpAddr.GetCountry().c_str());
159
160                                 if(!tmpAddr.GetPostalCode().empty())
161                                         maps_address_set_postal_code(pAddr, tmpAddr.GetPostalCode().c_str());
162
163                                 if(!tmpAddr.GetLabel().empty())
164                                         maps_address_set_freetext(pAddr, tmpAddr.GetLabel().c_str());
165                         }
166                 }
167                 maps_address_list_append(address_list, pAddr);
168         }
169
170         if (m_bCanceled || !m_pCbFunc)
171         {
172                 maps_address_list_destroy(address_list);
173         }
174         else if (nResults <= 0)
175         {
176                 ((maps_service_multi_reverse_geocode_cb)m_pCbFunc)(MAPS_ERROR_NOT_FOUND, m_nReqId, 0, NULL, m_pUserData);
177         }
178         else
179         {
180                 ((maps_service_multi_reverse_geocode_cb)m_pCbFunc)(MAPS_ERROR_NONE, m_nReqId, nResults, address_list, m_pUserData);
181         }
182
183         delete this;
184 }
185
186 void HereMultiRevGeocode::OnMultiReverseFailure(const MultiReverseReply& Reply)
187 {
188         if (!m_bCanceled && m_pCbFunc)
189                 ((maps_service_multi_reverse_geocode_cb)m_pCbFunc)((maps_error_e)GetErrorCode(Reply), m_nReqId, 0, NULL, m_pUserData);
190         delete this;
191 }
192
193 HERE_PLUGIN_END_NAMESPACE
194