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_geocode.h"
19 HERE_PLUGIN_BEGIN_NAMESPACE
21 HereGeocode::HereGeocode(void *pCbFunc, void *pUserData, int nReqId)
26 m_pUserData = pUserData;
30 HereGeocode::~HereGeocode()
39 here_error_e HereGeocode::PrepareQuery()
42 return HERE_ERROR_PERMISSION_DENIED;
44 m_pQuery = new (std::nothrow) GeoCoderQuery();
47 return HERE_ERROR_OUT_OF_MEMORY;
49 return HERE_ERROR_NONE;
52 here_error_e HereGeocode::PreparePreference(maps_preference_h hPref)
55 return HERE_ERROR_OUT_OF_MEMORY;
58 return HERE_ERROR_NONE;
61 char *szLanguage = NULL;
62 ret = maps_preference_get_language(hPref, &szLanguage);
63 if (ret == MAPS_ERROR_NONE && szLanguage && *szLanguage)
65 m_pQuery->AppendPreferredLanguage(szLanguage);
70 ret = maps_preference_get_max_results(hPref, &nMaxResults);
71 if (ret == MAPS_ERROR_NONE)
73 m_pQuery->SetMaxResults((size_t)nMaxResults);
76 return HERE_ERROR_NONE;
79 here_error_e HereGeocode::StartGeocode(const char* szAddr)
82 return HERE_ERROR_OUT_OF_MEMORY;
84 if (!szAddr || (szAddr && strlen(szAddr) <= 0))
85 return HERE_ERROR_INVALID_PARAMETER;
88 String sSearch(szAddr);
89 m_pQuery->SetSearchtext(sSearch);
92 m_nRestReqId = m_pQuery->Execute(*this, NULL);
94 return (m_nRestReqId > 0 ? HERE_ERROR_NONE : HERE_ERROR_INVALID_OPERATION);
97 here_error_e HereGeocode::StartGeocodeInsideArea(const char* szAddr, const maps_area_h hArea)
100 return HERE_ERROR_OUT_OF_MEMORY;
102 if (!szAddr || (szAddr && strlen(szAddr) <= 0) || !hArea)
103 return HERE_ERROR_INVALID_PARAMETER;
106 String sSearch(szAddr);
107 m_pQuery->SetSearchtext(sSearch);
109 maps_area_s *pArea = (maps_area_s *)hArea;
110 if (pArea->type == MAPS_AREA_RECTANGLE) {
111 double dLatTL = pArea->rect.top_left.latitude;
112 double dLngTL = pArea->rect.top_left.longitude;
113 GeoCoordinates geoCoordTL(dLatTL, dLngTL);
115 double dLatBR = pArea->rect.bottom_right.latitude;
116 double dLngBR = pArea->rect.bottom_right.longitude;
117 GeoCoordinates geoCoordBR(dLatBR, dLngBR);
119 GeoBoundingBox BoundingBox(geoCoordTL, geoCoordBR);
121 m_pQuery->SetBoundingBox(BoundingBox);
122 } else if (pArea->type == MAPS_AREA_CIRCLE) {
123 MAPS_LOGD("HERE Maps is not supported circle type in GeocoderQuery");
124 return HERE_ERROR_NOT_SUPPORTED;
126 return HERE_ERROR_INVALID_PARAMETER;
130 m_nRestReqId = m_pQuery->Execute(*this, NULL);
132 return (m_nRestReqId > 0 ? HERE_ERROR_NONE : HERE_ERROR_INVALID_OPERATION);
135 here_error_e HereGeocode::StartGeocodeByStructuredAddress(const maps_address_h hAddr)
138 return HERE_ERROR_OUT_OF_MEMORY;
141 return HERE_ERROR_INVALID_PARAMETER;
147 char *szCountry = NULL;
148 ret = maps_address_get_country(hAddr, &szCountry);
149 if (ret == MAPS_ERROR_NONE && szCountry && *szCountry)
150 rAddress.SetCountry(String(szCountry));
153 char *szCountryCode = NULL;
154 ret = maps_address_get_country_code(hAddr, &szCountryCode);
155 if (ret == MAPS_ERROR_NONE && szCountryCode && *szCountryCode)
156 rAddress.SetCountryCode(String(szCountryCode));
157 g_free(szCountryCode);
159 char *szCounty = NULL;
160 ret = maps_address_get_county(hAddr, &szCounty);
161 if (ret == MAPS_ERROR_NONE && szCounty && *szCounty)
162 rAddress.SetCounty(String(szCounty));
165 char *szState = NULL;
166 ret = maps_address_get_state(hAddr, &szState);
167 if (ret == MAPS_ERROR_NONE && szState && *szState)
168 rAddress.SetState(String(szState));
172 ret = maps_address_get_city(hAddr, &szCity);
173 if (ret == MAPS_ERROR_NONE && szCity && *szCity)
174 rAddress.SetCity(String(szCity));
177 char *szDistrict = NULL;
178 ret = maps_address_get_district(hAddr, &szDistrict);
179 if (ret == MAPS_ERROR_NONE && szDistrict && *szDistrict)
180 rAddress.SetDistrict(String(szDistrict));
183 char *szStreet = NULL;
184 ret = maps_address_get_street(hAddr, &szStreet);
185 if (ret == MAPS_ERROR_NONE && szStreet && *szStreet)
186 rAddress.SetStreet(String(szStreet));
189 char *szBuildingNumber = NULL;
190 ret = maps_address_get_building_number(hAddr, &szBuildingNumber);
191 if (ret == MAPS_ERROR_NONE && szBuildingNumber && *szBuildingNumber)
192 rAddress.SetHouseNumber(String(szBuildingNumber));
193 g_free(szBuildingNumber);
195 char *szPostalCode = NULL;
196 ret = maps_address_get_postal_code(hAddr, &szPostalCode);
197 if (ret == MAPS_ERROR_NONE && szPostalCode && *szPostalCode)
198 rAddress.SetPostalCode(String(szPostalCode));
199 g_free(szPostalCode);
201 //not defined in maps-service
202 //rAddress.SetLabel(String(sLabel));
203 //rAddress.SetFloor(String(sFloor));
204 //rAddress.SetSuite(String(sSuite));
206 m_pQuery->SetAddress(rAddress);
210 m_nRestReqId = m_pQuery->Execute(*this, NULL);
212 return (m_nRestReqId > 0 ? HERE_ERROR_NONE : HERE_ERROR_INVALID_OPERATION);
215 void HereGeocode::OnGeoCoderReply(const GeoCoderReply& Reply)
217 if (m_bCanceled || !m_pCbFunc) // ignore call back
224 size_t nResults = Reply.GetNumResults();
225 GeoCoordinates hereCoord;
226 maps_coordinates_h mapsCoord;
229 ((maps_service_geocode_cb)m_pCbFunc)(MAPS_ERROR_NOT_FOUND, m_nReqId,
230 0, 0, NULL, m_pUserData);
235 for (size_t i = 0 ; i < nResults; i++)
237 pResult = (Result*)Reply.GetResult(i);
240 hereCoord = (pResult->GetLocation()).GetDisplayPosition();
242 hereCoord.SetLatitude(0.0);
243 hereCoord.SetLongitude(0.0);
246 maps_error_e error = (maps_error_e)maps_coordinates_create(
247 hereCoord.GetLatitude(), hereCoord.GetLongitude(), &mapsCoord);
249 if (m_bCanceled || !m_pCbFunc) {
250 if (mapsCoord) maps_coordinates_destroy(mapsCoord);
253 if (((maps_service_geocode_cb)m_pCbFunc)(error, m_nReqId, i,
254 nResults, mapsCoord, m_pUserData) == FALSE) {
264 void HereGeocode::OnGeoCoderFailure(const GeoCoderReply& Reply)
266 if (!m_bCanceled && m_pCbFunc)
267 ((maps_service_geocode_cb)m_pCbFunc)((maps_error_e)GetErrorCode(Reply), m_nReqId, 0, 0, NULL, m_pUserData);
271 HERE_PLUGIN_END_NAMESPACE