Editing string variables - android

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?

Related

do not concatenate text displayed with settext

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

any way to bind parameters of embedded type in an android room database query?

Let's say I have an entity with an embedded object:
public class Coordinates {
double latitude;
double longitude;
}
#Entity
public class Address {
String street;
#Embedded
Coordinates coordinates;
}
Defining a query with longitude and latitude as parameters like this:
#Query("SELECT * FROM Address WHERE longitude = :longitude AND latitude = :latitude")
public LiveData<List<String>> getStreet(double longitude, double latitude);
works.
But is there a way to use Coordinates (vs. longitude and latitude) as the query parameter? Tried this:
#Query("SELECT * FROM Address WHERE longitude = :c.longitude AND latitude = :c.latitude")
public String whatStreetAt(Coordinates c);
but it did not work (getting an error in Android Studio). Any idea?

storing Geopoints into Parse.com

I am trying to store my location into the parse.com database, but i have having an issue doing so. First of all, I can't store anything into the database I believe it is returning null or something so. Not quite sure whats the behavior. Can someone guide me in the right direction?
double longitude = location.getLongitude();
double latitude = location.getLatitude();
// Get longitude of the current location
// Create a LatLng object for the current location
LatLng latLng = new LatLng(latitude, longitude);
ParseObject parkingobject = new ParseObject("Cooking");
parkingobject.put("username","Alana");
ParseGeoPoint point = new ParseGeoPoint(latLng); //<----- This is where the error is.
parkingobject.put("Location", point);
double longitude = location.getLongitude();
double latitude = location.getLatitude();
ParseGeoPoint geoPoint = new ParseGeoPoint(latitude , longitude );
ParseObject parkingobject = new ParseObject("Cooking");
parkingobject.put("username","Alana");
parkingobject.put("Location", geoPoint );
Try this code.

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

Converting String to Double for use in LatLng/Google Maps

I need to convert a String value into a LatLng value for use in a GoogleMaps fragment in an Android app. The string value will likely come in the form of "-45.654765, 65.432892".
I've tried two different ways of doing this, and both have resulted in errors. First, I've tried using split() and putting the results into a String[], then accessing each using parseDouble(), as follows:
String[] geo = GEO.split(",");
double lati = Double.parseDouble(geo[0]);
double lngi = Double.parseDouble(geo[1]);
LOCATION = new LatLng(lati, lngi);
This yields an ArrayIndexOutOfBoundsException caused by double lati = Double.parseDouble(geo[0]);. I'm not really sure why.
I've also tried using StringTokenizer, as follows:
StringTokenizer tokens = new StringTokenizer(GEO, ",");
String lat = tokens.nextToken();
String lng = tokens.nextToken();
double lati = Double.parseDouble(lat);
double lngi = Double.parseDouble(lng);
LOCATION = new LatLng(lati, lngi);
This yields a NoSuchElementException pointing to String lng = tokens.nextToken();.
In both cases, the String I am working on, GEO, is public static final and passed from another activity via intent, where it is currently just hardcoded as "43.75,-70.15".
LOCATION is public static and is a LatLng variable initialized as null.
Can anyone point me in the right direction? This seems really simple so I'm even more confused than usual...
EDIT:
The data originates in a different activity where it is passed via intent. The activity that receives the intent has GEO defined as follows:
public static final String GEO = "geo";
And the intent from the previous activity puts geo in like this:
bundle.putString(PlaceActivity.GEO, geo);
You should have
String loc = getIntent().getExtras().getString(PlaceActivity.GEO);
String[] geo = loc.split(",");
You could just use a very basic substring:
int index = GEO.indexOf(",");
String lat = GEO.substring(0, index).trim();
String lng = GEO.substring(index+1).trim();
double lati = Double.parseDouble(lat);
double lngi = Double.parseDouble(lng);
LOCATION = new LatLng(lati, lngi);
Sorry, untested.

Categories

Resources