do not concatenate text displayed with settext - android

How can i have bot the lat and log in one textview separated with a comma?
I tried as below and i am getting the "do not concatenate text displayed with settext" warning!
gpsTracker = new GpsTracker( MainActivity.this);
if(gpsTracker.canGetLocation()){
double latitude = gpsTracker.getLatitude();
double longitude = gpsTracker.getLongitude();
textviewGPSLocation.setText(String.valueOf(latitude) + "/," + String.valueOf ( longitude ));

You can format your string like this
double latitude = gpsTracker.getLatitude();
double longitude = gpsTracker.getLongitude();
String text = String.format("%1$2f / %2$2f", latitude, longitude);
textviewGPSLocation.setText(text);

Related

I have two latlong values. one for source and other for destination..In which format latlong value should pass to find distance between them

DIRECTION_URL_API = "https://maps.googleapis.com/maps/api/directions/json?"
DIRECTION_URL_API + "origin=" + origin + "&destination=" + destination + "&sensor=true" + "&mode=" +typeOpt+"&key=" + GOOGLE_API_KEY ;
I am using this format but its not working
Please suggest me :)
You can find distance following way
http://maps.googleapis.com/maps/api/directions/json?origin=21.1702,72.8311&destination=21.7051,72.9959&sensor=false&units=metric&mode=driving
origin=lat1,long1
destination=lat2,long2
Please use the below method to calculate the distance between two points
/**
* Returns Distance in kilometers (km)
*/
public static String distance(double startLat, double startLong, double endLat, double endLong) {
Location startPoint = new Location("locationA");
startPoint.setLatitude(startLat);
startPoint.setLongitude(startLong);
Location endPoint = new Location("locationA");
endPoint.setLatitude(endLat);
endPoint.setLongitude(endLong);
return String.format("%.2f", startPoint.distanceTo(endPoint) / 1000); //KMs
}
Method usage -
String mDistance = distance(startLat,
startLong,
endLat,endLng)).concat("km");

How to use Latitud and Longitud as a variable in Android?

