tizen 2.4 release
[framework/location/maps-plugin-here.git] / src / here / here_revgeocode.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_revgeocode.h"
18
19 HERE_PLUGIN_BEGIN_NAMESPACE
20
21 HereRevGeocode::HereRevGeocode(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 HereRevGeocode::~HereRevGeocode()
31 {
32         if (m_pQuery)
33         {
34                 delete m_pQuery;
35                 m_pQuery = NULL;
36         }
37 }
38
39 here_error_e HereRevGeocode::PrepareQuery()
40 {
41         if (m_pQuery)
42                 return HERE_ERROR_PERMISSION_DENIED;
43
44         m_pQuery = new ReverseGeoCoderQuery();
45
46         if (!m_pQuery)
47                 return HERE_ERROR_OUT_OF_MEMORY;
48         else
49                 return HERE_ERROR_NONE;
50 }
51
52 here_error_e HereRevGeocode::PreparePreference(maps_preference_h hPref)
53 {
54         if (!m_pQuery)
55                 return HERE_ERROR_OUT_OF_MEMORY;
56
57         if (!hPref)
58                 return HERE_ERROR_INVALID_PARAMETER;
59
60         int ret;
61         char *szLanguage;
62         ret = maps_preference_get_language(hPref, &szLanguage);
63         if (ret == MAPS_ERROR_NONE && szLanguage && *szLanguage)
64                 m_pQuery->AppendPreferredLanguage(szLanguage);
65         g_free(szLanguage);
66
67         int nMaxResults;
68         ret = maps_preference_get_max_results(hPref, &nMaxResults);
69         if (ret == MAPS_ERROR_NONE)
70                 m_pQuery->SetMaxResults((size_t)nMaxResults);
71
72         return HERE_ERROR_NONE;
73 }
74
75 here_error_e HereRevGeocode::PreparePosition(double dLat, double dLng)
76 {
77         if (!m_pQuery)
78                 return HERE_ERROR_OUT_OF_MEMORY;
79
80         GeoCoordinates geoCoord(dLat, dLng);
81         if (!HereUtils::IsValid(geoCoord))
82                 return HERE_ERROR_INVALID_PARAMETER;
83
84         m_pQuery->SetProximity(geoCoord, 0);
85         m_pQuery->SetMode(ReverseGeoCoderQuery::RM_RetrieveAddresses);
86
87         return HERE_ERROR_NONE;
88 }
89
90 here_error_e HereRevGeocode::StartRevGeocode(maps_item_hashtable_h hPref)
91 {
92         if (!m_pQuery)
93                 return HERE_ERROR_OUT_OF_MEMORY;
94
95         m_nRestReqId = m_pQuery->Execute(*this, NULL);
96
97         return (m_nRestReqId > 0 ? HERE_ERROR_NONE : HERE_ERROR_INVALID_OPERATION);
98 }
99
100 void HereRevGeocode::OnGeoCoderReply(const GeoCoderReply& Reply)
101 {
102         if (m_bCanceled) // ignore call back if it was cancelled.
103         {
104                 delete this;
105                 return;
106         }
107
108
109         int nResults = Reply.GetNumResults();
110         Result* pResult;
111         float fDistance, fShortestDistance = 0;
112         int nShortestIdx = -1;
113
114         for (size_t i = 0; i < (size_t)nResults; i++)
115         {
116                 pResult = (Result*)Reply.GetResult(i);
117
118                 if(pResult)
119                 {
120                         fDistance = pResult->GetDistance();
121
122                         if (nShortestIdx < 0 || fDistance < fShortestDistance)
123                         {
124                                 fShortestDistance = fDistance;
125                                 nShortestIdx = i;
126                         }
127                 }
128         }
129
130
131         if (nShortestIdx < 0)
132         {
133                 ((maps_service_reverse_geocode_cb)m_pCbFunc)(MAPS_ERROR_NOT_FOUND,
134                         m_nReqId, 0, 1, NULL, m_pUserData);
135                 delete this;
136                 return;
137         }
138
139         maps_address_h hAddr = NULL;
140         maps_error_e error = (maps_error_e)maps_address_create(&hAddr);
141
142         if(error == MAPS_ERROR_NONE)
143         {
144                 pResult = (Result*)Reply.GetResult(nShortestIdx);
145
146                 if (pResult)
147                 {
148                         Address tmpAddr = (pResult->GetLocation()).GetAddress();
149
150                         if(!tmpAddr.GetHouseNumber().empty())
151                                 maps_address_set_building_number(hAddr, tmpAddr.GetHouseNumber().c_str());
152
153                         if(!tmpAddr.GetStreet().empty())
154                                 maps_address_set_street(hAddr, tmpAddr.GetStreet().c_str());
155
156                         if(!tmpAddr.GetDistrict().empty())
157                                 maps_address_set_district(hAddr, tmpAddr.GetDistrict().c_str());
158
159                         if(!tmpAddr.GetCity().empty())
160                                 maps_address_set_city(hAddr, tmpAddr.GetCity().c_str());
161
162                         if(!tmpAddr.GetCounty().empty())
163                                 maps_address_set_county(hAddr, tmpAddr.GetCounty().c_str());
164
165                         if(!tmpAddr.GetState().empty())
166                                 maps_address_set_state(hAddr, tmpAddr.GetState().c_str());
167
168                         if(!tmpAddr.GetCountry().empty())
169                                 maps_address_set_country(hAddr, tmpAddr.GetCountry().c_str());
170
171                         if(!tmpAddr.GetCountryCode().empty())
172                                 maps_address_set_country_code(hAddr, tmpAddr.GetCountryCode().c_str());
173
174                         if(!tmpAddr.GetPostalCode().empty())
175                                 maps_address_set_postal_code(hAddr, tmpAddr.GetPostalCode().c_str());
176
177                         if(!tmpAddr.GetLabel().empty())
178                                 maps_address_set_freetext(hAddr, tmpAddr.GetLabel().c_str());
179                 }
180         }
181
182         if (m_bCanceled)
183         {
184                 maps_address_destroy(hAddr);
185         }
186         else
187         {
188                 ((maps_service_reverse_geocode_cb)m_pCbFunc)(error, m_nReqId, 0, 1, hAddr, m_pUserData);
189         }
190
191         delete this;
192 }
193
194 HERE_PLUGIN_END_NAMESPACE
195