Android location change Latitude and Longitude from DMS format to double - android

I have this format for a location (24°13'30.92"N,55°45'51.94"E) and I need to convert it through android to a double value like (24.225275, 55.764417).
I tried something like this and it did not work
String lonStr="24°13'30.92"N";
Double lon=Location.convert(lonStr);

Use substring and indexOf
extract minutes and second divide by 60 and 3600 respectively and
finally add degree part.
String str="24°13'30.92\"N,55°45'51.94\"E";
String s[]=str.split(",");
double lat=Integer.parseInt(s[0].substring(0,s[0].indexOf("°")))+(double)Integer.parseInt(s[0].substring(s[0].indexOf("°")+1, s[0].indexOf("'")))/60+ Double.parseDouble(s[0].substring(s[0].indexOf("'")+1, s[0].indexOf("\"")))/3600;
// same for longitude
System.out.println(String.format("%.6f", lat));
output:
24.225256

Related

Fatal Exception: java.lang.NumberFormatException

I get this exception from time to time, when I get a new location coordinate from the GPS, and I want to format it.
This is my code:
DecimalFormat decimalFormat = new DecimalFormat("0.00000");
location.setLatitude(Double.valueOf(decimalFormat.format(location.getLatitude())));
location.setLongitude(Double.valueOf(decimalFormat.format(location.getLongitude())));
Why does this happen? The latitude and longitude that I get back from the location are both Doubles. I format it transforming it into the needed format (5 decimals after point) and then when I try to make a double back, it crashes. Why does this happen? And why not everytime, just sometimes?
Example on what crashed:
Fatal Exception: java.lang.NumberFormatException
Invalid double: "52,36959"
This is where I use it:
Log.i("","enteredd latitude is:" + location.getLatitude());
try{
DecimalFormat decimalFormat = new DecimalFormat("0.00000");
location.setLatitude(Double.valueOf(decimalFormat.format(location.getLatitude()).replace(",", ".")));
location.setLongitude(Double.valueOf(decimalFormat.format(location.getLongitude()).replace(",", ".")));
}catch (Exception e){
Log.e("","enteredd error deimal formating" + e.getMessage());
}
Log.i("","enteredd latitude is:" + location.getLatitude());
And the same thing for:
location = LocationServices.FusedLocationApi.getLastLocation(PSLocationCenter.getInstance().mLocationClient);
Question: Will it fix it if I do it like this?
location.setLatitude(Double.valueOf(decimalFormat.format(location.getLatitude()).replace(",", ".")));
You are putting a double value into a decimal format, then parsing it back to double and using the very same value that you have gotten from the variable location to set the values for location. There is an exceeding amount of poor logic in this.
location.setLatitude(Double.valueOf(decimalFormat.format(location.getLatitude())));
location.setLongitude(Double.valueOf(decimalFormat.format(location.getLongitude())));
location.getLatitude is the latitude, you do not need to set it.
You need to set a decimal separator:
DecimalFormat decimalFormat = new DecimalFormat("0.00000");
DecimalFormatSymbols dfs = new DecimalFormatSymbols();
dfs.setDecimalSeparator('.');
decimalFormat.setDecimalFormatSymbols(dfs);
From here: (android) decimalformat, uses comma in place of fullstop while formatting with "#.##"
Also:
Get location.
double latitude = location.getLatitude();
double longitude = location.getLongitude();
For the purposes of your app, you do not need to change this.
Just pass the values to the server that you need:
String lat = decimalFormat(latitude);
String long = decimalFormat(longitude);

Android How can I convert String to Double without losing precision? [duplicate]

This question already has answers here:
How can I convert String to Double without losing precision in Java?
(4 answers)
Closed 9 years ago.
i want to develop one calculation base application but getting one problem.
Tried as below
double add_num = 10.06
String data = edittext.getText().toString();
value assign to data is
// data = 1000.06
now i am converting string to double
double amount = Double.parseDouble(data);
// amount = 1000.0
double final_amount = amount + add_num;
// final_amount = 1010.0
getting final_amount is 1000.0 which is not correct because amount value is losing precision i want the correct answer which is 1000.06
please let me know correct way without using format() method
Well, first of all correct value is 1010.12, not 1000.06. And this code:
double add_num = 10.06;
String value = "1000.06";
double amount = Double.parseDouble(value);
double final_amount = amount + add_num;
System.out.println(final_amount);
prints 1010.1199999999999, which is correct.
If you just want to print the number with the desired precision, use one of:
// Prints with two decimal places: "1010.12"
System.out.format("%.2f", final_amount);
System.out.println(String.format("%.2f", final_amount));
// Example, set TextView text with two decimal places:
edittext.setText(String.format("%.2f", final_amount));
By the way, in your code you have:
String data = edittext.getText().toString();
double amount = Double.parseDouble(value); // value?
Shouldn't it be?
double amount = Double.parseDouble(data);
String s="1000.06";
double d=Double.parseDouble(s);
System.out.println(d);
The above code give output: 1000.06
You should use String.ValueOf(double d);
For Example:-
Double to String
String value=String.valueOf(1000.06);
OR
String value=String.valueOf(add_num);
String to Double
Double value=Double.valueOf("1000.06");
OR
Double value=Double.valueOf(add_num);

remove latitude and longitude fraction part after 6 digit

i get lat and long in this format
Latitude23.132679999999997, Longitude72.20081833333333
but i want to in this format
Latitude = 23.132680 and Longitude 72.200818
how can i convert
double Latitude = 23.132679999999997;
int precision = Math.pow(10, 6);
double new_Latitude = double((int)(precision * Latitude))/precision;
This will give you only 6 digits after decimal point.
double d=23.132679999999997;
DecimalFormat dFormat = new DecimalFormat("#.######");
d= Double.valueOf(dFormat .format(d));
Once I solved my problem like this -
String.format("%.6f", latitude);
Return value is string. So you can use this if you need string result.
If you need double you can convert using Double.parseDouble() method.
So you want round a double to an arbitrary number of digits, don't you?
can use like
DecimalFormat df = new DecimalFormat("#,###,##0.00");
System.out.println(df.format(364565.14343));
If you have Latitude and Longitude as String then you can do
latitude = latitude.substring(0,latitude.indexOf(".")+6);
Of course you should check that there are at least 6 characters after "." by checking string length

Geo Point vs Location

This is maybe a noob question but im not 100% sure about it.
How can i make a Location Object using Geo points? I want to use it to get the distance between two points.
I already found a thread where it says
Location loc1 = new Location("Location");
loc.setLatitude(geoPoint.getLatitudeE6);
loc.setLongitude(geoPoint.getLongitudeE6);
Location loc2 = new Location("Location2");
loc2.setLatitude(geoPoint.getLatitudeE6);
loc2.setLongitude(geoPoint.getLongitudeE6);
and then i would use the distanceTo() to get the distance between the two points.
My Questions
What is the Providername for? ...new Location("What is this here???")
So do i have to define a Provider before or something?
I want to use this code in a for() to calaculate between more GeoPoints.
And btw - i have to convert the E6 Values back to normal?
Not exactly
loc.setLatitude() takes a double latitude. So the correct code is:
loc.setLatitude( geoPoint.getLatitudeE6() / 1E6);
Location() constructor take the name of the GPS provider used. It can be LocationManager.GPS_PROVIDER or NETWORK_PROVIDER among other values
To get the distance between two point you can use the Location class and more precisely the distanceBetween static method.
The doc is quite clear on what it does but here a quick code sample:
float[] results = new float[3];
Location.distanceBetween(destLatitude, destLongitude, mCurrentLocation.getLatitude(), mCurrentLocation.getLongitude(), results);
// result in meters, convert it in km
String distance = String.valueOf(Math.round(results[0] / 1000)) + " km");
To convert from minute/second to degree you can use the convert method of the Location class.
Log.i("MapView", "Map distance to mark (in meters): " + myLocation.distanceTo(GeoToLocation(point)) + "m");
then
public Location GeoToLocation(GeoPoint gp) {
Location location = new Location("dummyProvider");
location.setLatitude(gp.getLatitudeE6()/1E6);
location.setLongitude(gp.getLongitudeE6()/1E6);
return location;
}

Exif only returning lat & lon as whole numbers

Through my app I upload images to a database. With the images I want to capture the location the image was taken by using the exif data. When I tested on my phone the lat and lon did upload but only to a rounded positive number.
When I'm expecting a location of Lat = 52.4 and lon = -1.9 im actually getting lat = 52 and lon 1
Here is my code; lat and lon are both String:
ExifInterface exif = new ExifInterface(mainMenu.filename);
lat = exif.getAttribute(ExifInterface.TAG_GPS_LATITUDE);
lon = exif.getAttribute(ExifInterface.TAG_GPS_LONGITUDE);
My database values for holding the lat and lon are doubles and I've also tried float.
Looking at the documentation for ExifInterface, shouldn't you instead be using the getLatLong(float[] output) method?
float[] latLong = new float[2];
if (exif.getLatLong(latLong)) {
// latLong[0] holds the Latitude value now.
// latLong[1] holds the Longitude value now.
}
else {
// Latitude and Longitude were not included in the Exif data.
}

Categories

Resources