Update 'src/Location.java'
This commit is contained in:
parent
a26e9a9a9f
commit
59c0b1d3f9
@ -1,103 +1,106 @@
|
||||
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.ProtocolException;
|
||||
import java.net.URL;
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.net.HttpURLConnection;
|
||||
import org.json.simple.parser.JSONParser;
|
||||
import org.json.simple.parser.ParseException;
|
||||
import org.json.simple.JSONArray;
|
||||
import org.json.simple.JSONObject;
|
||||
|
||||
public class Location {
|
||||
private double latitude;
|
||||
private double longitude;
|
||||
private String city;
|
||||
|
||||
public Location(double lat, double lon) {
|
||||
latitude = lat;
|
||||
longitude = lon;
|
||||
//city = cityBasedOnIP(); //API intansive
|
||||
}
|
||||
|
||||
public Location() {
|
||||
city = cityBasedOnIP();
|
||||
}
|
||||
|
||||
public double getLatitude() {
|
||||
return latitude;
|
||||
}
|
||||
|
||||
public String getCity() {
|
||||
return city;
|
||||
}
|
||||
|
||||
public void setLatitude(double latitude) {
|
||||
this.latitude = latitude;
|
||||
}
|
||||
|
||||
public double getLongitude() {
|
||||
return longitude;
|
||||
}
|
||||
|
||||
public void setLongitude(double longitude) {
|
||||
this.longitude = longitude;
|
||||
}
|
||||
|
||||
public void setLocation(double lat, double lon) {
|
||||
this.latitude = lat;
|
||||
this.longitude = lon;
|
||||
}
|
||||
|
||||
public String cityBasedOnIP() {
|
||||
String city = "";
|
||||
try {
|
||||
|
||||
URL url = new URL("http://ip-api.com/json");
|
||||
HttpURLConnection con = (HttpURLConnection) url.openConnection();
|
||||
con.setRequestMethod("GET");
|
||||
con.setRequestProperty("Content-Type", "application/json");
|
||||
int status = con.getResponseCode();
|
||||
BufferedReader in = new BufferedReader(
|
||||
new InputStreamReader(con.getInputStream()));
|
||||
String inputLine;
|
||||
StringBuffer content = new StringBuffer();
|
||||
while ((inputLine = in.readLine()) != null) {
|
||||
content.append(inputLine);
|
||||
}
|
||||
in.close();
|
||||
|
||||
JSONParser parser = new JSONParser();
|
||||
Object resultObject = parser.parse(content.toString());
|
||||
|
||||
if (resultObject instanceof JSONArray) {
|
||||
JSONArray array=(JSONArray)resultObject;
|
||||
for (Object object : array) {
|
||||
JSONObject obj =(JSONObject)object;
|
||||
city = (String)obj.get("city");
|
||||
}
|
||||
|
||||
}
|
||||
else if (resultObject instanceof JSONObject) {
|
||||
JSONObject obj =(JSONObject)resultObject;
|
||||
city = (String)obj.get("city");
|
||||
|
||||
}
|
||||
}
|
||||
catch (MalformedURLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
catch (ProtocolException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
catch (ParseException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return city;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.ProtocolException;
|
||||
import java.net.URL;
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.net.HttpURLConnection;
|
||||
import org.json.simple.parser.JSONParser;
|
||||
import org.json.simple.parser.ParseException;
|
||||
import org.json.simple.JSONArray;
|
||||
import org.json.simple.JSONObject;
|
||||
|
||||
public class Location {
|
||||
private double latitude;
|
||||
private double longitude;
|
||||
private String city;
|
||||
|
||||
public Location(double lat, double lon) {
|
||||
latitude = lat;
|
||||
longitude = lon;
|
||||
city = "Unknown";
|
||||
//city = cityBasedOnIP(); //API intansive
|
||||
}
|
||||
|
||||
public Location() {
|
||||
//city = cityBasedOnIP();
|
||||
city = "Unknown";
|
||||
}
|
||||
|
||||
public double getLatitude() {
|
||||
return latitude;
|
||||
}
|
||||
|
||||
public String getCity() {
|
||||
return city;
|
||||
}
|
||||
|
||||
public void setLatitude(double latitude) {
|
||||
this.latitude = latitude;
|
||||
}
|
||||
|
||||
public double getLongitude() {
|
||||
return longitude;
|
||||
}
|
||||
|
||||
public void setLongitude(double longitude) {
|
||||
this.longitude = longitude;
|
||||
}
|
||||
|
||||
public void setLocation(double lat, double lon) {
|
||||
this.latitude = lat;
|
||||
this.longitude = lon;
|
||||
}
|
||||
|
||||
public String cityBasedOnIP() {
|
||||
String city = "";
|
||||
try {
|
||||
|
||||
URL url = new URL("http://ip-api.com/json");
|
||||
HttpURLConnection con = (HttpURLConnection) url.openConnection();
|
||||
con.setRequestMethod("GET");
|
||||
con.setRequestProperty("Content-Type", "application/json");
|
||||
int status = con.getResponseCode();
|
||||
BufferedReader in = new BufferedReader(
|
||||
new InputStreamReader(con.getInputStream()));
|
||||
String inputLine;
|
||||
StringBuffer content = new StringBuffer();
|
||||
while ((inputLine = in.readLine()) != null) {
|
||||
content.append(inputLine);
|
||||
}
|
||||
in.close();
|
||||
|
||||
JSONParser parser = new JSONParser();
|
||||
Object resultObject = parser.parse(content.toString());
|
||||
|
||||
if (resultObject instanceof JSONArray) {
|
||||
JSONArray array=(JSONArray)resultObject;
|
||||
for (Object object : array) {
|
||||
JSONObject obj =(JSONObject)object;
|
||||
city = (String)obj.get("city");
|
||||
}
|
||||
|
||||
}
|
||||
else if (resultObject instanceof JSONObject) {
|
||||
JSONObject obj =(JSONObject)resultObject;
|
||||
city = (String)obj.get("city");
|
||||
|
||||
}
|
||||
}
|
||||
catch (MalformedURLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
catch (ProtocolException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
catch (ParseException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return city;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user