I have an hybrid C++ / Javascript application that displays a Leaflet map inside a QtWebEngine view
http://doc.qt.io/qt-5/qwebengineview.html
or inside an wxWebView
http://docs.wxwidgets.org/trunk/classwx_web_view.html
so far the communication has been 1 directional
in C++ I define Javascript as a C++ string and call the appropriate javascript running method (from Qt or WxWidgets), like this I can input , say latitude and longitude on the C++ side, and the javascrit side is just the end point.
for example, for Qt
void WebView::loadFinished(bool)
{
std::string str_js = get_render_javascript_leaflet();
page()->runJavaScript(QString::fromStdString(str_js));
}
std::string WebView::get_render_javascript_leaflet()
{
std::string js;
std::string str_lat;
std::string str_lon;
str_lat = std::to_string((long double)38.9250);
str_lon = std::to_string((long double)-77.0387);
js = "var map = L.map('map').setView([";
js += str_lat;
js += ",";
js += str_lon;
js += "], 14);";
js += "map.options.scrollWheelZoom = false;";
js += "map.options.minZoom = 2;";
js += "map.options.maxZoom = 20;";
js += "L.tileLayer('http://{s}.google.com/vt/lyrs=p&x={x}&y={y}&z={z}',{";
js += "maxZoom: 20,";
js += "subdomains:['mt0','mt1','mt2','mt3']";
js += "}).addTo(map);";
//mapzen geocoder
js += "L.control.geocoder('mapzen-<YOUR MAPZEN KEY>').addTo(map);";
return js;
}
but now , I want to add a geocoding input to the map, using Mapzeen,
https://github.com/mapzen/leaflet-geocoder
so i was wondering how the call
//mapzen geocoder
js += "L.control.geocoder('mapzen-<YOUR MAPZEN KEY>').addTo(map);";
could return the geocoding results to the C++ side.
Qt has methods that allow peer-to-peer communication between a C++ application and a client (HTML/JavaScript) , but the question here is more what to retrieve from the Mapzen search
There are 2 ways to solve this
1) First, read the Mazen documentation, always a good idea before asking.
2) The first way:
add an event listener for the "results" event on the geocoder
https://github.com/mapzen/leaflet-geocoder#on-results-or-error
then get the results data, and pass that as JSON through the qtwebchannel.
example of an mapzen event for a leaflet map (replace YOUR_API_KEY with a key obtained on the mapzen web site)
3) A simpler way
Do a HTTP request using C sockets.
This way is pure C/C++ , so no need to use qtwebchannel to link to Javascript.
the result (latitude, longitude) can be sent directly to a leaflet map without the need to do the geocode query inside the javascript.
It's just a matter of constructing an HTTP header that has the query parameters we need.
an example in C++:
this uses lib_netsockets
https://github.com/pedro-vicente/lib_netsockets/blob/master/examples/http_client.cc
mapzen replied with this HTTP