2013-04-21 7 views
6

Muszę przekazać wystąpienie klasy LatLng do innej intencji. Jak mam to zrobić? Oto kod.Jak wysłać instancję LatLng do nowego celu

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

Proszę, pomóżcie mi tutaj.

klasa Trasa:

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

To moja klasa trasy. Nie znam problemu. Pomóż mi tutaj. Wydaje się, że pakiet jest w porządku, ale podczas odbierania jest błąd.

Odpowiedz

18

użyć metody putParcelable na załączonym długość i szerokość geograficzną obiektu do Bundle:

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

Teraz dołączyć go do swojej intencji:

i.putExtra("bundle", args); 

Aby dostać go w swojej nowej działalności:

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

'fromPosition' i' toPosition' nie są 'int' .... są' LatLng'. – Doomsknight

+0

Dzięki! Naprawiono w edycji. – wangyif2

+0

Metoda getArguments() jest niezdefiniowana dla typu Routes –

Powiązane problemy