Reputation: 2443
I am trying to enable the user to long click on a map and, instead of creating a marker, the new activity should open. It should contain latitude and longitude, i.e. the location on which the user clicked.
I know data transfer is easy when I use dialogs, but I don't want to use dialog fragment in this part of my app.
MapActivity:
//Add marker on long click
final double finalLongitude = longitude;
final double finalLatitude = latitude;
mMap.setOnMapLongClickListener(new GoogleMap.OnMapLongClickListener() {
// start SendMessageActivity need to add marker to message activity
Intent intent = new Intent(getActivity(), DisplayLocationActivity.class);
intent.putExtra("latitude",""+ finalLatitude);
intent.putExtra("longitude", "" + finalLongitude);
startActivity(intent);
}
DisplayLocationActivity:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_set_location);
Bundle bundle = getIntent().getExtras();
double lat = bundle.getDouble("latitude");
double lng = bundle.getDouble("longitude");
//display lat lng
mLatDisplay = (TextView) findViewById(R.id.latTextViewDisplay);
mLangDisplay = (TextView) findViewById(R.id.lngTextViewDisplay);
mLatDisplay.setText(String.valueOf(lat));
mLangDisplay.setText(String.valueOf(lng));
}
My problem is this - when I start the app and click on the map, the newly opened activity displays the lat and lng as 0.0 no matter where on the map I clicked.
Upvotes: 3
Views: 7531
Reputation: 1583
In my case I am doing like this. First activity for sendinf intent...
LatLng center = mMap.getCameraPosition().target;
intent.putExtra("Latlng", center);
Second Activity.
Intent intent;
LatLng latlong =intent.getIntent().getExtras().getParcelable("Latlng");
Now you can get Latitude and Longitude from latlong Object
latlong.getlatitude;
latlong.getlongitude
Upvotes: 2
Reputation: 4345
Try this,
intent.putExtra("lat","" finalLatitude);
intent.putExtra("lng", "" finalLongitude);
then get like,
double lat = Double.valueOf(bundle.getString("lat"));
double lng = Double.valueOf(bundle.getString("lng"));
Thanks.
Upvotes: 1
Reputation: 132982
As see here LatLng:
public final class LatLng extends Object implements Parcelable
Means Latlng
class implements Parcelable
interface, so we can pass it direclty using Intent.putExtra
like:
@Override
public void onMapLongClick(LatLng arg) {
intent.putExtra("Latlng", arg);
..
}
and get it in next Activity using getExtras().getParcelable
like:
LatLng objLatLng=getIntent().getExtras().getParcelable("Latlng");
Now use objLatLng
to show location coordinates in TextView's.
Upvotes: 9
Reputation: 7082
Change the read code in the receiving activity to this:
Bundle bundle = getIntent().getExtras();
double lat = Double.valueOf(bundle.getString("latitude"));
double lng = Double.valueOf(bundle.getString("longitude"));
Or pass the Extras as doubles in the first place :-)
Upvotes: 1
Reputation: 11245
You should write your Intent
while passing data as this.
intent.putExtra("latitude", finalLatitude);
intent.putExtra("longitude", finalLongitude);
Upvotes: 1
Reputation: 18242
You are passing the data as String
but retrieving it as double
.
Change
intent.putExtra("latitude",""+ finalLatitude);
intent.putExtra("longitude", "" + finalLongitude);
for
intent.putExtra("latitude", finalLatitude);
intent.putExtra("longitude", finalLongitude);
Upvotes: 4