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_multirevgeocode.h"
19 HERE_PLUGIN_BEGIN_NAMESPACE
21 HereMultiRevGeocode::HereMultiRevGeocode(void* pCbFunc, void* pUserData, int nReqId)
26 m_pUserData = pUserData;
30 HereMultiRevGeocode::~HereMultiRevGeocode()
38 here_error_e HereMultiRevGeocode::PrepareQuery()
41 return HERE_ERROR_PERMISSION_DENIED;
43 m_pQuery = new (std::nothrow) MultiReverseQuery();
46 return HERE_ERROR_OUT_OF_MEMORY;
48 return HERE_ERROR_NONE;
51 static bool geocoder_coordinates_cb(int index, maps_coordinates_h coordinates, void *user_data)
53 double dLatitude = 0.0;
54 double dLongitude = 0.0;
55 GeoCoordinates hereCoord;
56 maps_coordinates_get_latitude(coordinates, &dLatitude);
57 maps_coordinates_get_longitude(coordinates, &dLongitude);
58 hereCoord = GeoCoordinates(dLatitude, dLongitude);
60 if (!HereUtils::IsValid(hereCoord))
61 return HERE_ERROR_INVALID_PARAMETER;
63 GeoCoordinateList *pList =(GeoCoordinateList *) user_data;
64 pList->push_back(hereCoord);
69 here_error_e HereMultiRevGeocode::PreparePositionList(const maps_coordinates_list_h hGeocodeList)
72 return HERE_ERROR_OUT_OF_MEMORY;
75 return HERE_ERROR_INVALID_PARAMETER;
77 GeoCoordinateList hereCoordList;
78 maps_coordinates_list_foreach(hGeocodeList, geocoder_coordinates_cb, &hereCoordList);
80 m_pQuery->SetMode(MultiReverseQuery::RM_RetrieveAddresses);
81 m_pQuery->SetGeocodeList(hereCoordList);
82 m_pQuery->SetMaxResults(1);
83 m_pQuery->SetGenParameter(8);
85 return HERE_ERROR_NONE;
88 here_error_e HereMultiRevGeocode::StartMultiReverse(maps_item_hashtable_h hPref)
91 return HERE_ERROR_OUT_OF_MEMORY;
93 m_nRestReqId = m_pQuery->Execute(*this, NULL);
95 return (m_nRestReqId > 0 ? HERE_ERROR_NONE : HERE_ERROR_CANCELED);
98 void HereMultiRevGeocode::OnMultiReverseReply(const MultiReverseReply& Reply)
100 if (m_bCanceled || !m_pCbFunc) // ignore call back
106 int nResults = Reply.GetNumResults();
109 maps_address_list_h address_list;
110 maps_address_list_create(&address_list);
111 maps_address_h pAddr = NULL;
112 String *additionalDataValue = NULL;
114 for (size_t i = 0; i < (size_t)nResults; i++)
116 maps_error_e error = (maps_error_e)maps_address_create(&pAddr);
118 if(error == MAPS_ERROR_NONE) {
119 pResult = (Result*)Reply.GetResult(i);
122 Address tmpAddr = (pResult->GetLocation()).GetAddress();
124 if(!tmpAddr.GetHouseNumber().empty())
125 maps_address_set_building_number(pAddr, tmpAddr.GetHouseNumber().c_str());
127 if(!tmpAddr.GetStreet().empty())
128 maps_address_set_street(pAddr, tmpAddr.GetStreet().c_str());
130 if(!tmpAddr.GetDistrict().empty())
131 maps_address_set_district(pAddr, tmpAddr.GetDistrict().c_str());
133 if(!tmpAddr.GetCity().empty())
134 maps_address_set_city(pAddr, tmpAddr.GetCity().c_str());
136 additionalDataValue = (String*)tmpAddr.GetAdditionalDataValue("CountyName");
137 if (additionalDataValue && !additionalDataValue->empty())
138 maps_address_set_county(pAddr, additionalDataValue->c_str());
139 else if (!tmpAddr.GetCounty().empty())
140 maps_address_set_county(pAddr, tmpAddr.GetCounty().c_str());
142 additionalDataValue = (String*)tmpAddr.GetAdditionalDataValue("StateName");
143 if (additionalDataValue && !additionalDataValue->empty())
144 maps_address_set_state(pAddr, additionalDataValue->c_str());
145 else if (!tmpAddr.GetState().empty())
146 maps_address_set_state(pAddr, tmpAddr.GetState().c_str());
148 additionalDataValue = (String*)tmpAddr.GetAdditionalDataValue("CountryName");
149 if (additionalDataValue && !additionalDataValue->empty())
150 maps_address_set_country(pAddr, additionalDataValue->c_str());
151 else if (!tmpAddr.GetCountry().empty())
152 maps_address_set_country(pAddr, tmpAddr.GetCountry().c_str());
154 if(!tmpAddr.GetCountry().empty())
155 maps_address_set_country_code(pAddr, tmpAddr.GetCountry().c_str());
157 if(!tmpAddr.GetPostalCode().empty())
158 maps_address_set_postal_code(pAddr, tmpAddr.GetPostalCode().c_str());
160 if(!tmpAddr.GetLabel().empty())
161 maps_address_set_freetext(pAddr, tmpAddr.GetLabel().c_str());
164 maps_address_list_append(address_list, pAddr);
167 if (m_bCanceled || !m_pCbFunc) {
168 maps_address_list_destroy(address_list);
169 } else if (nResults <= 0) {
170 ((maps_service_multi_reverse_geocode_cb)m_pCbFunc)(MAPS_ERROR_NOT_FOUND, m_nReqId, 0, NULL, m_pUserData);
172 ((maps_service_multi_reverse_geocode_cb)m_pCbFunc)(MAPS_ERROR_NONE, m_nReqId, nResults, address_list, m_pUserData);
178 void HereMultiRevGeocode::OnMultiReverseFailure(const MultiReverseReply& Reply)
180 if (!m_bCanceled && m_pCbFunc)
181 ((maps_service_multi_reverse_geocode_cb)m_pCbFunc)((maps_error_e)GetErrorCode(Reply), m_nReqId, 0, NULL, m_pUserData);
185 HERE_PLUGIN_END_NAMESPACE