2 * Copyright (c) 2014 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.
17 #include "here_revgeocode.h"
19 HERE_PLUGIN_BEGIN_NAMESPACE
21 HereRevGeocode::HereRevGeocode(void* pCbFunc, void* pUserData, int nReqId)
26 m_pUserData = pUserData;
30 HereRevGeocode::~HereRevGeocode()
38 here_error_e HereRevGeocode::PrepareQuery()
41 return HERE_ERROR_PERMISSION_DENIED;
43 m_pQuery = new (std::nothrow) ReverseGeoCoderQuery();
46 return HERE_ERROR_OUT_OF_MEMORY;
48 return HERE_ERROR_NONE;
51 here_error_e HereRevGeocode::PreparePreference(maps_preference_h hPref)
54 return HERE_ERROR_OUT_OF_MEMORY;
57 return HERE_ERROR_NONE;
61 ret = maps_preference_get_language(hPref, &szLanguage);
62 if (ret == MAPS_ERROR_NONE && szLanguage && *szLanguage)
63 m_pQuery->AppendPreferredLanguage(szLanguage);
67 ret = maps_preference_get_max_results(hPref, &nMaxResults);
68 if (ret == MAPS_ERROR_NONE)
69 m_pQuery->SetMaxResults((size_t)nMaxResults);
71 return HERE_ERROR_NONE;
74 here_error_e HereRevGeocode::PreparePosition(double dLat, double dLng)
77 return HERE_ERROR_OUT_OF_MEMORY;
79 GeoCoordinates geoCoord(dLat, dLng);
80 if (!HereUtils::IsValid(geoCoord))
81 return HERE_ERROR_INVALID_PARAMETER;
83 m_pQuery->SetProximity(geoCoord, 0);
84 m_pQuery->SetMode(ReverseGeoCoderQuery::RM_RetrieveAddresses);
86 return HERE_ERROR_NONE;
89 here_error_e HereRevGeocode::StartRevGeocode(maps_item_hashtable_h hPref)
92 return HERE_ERROR_OUT_OF_MEMORY;
94 m_nRestReqId = m_pQuery->Execute(*this, NULL);
96 return (m_nRestReqId > 0 ? HERE_ERROR_NONE : HERE_ERROR_INVALID_OPERATION);
99 void HereRevGeocode::OnGeoCoderReply(const GeoCoderReply& Reply)
101 if (m_bCanceled || !m_pCbFunc) { // ignore call back
107 int nResults = Reply.GetNumResults();
109 float fDistance, fShortestDistance = 0;
110 int nShortestIdx = -1;
112 for (size_t i = 0; i < (size_t)nResults; i++) {
113 pResult = (Result*)Reply.GetResult(i);
116 fDistance = pResult->GetDistance();
118 if (nShortestIdx < 0 || fDistance < fShortestDistance) {
119 fShortestDistance = fDistance;
126 if (nShortestIdx < 0) {
127 ((maps_service_reverse_geocode_cb)m_pCbFunc)(MAPS_ERROR_NOT_FOUND,
128 m_nReqId, 0, 0, NULL, m_pUserData);
133 maps_address_h hAddr = NULL;
134 maps_error_e error = (maps_error_e)maps_address_create(&hAddr);
135 String *additionalDataValue = NULL;
137 if(error == MAPS_ERROR_NONE) {
138 pResult = (Result*)Reply.GetResult(nShortestIdx);
141 Address tmpAddr = (pResult->GetLocation()).GetAddress();
143 if (!tmpAddr.GetHouseNumber().empty())
144 maps_address_set_building_number(hAddr, tmpAddr.GetHouseNumber().c_str());
146 if (!tmpAddr.GetStreet().empty())
147 maps_address_set_street(hAddr, tmpAddr.GetStreet().c_str());
149 if (!tmpAddr.GetDistrict().empty())
150 maps_address_set_district(hAddr, tmpAddr.GetDistrict().c_str());
152 if (!tmpAddr.GetCity().empty())
153 maps_address_set_city(hAddr, tmpAddr.GetCity().c_str());
155 additionalDataValue = (String*)tmpAddr.GetAdditionalDataValue("CountyName");
156 if (additionalDataValue && !additionalDataValue->empty())
157 maps_address_set_county(hAddr, additionalDataValue->c_str());
158 else if (!tmpAddr.GetCounty().empty())
159 maps_address_set_county(hAddr, tmpAddr.GetCounty().c_str());
161 additionalDataValue = (String*)tmpAddr.GetAdditionalDataValue("StateName");
162 if (additionalDataValue && !additionalDataValue->empty())
163 maps_address_set_state(hAddr, additionalDataValue->c_str());
164 else if (!tmpAddr.GetState().empty())
165 maps_address_set_state(hAddr, tmpAddr.GetState().c_str());
167 additionalDataValue = (String*)tmpAddr.GetAdditionalDataValue("CountryName");
168 if (additionalDataValue && !additionalDataValue->empty())
169 maps_address_set_country(hAddr, additionalDataValue->c_str());
170 else if (!tmpAddr.GetCountry().empty())
171 maps_address_set_country(hAddr, tmpAddr.GetCountry().c_str());
173 if (!tmpAddr.GetCountry().empty())
174 maps_address_set_country_code(hAddr, tmpAddr.GetCountry().c_str());
176 if (!tmpAddr.GetPostalCode().empty())
177 maps_address_set_postal_code(hAddr, tmpAddr.GetPostalCode().c_str());
179 if (!tmpAddr.GetLabel().empty())
180 maps_address_set_freetext(hAddr, tmpAddr.GetLabel().c_str());
185 maps_address_destroy(hAddr);
187 int nResult = (error == MAPS_ERROR_NONE ? 1 : 0);
188 ((maps_service_reverse_geocode_cb)m_pCbFunc)(error, m_nReqId, 0, nResult, hAddr, m_pUserData);
194 void HereRevGeocode::OnGeoCoderFailure(const GeoCoderReply& Reply)
196 if (!m_bCanceled && m_pCbFunc)
197 ((maps_service_reverse_geocode_cb)m_pCbFunc)((maps_error_e)GetErrorCode(Reply), m_nReqId, 0, 0, NULL, m_pUserData);
201 HERE_PLUGIN_END_NAMESPACE