Reputation: 47
I want to pass latitude from one activity to another, but I am a bit confused… I have written the code below.
On button click:
lat = Double.toString(latitude);
// lat = latitude;
Intent intent = new Intent(MapsActivity.this,Restaurant_List.class);
intent.putExtra("lat", lat);
startActivity(intent);
I have declared lat
as String. latitude
is the variable double which already has the value. So I am passing String…
And on next page:
Intent intent = getIntent();
lat = intent.getDoubleExtra("lat", 19.666);
Of course I have added 19.66, that's why it will show that only. Otherwise if I don't put digits it shows error…
Or if I use getStringExtra()
, it shows the by default page whose lat doesn't match with the lat I am passing. I searched a lot on the Internet but didn't get answer.
Upvotes: 0
Views: 2370
Reputation: 712
its Very Simple to Send and Get The intent.
String lat = "10.12456521";
Intent intent=new Intent(MapsActivity.this,Restaurant_List.class);
intent.putExtra("lat",lat);
startActivity(intent);
and In your Next Activity.
String lat;
Intent intent = getIntent();
lat =intent.getStringExtra("lat");
and After you casting into double. Thanks
Upvotes: 0
Reputation: 816
String message = String.format("New Location \n Longitude: %1$s \n Latitude:%2$s",location.getLongitude(), location.getLatitude());
Intent intent=new Intent(MapsActivity.this,Restaurant_List.class);
intent.putExtra("lat",message);
startActivity(intent);
Hope this could help you.
Upvotes: 0
Reputation: 10687
lat
is a String value obtained after converting latitude
which is the actual double value. So, pass latitude
through the intent instead of lat
.
intent.putExtra("lat",latitude);
Edit: Since the above method is not working for you, try putting your double inside of a bundle.
Intent intent = new Intent(MapsActivity.this,Restaurant_list.class);
Bundle b = new Bundle();
b.putDouble("lat", latitude);
intent.putExtras(b);
startActivity(intent);
Then inside your other activity, do this
Bundle b = getIntent().getExtras();
double lat= b.getDouble("lat");
Upvotes: 1
Reputation: 710
Change it to String and pass
String latt=String.valueOf(lat);
Intent intent=new Intent(MapsActivity.this,Restaurant_List.class);
intent.putExtra("lat",latt);
startActivity(intent);
And in Restaurant_List class, retrieve the String and convert:
Double lat=Double.parseDouble(getIntent().getStringExtra("lat");
Upvotes: 0
Reputation: 43322
Since the LatLng
Object is Parcelable, you can pass it in a Bundle.
Add import in both Activities:
import com.google.android.gms.maps.model.LatLng;
First Activity:
Intent intent=new Intent(MapsActivity.this,Restaurant_List.class);
Bundle b = new Bundle();
b.putParcelable("location", new LatLng(latitude, longitude);
intent.putExtras(b);
startActivity(intent);
Second Activity:
Intent intent=getIntent();
LatLng latLng = intent.getParcelableExtra("location");
double lat = latLng.latitude;
double lon = latLng.longitude;
Upvotes: 1