How to send a LatLng instance to new intent
Asked Answered
M

3

8

I need to pass an instance of the LatLng class to another intent. How should I do it? Here's the code.

LatLng fromPosition = new LatLng(23.4555453556, 11.145315551);
LatLng toPosition = new LatLng(12.1115145311, 99.333455333);

Intent i= new Intent(Maps.this, Routes.class);
        startActivity(i);

Please help me out here.

Route class:

 public class Routes extends FragmentActivity {
GoogleMap mMap;
 GMapV2Direction md;
 private String provider;
 double lati;
 double longi;
 String name;
 Location location;

Document doc;
PolylineOptions rectLine;

Bundle bundle = getIntent().getParcelableExtra("bundle");
LatLng fromPosition = bundle.getParcelable("from_position");
LatLng toPosition = bundle.getParcelable("to_position");

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.maps2);

    md = new GMapV2Direction();
    mMap = ((SupportMapFragment)getSupportFragmentManager()
                    .findFragmentById(R.id.map)).getMap();

    LatLng coordinates = fromPosition;      
    mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(coordinates, 16));

    mMap.addMarker(new MarkerOptions().position(fromPosition).title("Start"));
    mMap.addMarker(new MarkerOptions().position(toPosition).title("End"));

    new ParseXML().execute();
}

private class ParseXML extends AsyncTask<Void, Void, Document> {         
  @Override
  protected Document doInBackground(Void... params) {
    doc = md.getDocument(fromPosition, toPosition,
    GMapV2Direction.MODE_DRIVING);
    ArrayList<LatLng> directionPoint = md.getDirection(doc);
    rectLine = new PolylineOptions().width(3).color(Color.RED);

    for (int i = 0; i < directionPoint.size(); i++) {
        rectLine.add(directionPoint.get(i));
    }
    return null;    
  }

   @Override
   protected void onPostExecute(Document result) {
            // TODO Auto-generated method stub
       mMap.addPolyline(rectLine);     
   }  
}
}

This is my route class. I don't know the problem. Help me out here. It seems to send the bundle fine but there's an error while receiving it.

Maynard answered 21/4, 2013 at 18:27 Comment(0)
Q
23

use the putParcelable method to attached LatLng Object to a Bundle:

Bundle args = new Bundle();
args.putParcelable("from_position", fromPosition);
args.putParcelable("to_position", toPosition);

Now attach it to your intent:

i.putExtra("bundle", args);

To get it in your new activity:

Bundle bundle = getIntent().getParcelableExtra("bundle");
LatLng fromPosition = bundle.getParcelable("from_position");
LatLng toPosition = bundle.getParcelable("to_position");
Quintile answered 21/4, 2013 at 18:34 Comment(16)
The method getArguments() is undefined for the type RoutesMaynard
Fixed, the previous method is used when you are creating a Fragment.Quintile
The method getParcelableExtra(String) in the type Intent is not applicable for the arguments (LatLng)Maynard
Fixed again, sorry for the iteration, I had to actually create a project and try it out.Quintile
It's still not working for me. Tell me what do the "FROM_POSITION" "TO_POSITION" define?Maynard
That is just a string you can define, it is equivalent to the key in a HashMap. Define them to "from_position" and "to_position".Quintile
I'm such a rookie I don't even know how to do that :/Maynard
check again, i removed the constant reference, should work now as is.Quintile
Checked! It doesn't give any errors but I got a nullpointer exception run-time.Maynard
On this line > Bundle bundle = getIntent().getParcelableExtra("bundle");Maynard
Intent i= new Intent(Maps.this, Routes.class); Bundle args = new Bundle(); args.putParcelable("from_position", fromPosition); args.putParcelable("to_position", toPosition); i.putExtra("bundle", args); startActivity(i); and then in the next class: Bundle bundle = getIntent().getParcelableExtra("bundle"); LatLng fromPosition = bundle.getParcelable("from_position"); LatLng toPosition = bundle.getParcelable("to_position");Maynard
Change your intent to new Intent(this, Routes.class);Quintile
Does your Route class extends Activity?Quintile
it extends FragmentActivityMaynard
post your Route class in your original question.Quintile
Okay, the problem is clear, put Bundle bundle = getIntent().getParcelableExtra("bundle"); LatLng fromPosition = bundle.getParcelable("from_position"); LatLng toPosition = bundle.getParcelable("to_position"); in your onCreate Method. Please accept the answer if it worksQuintile
G
2

Much easier way, since LatLng is parcelable:

on caller side:

LatLng position = new LatLng(16.099108, -22.812924); // Boa Vista
intent.putExtra("Pos", position);

On receiver side

LatLng position = getIntent().getExtras().getParcelable("Pos");
Grouping answered 29/6, 2018 at 10:16 Comment(0)
L
0

We have another alternative to get LatLng using intent in the activity

   intent.putExtra(Constants.LOCATION, latLng);

   LatLng latLng = getIntent().getParcelableExtra(Constants.LOCATION);
Lingulate answered 29/5, 2020 at 6:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.