Banana
Banana

Reputation: 2443

Passing lat lng to another activity in Android

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

Answers (6)

Null Pointer Exception
Null Pointer Exception

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

Sathish Kumar J
Sathish Kumar J

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

ρяσѕρєя K
ρяσѕρєя K

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

Kelevandos
Kelevandos

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

Jay Rathod
Jay Rathod

Reputation: 11245

You should write your Intent while passing data as this.

  intent.putExtra("latitude", finalLatitude);
  intent.putExtra("longitude", finalLongitude);

Upvotes: 1

antonio
antonio

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

Related Questions