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()
39 here_error_e HereRevGeocode::PrepareQuery()
42 return HERE_ERROR_PERMISSION_DENIED;
44 m_pQuery = new ReverseGeoCoderQuery();
47 return HERE_ERROR_OUT_OF_MEMORY;
49 return HERE_ERROR_NONE;
52 here_error_e HereRevGeocode::PreparePreference(maps_preference_h hPref)
55 return HERE_ERROR_OUT_OF_MEMORY;
58 return HERE_ERROR_INVALID_PARAMETER;
62 ret = maps_preference_get_language(hPref, &szLanguage);
63 if (ret == MAPS_ERROR_NONE && szLanguage && *szLanguage)
64 m_pQuery->AppendPreferredLanguage(szLanguage);
68 ret = maps_preference_get_max_results(hPref, &nMaxResults);
69 if (ret == MAPS_ERROR_NONE)
70 m_pQuery->SetMaxResults((size_t)nMaxResults);
72 return HERE_ERROR_NONE;
75 here_error_e HereRevGeocode::PreparePosition(double dLat, double dLng)
78 return HERE_ERROR_OUT_OF_MEMORY;
80 GeoCoordinates geoCoord(dLat, dLng);
81 if (!HereUtils::IsValid(geoCoord))
82 return HERE_ERROR_INVALID_PARAMETER;
84 m_pQuery->SetProximity(geoCoord, 0);
85 m_pQuery->SetMode(ReverseGeoCoderQuery::RM_RetrieveAddresses);
87 return HERE_ERROR_NONE;
90 here_error_e HereRevGeocode::StartRevGeocode(maps_item_hashtable_h hPref)
93 return HERE_ERROR_OUT_OF_MEMORY;
95 m_nRestReqId = m_pQuery->Execute(*this, NULL);
97 return (m_nRestReqId > 0 ? HERE_ERROR_NONE : HERE_ERROR_INVALID_OPERATION);
100 void HereRevGeocode::OnGeoCoderReply(const GeoCoderReply& Reply)
102 if (m_bCanceled) // ignore call back if it was cancelled.
109 int nResults = Reply.GetNumResults();
111 float fDistance, fShortestDistance = 0;
112 int nShortestIdx = -1;
114 for (size_t i = 0; i < (size_t)nResults; i++)
116 pResult = (Result*)Reply.GetResult(i);
120 fDistance = pResult->GetDistance();
122 if (nShortestIdx < 0 || fDistance < fShortestDistance)
124 fShortestDistance = fDistance;
131 if (nShortestIdx < 0)
133 ((maps_service_reverse_geocode_cb)m_pCbFunc)(MAPS_ERROR_NOT_FOUND,
134 m_nReqId, 0, 1, NULL, m_pUserData);
139 maps_address_h hAddr = NULL;
140 maps_error_e error = (maps_error_e)maps_address_create(&hAddr);
142 if(error == MAPS_ERROR_NONE)
144 pResult = (Result*)Reply.GetResult(nShortestIdx);
148 Address tmpAddr = (pResult->GetLocation()).GetAddress();
150 if(!tmpAddr.GetHouseNumber().empty())
151 maps_address_set_building_number(hAddr, tmpAddr.GetHouseNumber().c_str());
153 if(!tmpAddr.GetStreet().empty())
154 maps_address_set_street(hAddr, tmpAddr.GetStreet().c_str());
156 if(!tmpAddr.GetDistrict().empty())
157 maps_address_set_district(hAddr, tmpAddr.GetDistrict().c_str());
159 if(!tmpAddr.GetCity().empty())
160 maps_address_set_city(hAddr, tmpAddr.GetCity().c_str());
162 if(!tmpAddr.GetCounty().empty())
163 maps_address_set_county(hAddr, tmpAddr.GetCounty().c_str());
165 if(!tmpAddr.GetState().empty())
166 maps_address_set_state(hAddr, tmpAddr.GetState().c_str());
168 if(!tmpAddr.GetCountry().empty())
169 maps_address_set_country(hAddr, tmpAddr.GetCountry().c_str());
171 if(!tmpAddr.GetCountryCode().empty())
172 maps_address_set_country_code(hAddr, tmpAddr.GetCountryCode().c_str());
174 if(!tmpAddr.GetPostalCode().empty())
175 maps_address_set_postal_code(hAddr, tmpAddr.GetPostalCode().c_str());
177 if(!tmpAddr.GetLabel().empty())
178 maps_address_set_freetext(hAddr, tmpAddr.GetLabel().c_str());
184 maps_address_destroy(hAddr);
188 ((maps_service_reverse_geocode_cb)m_pCbFunc)(error, m_nReqId, 0, 1, hAddr, m_pUserData);
194 HERE_PLUGIN_END_NAMESPACE