Passing Longitude and Latitude beetween activities

Put problem concerning MapActivities, MapViews, Overlays and GPS in this forum.

Passing Longitude and Latitude beetween activities

Postby reza » Fri Jan 28, 2011 9:34 am

Hi.
I have an activity where user enters an address and click a button.
On the button click, I want to pass the Longitude and Latitude of that address to another activity.
The second activity receives the Longitude and Latitude and displays the map.

The problem is, the second activity is not receiving the Longitude and Latitude.

My first activity:
Code: Select all
Button btnDrive=(Button)findViewById(R.id.btnDrive);
        btnDrive.setOnClickListener(new View.OnClickListener() {
         public void onClick(View v) {
            // TODO Auto-generated method stub
            String txtAddress=address.getText().toString();
            try{
               List<Address>foundAddresses=gc.getFromLocationName(txtAddress,1);
               if(foundAddresses.size()==0)
               {
                  Dialog locationError=new AlertDialog.Builder(NavigationActivity.this)
                  .setIcon(0)
                  .setTitle("Error")
                  .setPositiveButton(R.string.hello, null)
                  .setMessage("Sorry,your address does not exist in this world")
                  .create();
                  locationError.show();
               }
               else{
                                                       Address x=foundAddresses.get(0);
                     srchLat=x.getLatitude();
                     srchLon=x.getLongitude();
                                               
                                                Intent myIntent=new Intent();
                  myIntent.setClass(NavigationActivity.this,MapRouteActivity.class);
                  Bundle bundle=new Bundle();
                  bundle.putDouble("toLat", srchLat);
                  bundle.putDouble("toLon", srchLon);
                  myIntent.putExtras(bundle);
                  startActivity(myIntent);
                  
               }
               }catch(Exception ex){
                  //error message.
            }



My second activity is:
Code: Select all
public class MapRouteActivity extends MapActivity{
   LinearLayout linearLayout;
    MapView mapView;
    private Road mRoad;
   Bundle bundle=this.getIntent().getExtras();
   
    @Override
    public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.mapview);
            mapView = (MapView) findViewById(R.id.map_view);
            mapView.setBuiltInZoomControls(true);
                       

            new Thread() {
                    @Override
                    public void run() {
                            double fromLat = 55.6873750, fromLon = 12.5039390;
                           //double toLat = 55.7053041;
                            //double toLon = 12.5442230;
                           
                            double toLat=bundle.getDouble("toLat");
                            double toLon=bundle.getDouble("toLon");
                            String url = RoadProvider
                                            .getUrl(fromLat, fromLon, toLat, toLon);
                            InputStream is = getConnection(url);
                            mRoad = RoadProvider.getRoute(is);
                            mHandler.sendEmptyMessage(0);
                    }
            }.start();
    }


Any idea what may the problem be??
[I did not write the full code here rather I wrote the problem area for better understanding.]
reza
Freshman
Freshman
 
Posts: 9
Joined: Sat Jan 22, 2011 12:29 am

Top

Re: Passing Longitude and Latitude beetween activities

Postby JoxTraex » Sat Jan 29, 2011 2:54 am

Instead of using a bundle... (should be the same) put the information into the intent and then fire that intent. Once you are in the second Activity (On create) retrieve that intent:

Bundle b = getIntent().getExtras();

Then using this bundle try retrieve your information from this bundle, it should have your information.

My initial impression is that when the bundle is retrieve its retrieve even BEFORE it goes into the oncreate() which is where it receives the information.

I hope this helps.
JoxTraex
Developer
Developer
 
Posts: 25
Joined: Wed Jan 26, 2011 12:29 am

Top

Return to Map Problems

Who is online

Users browsing this forum: No registered users and 1 guest