diff --git a/DataStructures/RouteParameters.cpp b/DataStructures/RouteParameters.cpp index 4357ebd39..1019a9149 100644 --- a/DataStructures/RouteParameters.cpp +++ b/DataStructures/RouteParameters.cpp @@ -33,7 +33,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. RouteParameters::RouteParameters() : zoom_level(18), print_instructions(false), alternate_route(true), geometry(true), - compression(true), deprecatedAPI(false), uturn_default(false), check_sum(-1) + compression(true), deprecatedAPI(false), uturn_default(false), check_sum(-1), num_results(1) { } @@ -45,6 +45,14 @@ void RouteParameters::setZoomLevel(const short level) } } +void RouteParameters::setNumberOfResults(const short number) +{ + if (number > 0 && number <= 100) + { + num_results = number; + } +} + void RouteParameters::setAlternateRouteFlag(const bool flag) { alternate_route = flag; } void RouteParameters::setUTurn(const bool flag) diff --git a/Include/osrm/RouteParameters.h b/Include/osrm/RouteParameters.h index 33a8296c3..fd570a1ef 100644 --- a/Include/osrm/RouteParameters.h +++ b/Include/osrm/RouteParameters.h @@ -40,6 +40,8 @@ struct RouteParameters RouteParameters(); void setZoomLevel(const short level); + + void setNumberOfResults(const short number); void setAlternateRouteFlag(const bool flag); @@ -77,6 +79,7 @@ struct RouteParameters bool deprecatedAPI; bool uturn_default; unsigned check_sum; + short num_results; std::string service; std::string output_format; std::string jsonp_parameter; diff --git a/Plugins/NearestPlugin.h b/Plugins/NearestPlugin.h index fc095bcfb..71f99188c 100644 --- a/Plugins/NearestPlugin.h +++ b/Plugins/NearestPlugin.h @@ -53,13 +53,13 @@ template class NearestPlugin : public BasePlugin reply = http::Reply::StockReply(http::Reply::badRequest); return; } - + int number_of_results = route_parameters.num_results; std::vector phantom_node_vector; facade->IncrementalFindPhantomNodeForCoordinate(route_parameters.coordinates.front(), phantom_node_vector, route_parameters.zoom_level, - 1); - + number_of_results); + JSON::Object json_result; if (phantom_node_vector.empty() || !phantom_node_vector.front().isValid()) { @@ -69,17 +69,41 @@ template class NearestPlugin : public BasePlugin { reply.status = http::Reply::ok; json_result.values["status"] = 0; - JSON::Array json_coordinate; - json_coordinate.values.push_back(phantom_node_vector.front().location.lat / - COORDINATE_PRECISION); - json_coordinate.values.push_back(phantom_node_vector.front().location.lon / - COORDINATE_PRECISION); - json_result.values["mapped_coordinate"] = json_coordinate; - std::string temp_string; - facade->GetName(phantom_node_vector.front().name_id, temp_string); - json_result.values["name"] = temp_string; + + if (number_of_results > 1) + { + JSON::Array results; + + int vector_length = phantom_node_vector.size(); + for (const auto i : osrm::irange(0, std::min(number_of_results, vector_length))) + { + JSON::Array json_coordinate; + JSON::Object result; + json_coordinate.values.push_back(phantom_node_vector.at(i).location.lat / + COORDINATE_PRECISION); + json_coordinate.values.push_back(phantom_node_vector.at(i).location.lon / + COORDINATE_PRECISION); + result.values["mapped coordinate"] = json_coordinate; + std::string temp_string; + facade->GetName(phantom_node_vector.front().name_id, temp_string); + result.values["name"] = temp_string; + results.values.push_back(result); + } + json_result.values["results"] = results; + } + else + { + JSON::Array json_coordinate; + json_coordinate.values.push_back(phantom_node_vector.front().location.lat / + COORDINATE_PRECISION); + json_coordinate.values.push_back(phantom_node_vector.front().location.lon / + COORDINATE_PRECISION); + json_result.values["mapped_coordinate"] = json_coordinate; + std::string temp_string; + facade->GetName(phantom_node_vector.front().name_id, temp_string); + json_result.values["name"] = temp_string; + } } - JSON::render(reply.content, json_result); } diff --git a/Server/APIGrammar.h b/Server/APIGrammar.h index ddf53ce71..83a5cd03b 100644 --- a/Server/APIGrammar.h +++ b/Server/APIGrammar.h @@ -40,7 +40,7 @@ struct APIGrammar : qi::grammar explicit APIGrammar(HandlerT * h) : APIGrammar::base_type(api_call), handler(h) { api_call = qi::lit('/') >> string[boost::bind(&HandlerT::setService, handler, ::_1)] >> *(query) >> -(uturns); - query = ('?') >> (+(zoom | output | jsonp | checksum | location | hint | u | cmp | language | instruction | geometry | alt_route | old_API)); + query = ('?') >> (+(zoom | output | jsonp | checksum | location | hint | u | cmp | language | instruction | geometry | alt_route | old_API | num_results) ) ; zoom = (-qi::lit('&')) >> qi::lit('z') >> '=' >> qi::short_[boost::bind(&HandlerT::setZoomLevel, handler, ::_1)]; output = (-qi::lit('&')) >> qi::lit("output") >> '=' >> string[boost::bind(&HandlerT::setOutputFormat, handler, ::_1)]; @@ -56,6 +56,7 @@ struct APIGrammar : qi::grammar language = (-qi::lit('&')) >> qi::lit("hl") >> '=' >> string[boost::bind(&HandlerT::setLanguage, handler, ::_1)]; alt_route = (-qi::lit('&')) >> qi::lit("alt") >> '=' >> qi::bool_[boost::bind(&HandlerT::setAlternateRouteFlag, handler, ::_1)]; old_API = (-qi::lit('&')) >> qi::lit("geomformat") >> '=' >> string[boost::bind(&HandlerT::setDeprecatedAPIFlag, handler, ::_1)]; + num_results = (-qi::lit('&')) >> qi::lit("num_results") >> '=' >> qi::short_[boost::bind(&HandlerT::setNumberOfResults, handler, ::_1)]; string = +(qi::char_("a-zA-Z")); stringwithDot = +(qi::char_("a-zA-Z0-9_.-")); @@ -65,7 +66,7 @@ struct APIGrammar : qi::grammar qi::rule api_call, query; qi::rule service, zoom, output, string, jsonp, checksum, location, hint, stringwithDot, stringwithPercent, language, instruction, geometry, - cmp, alt_route, u, uturns, old_API ; + cmp, alt_route, u, uturns, old_API, num_results; HandlerT * handler; };