android - 如何将 LatLng 实例发送到新 Intent

标签 android maps

我需要将 LatLng 类的实例传递给另一个 Intent 。我应该怎么做? 这是代码。

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

这是我的路线类。我不知道问题所在。帮我出去。似乎可以正常发送 bundle ,但在接收时出现错误。

最佳答案

使用 putParcelable 方法将 LatLng 对象附加到 Bundle:

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

现在将其附加到您的 Intent 上:

i.putExtra("bundle", args);

要在您的新 Activity 中获取它:

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

关于android - 如何将 LatLng 实例发送到新 Intent ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16134682/

相关文章:

android - 铃声选择器 - 单选按钮集

android - 制作华硕谷歌nexus 7模拟器

javascript - 传单:带有图层组的 .bindPopup 在所有功能的同一位置打开弹出窗口

java - 使用硬编码内联字符串或 Strings.xml 中的字符串更快

android - Editor.java IndexOutOfBoundsException offset(x) 应该小于 line limit(y)

android - Paint.getTextWidths。什么是提前宽度?

javascript - Angular/Ionic 谷歌地图关于状态变化

algorithm - 寻路应用算法

android - Flutter 如何让 uber 或 ola 像在谷歌地图上移动的汽车图标

JavaScript while 循环将每个数据一一传递到另一个函数中