如何将LatLng实例发送到新意图 [英] How to send a LatLng instance to new intent

查看:83
本文介绍了如何将LatLng实例发送到新意图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将LatLng类的实例传递给另一个意图.我该怎么办? 这是代码.

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);

请在这里帮助我.

路线类别:

 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.

推荐答案

使用putParcelable方法将LatLng对象附加到捆绑包中:

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");

这篇关于如何将LatLng实例发送到新意图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