Fatal Exception: java.lang.NumberFormatException - android

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);

Related

how to save geolocation co-ordinates

First of all I'm a complete noob to android. Going step by step on my first app and facing some problems.
I can get my location in terms of Lat and Lon and now i have to save it to file and able to read the file to compare location in future. Could anybody please help me out on this can be done.
Following is my INCORRECT CODE
public void saveCurrentLocation(Location location){
SharedPreferences prefs = this.getSharedPreferences("com.example.mylocation", Context.MODE_PRIVATE);
String currentLat = "com.example.mylocation.location";
String now = prefs.getString(currentLat, location.getLatitude());
}
Error shown is that location.getLatitude is a double and cannot be saved to string (quite obvious but not sure how to change it)
Thanks
location.getLatitude() + "";
In Java, the + operator is overloaded to concatenate Strings. If you add "" to anything, it will be automatically cast to String.
If you want to store the result of location.getLatitude() in your sharedpreferences try converting the double to a String:
String.valueOf(location.getLatitude())

Reverse Geocoding issue

I implemented the reverse geocoding in my app and it is working, but sometimes it happens a very strange issue.
The code is that
List<Address> addresses = geo.getFromLocation(
obj.getLatitude(), obj.getLongitude(), 1);
List<Address> address = geo.getFromLocationName( addresses.get(0).getLocality().getBytes() , 1 );
Address location = address.get(0);
In the first part I get the address object of the place in which I'm. Than I want recover the generic coordinates of the city in where I'm because I don't want store the coordinates of my exact position.
This is working but I encounter a very strange issue! Trying the app with the fakegps app I set my position in "Ñuñoa", and the first address was found correctly, but when I try to get the generic coordinates, I get "Nunoa" that isn't in Chile, but in Peru!!
That makes no sense! why this?
Thanks for helping me
Sure Swathi.
List<Address> addresses = geo.getFromLocation(msg_r.getLatitude(),
msg_r.getLongitude(), 1);
String geoL = addresses.get(0).getLocality() + ", " + addresses.get(0).getAdminArea() + ", " + addresses.get(0).getCountryCode();
// reverse-reverseGeocoding
List<Address> address = geo.getFromLocationName(geoL, 1);
System.out.println("Where I am? " + geoL);
Address location = address.get(0);
// generic coordinate for the locality/city/town
location.getLatitude();
location.getLongitude();

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