How to send a LatLng instance to new intent

前端 未结 3 1585
野的像风
野的像风 2021-01-05 09:16

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.14         


        
相关标签:
3条回答
  • 2021-01-05 09:55

    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");
    
    0 讨论(0)
  • 2021-01-05 10:06

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

       intent.putExtra(Constants.LOCATION, latLng);
    
       LatLng latLng = getIntent().getParcelableExtra(Constants.LOCATION);
    
    0 讨论(0)
  • 2021-01-05 10:09

    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");
    
    0 讨论(0)
提交回复
热议问题