I have written a Java API, which consumes another API, which is a list of users, with the following properties in JSON format. Users have firstname, lastname, IP address, email, and location coordinates of latitude and longitude.
The Java API written is supposed to get all the users who live in London and/or live in a 50 mile radius. Mine doesn't because I can't figure out the formula needed to check for the users who live in London, or within a 50 mile radius.
Here is my Java API:
package com.company;
import org.json.JSONArray;
import org.json.JSONObject;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
public class Main
{
public static void main(String[] args)
{
// Using java.net.http.HttpClient
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder().uri(URI.create("https://bpdts-test-app.herokuapp.com/city/London/users")).build();
client.sendAsync(request, HttpResponse.BodyHandlers.ofString())
.thenApply(HttpResponse::body)
.thenApply(Main::parse)
.join();
}
// Parse the returned JSON data
public static String parse(String responseBody)
{
System.out.println("People who live in London");
JSONArray usersLondon = new JSONArray((responseBody));
for (int i = 0; i < usersLondon.length(); i++)
{
JSONObject userLondon = usersLondon.getJSONObject(i);
int id = userLondon.getInt("id");
String first_name = userLondon.getString("first_name");
String last_name = userLondon.getString("last_name");
String email = userLondon.getString("email");
String ip_address = userLondon.getString("ip_address");
int latitude = userLondon.getInt("latitude");
int longitude = userLondon.getInt("longitude");
System.out.println("id: " + id + " " + "first name: " + first_name + " " + "last name: " + last_name + " " + "email: " + email + " "
+ "IP Address: " + ip_address + " " + "latitude: " + latitude + " " + "longtitude: " + longitude);
}
return null;
}
}
So it returns just 6 users, which I know is incorrect. What would the Mathematical formula be, to test whether the coordinates for users in the API are are living in London, and/or living within a 50 mile radius of London?
Appreciate your help.