//GIVE THE DISTANCE BETWEEN TWO LAT, LNG COORDINATES IN METERS
public double distanceInMeters(double lat1, double lng1, double lat2,double lng2) {
double earthRadius = 3958.75;
double dLat = Math.toRadians(lat2 - lat1);
double dLng = Math.toRadians(lng2 - lng1);
double a = Math.sin(dLat / 2) * Math.sin(dLat / 2)
+ Math.cos(Math.toRadians(lat1))
* Math.cos(Math.toRadians(lat2)) * Math.sin(dLng / 2)* Math.sin(dLng / 2);
double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
double dist = earthRadius * c;
int meterConversion = 1609;
return dist * meterConversion;
}
//GIVE THE NEAREST LAT, LNG COORDINATE WHICH IS IN A ROAD, FOR THE GIVEN LAT, LNG
private double[] nearToRoad(double[] latLng1) {
double[] latLng = new double[] { latLng1[0], latLng1[1] };
try {
URL url = new URL(
"http://maps.googleapis.com/maps/api/directions/xml?origin="+ latLng1[0] + "," + latLng1[1] + "&destination=" + latLng1[0] + "," + latLng1[1] + "&sensor=false");
BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream()));
String line;
while ((line = reader.readLine()) != null) {
if (line.contains("<lat>")) {
latLng[0] = Double.parseDouble(line.substring(line.indexOf('>') + 1, line.indexOf('/') - 1));
line = reader.readLine();
latLng[1] = Double.parseDouble(line.substring(line.indexOf('>') + 1, line.indexOf('/') - 1));
break;
}
}
reader.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return latLng;
}
//GIVE THE ADDRESS OF THE LAT, LNG COORDINATE (REVERSE GEOCODING)
public String findLocationAddress(double lat, double lng) {
String name = "unnamed";
try {
URL url = new URL(
"https://maps.googleapis.com/maps/api/geocode/xml?latlng="+ lat + "," + lng + "&sensor=false");
BufferedReader reader = new BufferedReader(new InputStreamReader(
url.openStream()));
String line;
while ((line = reader.readLine()) != null) {
if (line.contains("<formatted_address>")) {
name = line.substring(line.indexOf('>') + 1,line.indexOf('/') - 1);
break;
}
}
reader.close();
} catch (Exception e) {
e.printStackTrace();
}
return name;
}
public double distanceInMeters(double lat1, double lng1, double lat2,double lng2) {
double earthRadius = 3958.75;
double dLat = Math.toRadians(lat2 - lat1);
double dLng = Math.toRadians(lng2 - lng1);
double a = Math.sin(dLat / 2) * Math.sin(dLat / 2)
+ Math.cos(Math.toRadians(lat1))
* Math.cos(Math.toRadians(lat2)) * Math.sin(dLng / 2)* Math.sin(dLng / 2);
double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
double dist = earthRadius * c;
int meterConversion = 1609;
return dist * meterConversion;
}
//GIVE THE NEAREST LAT, LNG COORDINATE WHICH IS IN A ROAD, FOR THE GIVEN LAT, LNG
private double[] nearToRoad(double[] latLng1) {
double[] latLng = new double[] { latLng1[0], latLng1[1] };
try {
URL url = new URL(
"http://maps.googleapis.com/maps/api/directions/xml?origin="+ latLng1[0] + "," + latLng1[1] + "&destination=" + latLng1[0] + "," + latLng1[1] + "&sensor=false");
BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream()));
String line;
while ((line = reader.readLine()) != null) {
if (line.contains("<lat>")) {
latLng[0] = Double.parseDouble(line.substring(line.indexOf('>') + 1, line.indexOf('/') - 1));
line = reader.readLine();
latLng[1] = Double.parseDouble(line.substring(line.indexOf('>') + 1, line.indexOf('/') - 1));
break;
}
}
reader.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return latLng;
}
//GIVE THE ADDRESS OF THE LAT, LNG COORDINATE (REVERSE GEOCODING)
public String findLocationAddress(double lat, double lng) {
String name = "unnamed";
try {
URL url = new URL(
"https://maps.googleapis.com/maps/api/geocode/xml?latlng="+ lat + "," + lng + "&sensor=false");
BufferedReader reader = new BufferedReader(new InputStreamReader(
url.openStream()));
String line;
while ((line = reader.readLine()) != null) {
if (line.contains("<formatted_address>")) {
name = line.substring(line.indexOf('>') + 1,line.indexOf('/') - 1);
break;
}
}
reader.close();
} catch (Exception e) {
e.printStackTrace();
}
return name;
}
No comments:
Post a Comment