Skip to content

Commit

Permalink
BGeolocation: make it work with the actual API.
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
pulkomandy committed Sep 24, 2014
1 parent 7c9c3b6 commit 80e7d92
Showing 1 changed file with 19 additions and 13 deletions.
32 changes: 19 additions & 13 deletions src/kits/network/libnetapi/Geolocation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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)
Expand Down Expand Up @@ -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());

Expand Down Expand Up @@ -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");

0 comments on commit 80e7d92

Please sign in to comment.