I have an SQLITE3 database where I defined lat and long as text.
I need to use those lat, and long as the final destination in a map.
The intent is defined as:
if(locationMap != null){
Intent theIntent = new Intent(getApplication(), displayMap.class);
theIntent.putExtra("_Id", locationMap.get("_Id"));
theIntent.putExtra("locCode", locationMap.get("locCode"));
theIntent.putExtra("locDesc", locationMap.get("locDesc"));
theIntent.putExtra("locLat", locationMap.get("locLat"));
theIntent.putExtra("locLong", locationMap.get("locLong"));
theIntent.putExtra("locTelephone", locationMap.get("locTelephone"));
theIntent.putExtra("locComments", locationMap.get("locComments"));
startActivity(theIntent); // display map with coordinates
}
In the next activity I recover the values in the On create method:
// Parameters
String locCode = i.getStringExtra("locCode");
String locDesc = i.getStringExtra("locDesc");
String locLat = i.getStringExtra("locLat");
String locLong = i.getStringExtra("locLong");
String locTelephone = i.getStringExtra("locTelephone");
String locComments = i.getStringExtra("locComments");
String Text = "Current location is: " +
i.getStringExtra("locLat");
Toast.makeText( getApplicationContext(),Text,
Toast.LENGTH_SHORT).show();
System.out.println("locCode: " + locCode);
System.out.println("LocDesc: " + locDesc);
System.out.println("LocLat: " + locLat);
System.out.println("LocLong: " + locLong);
System.out.println("LocTelephone: " + locTelephone);
System.out.println("LocComment: " + locComments);
getLocation(ORIGIN);
setContentView(R.layout.map);
if (mLastSelectedMarker != null && mLastSelectedMarker.isInfoWindowShown()) {
// Refresh the info window when the info window's content has changed.
mLastSelectedMarker.showInfoWindow();
}
setUpMapIfNeeded();
}
I need to use those locLat and Loclong instead of the numbers:
public class displayMap extends FragmentActivity implements
OnMarkerClickListener,
OnInfoWindowClickListener {
public LatLng ORIGIN = new LatLng(34.02143074239393, -117.61349469423294);
public LatLng DESTINY = new LatLng(34.022365269080886, -117.61271852999926);
private GoogleMap mMap;
private Marker mDestiny;
private Marker mOrigin;
private Marker mLastSelectedMarker; // keeps track of last selected marker
I've tried transforming the text to double and It won't allow me to.
I've tried many solutions I found on stack overflow, but no luck yet.
I appreciate any help
Thanks in advance.
You need to parse the latitude and longitude from String to double to use in new LatLng();
double latitude = Double.parseDouble(locLat);
double longitude = Double.parseDouble(locLong);
and then,
public LatLng ORIGIN = new LatLng(latitude, longitude);
you need cast them into double. As GPRathour says.
Change the type of Lat Long Text to REAL in your SQL Lite,
when inserting values use this
values.put(Latitude_Column, ORIGIN.latitude);
values.put(Longitude__Column,ORIGIN.longitude);
And for retrieving values
LatLng origin = new LatLng(cursor.getDouble(cursor.getColumnIndex(Latitude_Column)),cursor.getDouble(cursor.getColumnIndex(Longitude__Column)));
No need to parsing values

Editing string variables

i have a string variable for a JSON API im trying to read
in it i have to add latitude and longitude
i got the latitude and longitude inside a variable and the site inside a variable
how can i now edit the string to add my variables in?
// JSON URL
private static String url = "https://codingsquared-prod.apigee.net/masaajid?lat=42.93167&long=-81.22807";
// latitude variable
double latitude = location.getLatitude();
// longitude variable
double longitude = location.getLongitude();
i would like to edit the
lat=42.93167&long=-81.22807
(the numbers after lat= and long=
Thank you
// latitude variable
double latitude = location.getLatitude();
// longitude variable
double longitude = location.getLongitude();
// JSON URL
private static String url = "https://codingsquared-prod.apigee.net/masaajid?lat="+Double.toString(latitude)+"&long="+Double.toString(longitude);
Why not just store the base url private static String url = "https://codingsquared-prod.apigee.net/masaajid?"; and when you know the 2 variables form the complete url via concatenation?

android losing precision of the lon and lat from location object

I've an app that gets the current user's location from a location service. When i get the lon and lat from the location object, they are as follows
lat = 53.653770446777344
lon = -1.520833969116211
.
I then store these in a GeoPoint object which is passed to a query string for Google Servers. This eventually plots a polyline between the current location and a destination.
It all works fine and the polyline is drawn, however it is drawn incorrectly as the current location is set about 100 mile away. I've logged out some values and there is a loss of precision when the lon and lat get passed to the geopoint.
How can i get around this.
#Override
public void onLocationChanged(Location location) {
lati = (location.getLatitude());
lngi = (location.getLongitude());
startAddr = new GeoPoint((int)lati, (int)lngi);
Log.e(TAG, "lat = " + lati);
Log.e(TAG, "lon = " + lngi);
Log.e(TAG, "lat after cast = " + (int)(lati * 1000000));
Log.e(TAG, "lon after cast = " + (int)(lngi * 1000000));
locationManager.removeUpdates(this);
StringBuilder sb = new StringBuilder();
sb.append("http://maps.google.com/maps/api/directions/json?origin=");
sb.append(startAddr);
sb.append("&destination=");
sb.append(endAddr);
sb.append("&sensor=false");
stringUrl = sb.toString();
Log.e(TAG, "url = " + stringUrl);
AsyncGetRoute agr = new AsyncGetRoute();
agr.execute();
.
11-15 12:45:17.280: E/GetClientDirections(23220): lat = 53.653770446777344
11-15 12:45:17.280: E/GetClientDirections(23220): lon = -1.520833969116211
11-15 12:45:17.280: E/GetClientDirections(23220): lat after cast = 53653770
11-15 12:45:17.280: E/GetClientDirections(23220): lon after cast = -1520833
11-15 12:45:17.290: E/GetClientDirections(23220): url = http://maps.google.com/maps/api/directions/json?origin=53,-1&destination=AL73EZ&sensor=false
This is the problem:
startAddr = new GeoPoint((int)lati, (int)lngi);
This truncates the fractions, so effectively for your input, your result is:
startAddr = new GeoPoint(53, -1);
As the API doc says, the GeoPoint accepts integer values: the two angles multiplied by 10^6. And because of this, the coordinates given would correspond to these values:
Latitude: 0.000053
Longitude: -0.000001
You should multiply first with 10^6, then truncate, so you should try:
startAddr = new GeoPoint((int)(lati*1000000.0), (int)(lngi*1000000.0));

Geocoding android - Longitude and Latitude values incorrect

I Have an address that I would like to know the coordinates of. For instance the address is "Skillman Ave" Queens, NY. The coordinates is: 40.747281, -73.9283169 according to maps.google.com. In my app I have a function like this:
public GeoPoint addressToGeo(String adr) {
Geocoder coder = new Geocoder(this);
List<Address> address = null;
GeoPoint coordinates;
try {
address = coder.getFromLocationName(adr, 1);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (address == null) {
return null;
}
Address location = address.get(0);
location.getLatitude();
location.getLongitude();
coordinates = new GeoPoint((int) (location.getLatitude() *1E6),
(int) (location.getLongitude() * 1E6));
return coordinates;
}
Which takes an address as parameter, and hopefully it will return the coordinates. My debugger says the the first element in the list adress contains this information:
[Address[addressLines=[0:"Skillman Ave",1:"Queens, New York",2:"Amerikas forente stater"],feature=Skillman Ave,admin=New York,sub-admin=Queens,locality=Queens,thoroughfare=Skillman Ave,postalCode=null,countryCode=US,countryName=Amerikas forente stater,hasLatitude=true,latitude=40.747281,hasLongitude=true,longitude=-73.9283169,phone=null,url=null,extras=null]]
which seems correct if you look at the latitude and longitude variables. But when I type in this code:
GeoPoint test;
test = addressToGeo("Skillman Ave");
double latitude = test.getLatitudeE6();
double longitude = test.getLongitudeE6();
String lat = Double.toString(latitude);
String lng = Double.toString(longitude);
String total = lat + " " + lng;
toAdress.setText(total);
the toAdress textField will contain 4.0747281E7, -7.3928316E7 The comma is not in the right spot, and what is the E7 in the end of each double?
'E7' is notation meaning that you need to multiply by 10^7 to get the actual number. In this case it would give you 40747281. You then need to format that into a proper coordinate.
Ankit's code looks like it might do that, but test to make sure.
Try this.
String lat = Double.toString(latitude);
String lng = Double.toString(longitude);
lat= (float) (lat / 1E6);
lng = (float)(lon / 1E6);
System.out.println("lat :" + (float) lat / 1E6);
System.out.println("lon :" + (float) lon / 1E6);
You've got all the right data, so this question is really about formatting a double. Use DecimalFormat.
Use this to display the lat/lon points in your test:
DecimalFormat formatter = new DecimalFormat("0.0000000");
String lat = formatter.format(test.getLatitudeE6() / 1E6);
String lon = formatter.format(test.getLongitudeE6() / 1E6);
toAddress.setText(lat + " " + lon);

Categories

Resources