From 80e7d92949465164656855328ecb4b1fbe0ecbaa Mon Sep 17 00:00:00 2001 From: Adrien Destugues Date: Wed, 24 Sep 2014 18:23:31 +0200 Subject: [PATCH] BGeolocation: make it work with the actual API. * It seems openbmap is using a variation of the API that's not compatible with what other providers use. * Fix a ";" instead of "," in the JSON request. We should get a BJsonBuilder to avoid such silly errors * Improve indenting of the request to ease readability. * Parse the latitude and longitude as doubles, not strings. This was tested against Mozilla Location Services and I get accurate results (withing a few hundred meters). However I'm not sure how to share the MLS API key safely so it is used only in Haiku, I will discuss this with the MLS team. --- src/kits/network/libnetapi/Geolocation.cpp | 32 +++++++++++++--------- 1 file changed, 19 insertions(+), 13 deletions(-) diff --git a/src/kits/network/libnetapi/Geolocation.cpp b/src/kits/network/libnetapi/Geolocation.cpp index d459e43de9..e86c4b33fe 100644 --- a/src/kits/network/libnetapi/Geolocation.cpp +++ b/src/kits/network/libnetapi/Geolocation.cpp @@ -36,7 +36,7 @@ BGeolocation::LocateSelf(float& latitude, float& longitude) uint32 interfaceCookie = 0; BNetworkInterface interface; - BString query("{\"wifiAccessPoints\":["); + BString query("{\n\t\"wifiAccessPoints\": ["); int32 count = 0; while (roster.GetNextInterface(&interfaceCookie, interface) == B_OK) { @@ -48,22 +48,22 @@ BGeolocation::LocateSelf(float& latitude, float& longitude) while (device.GetNextNetwork(networkCookie, network) == B_OK) { if (count != 0) - query += ';'; + query += ','; count++; - query += "\n{\"macAddress\":\""; + query += "\n\t\t{ \"macAddress\": \""; query += network.address.ToString().ToUpper(); - query += "\", \"signalStrength\":"; + query += "\", \"signalStrength\": "; query << (int)network.signal_strength; - query += ", \"signalToNoiseRatio\":"; + query += ", \"signalToNoiseRatio\": "; query << (int)network.noise_level; - query += '}'; + query += " }"; } } - query += "]}"; + query += "\n\t]\n}\n"; // Check that we have enough data (we need at least 2 networks) if (count < 2) @@ -94,6 +94,7 @@ BGeolocation::LocateSelf(float& latitude, float& longitude) return B_BAD_DATA; } + http->SetMethod(B_HTTP_POST); BMemoryIO* io = new BMemoryIO(query.String(), query.Length()); http->AdoptInputData(io, query.Length()); @@ -122,18 +123,23 @@ BGeolocation::LocateSelf(float& latitude, float& longitude) BMessage location; result = data.FindMessage("location", &location); - if(result != B_OK) + if (result != B_OK) return result; - BString lat = location.FindString("latitude"); - BString lon = location.FindString("longitude"); + location.PrintToStream(); + double lat, lon; + result = location.FindDouble("lat", &lat); + if (result == B_OK) + result = location.FindDouble("lng", &lon); - latitude = strtof(lat.String(), NULL); - longitude = strtof(lon.String(), NULL); + latitude = lat; + longitude = lon; - return B_OK; + return result; } +// FIXME switch to an openly available service that will actually work with the +// "standard" geolocation API. Openbmap has a few variations. const BUrl BGeolocation::kDefaultService = BUrl( "http://openbmap.org/api/json.php5");