GPS Data Flow problem

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

GPS Data Flow problem

Postby ArtworkAD » Thu Jul 29, 2010 8:17 pm

Hi,

I have a really basic problem working with gps. I implemented the following class:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. private LocationManager lm;    
  2.         public MapsActivity SystemService;
  3.        
  4.         MapController mc;
  5.        
  6.         private GeoPoint p;
  7.        
  8.         private double mLongitude;
  9.         private double mLatitude;
  10.        
  11.         public GeoDataGateway(MapsActivity  sservice){
  12.                 this.SystemService = sservice;
  13.                 this.startLocationService();
  14.         }
  15.        
  16.         public void startLocationService(){
  17.                 this.lm = (LocationManager) this.SystemService.getSystemService(Context.LOCATION_SERVICE);
  18.                 this.lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 1, this);
  19.         }
  20.        
  21.         public String getCity(GeoPoint pp){
  22.                 Geocoder geoc = new Geocoder(SystemService.getBaseContext(), Locale.getDefault());
  23.                 List<Address> addresses;
  24.                 String city = "";
  25.                 try {
  26.                         addresses = geoc.getFromLocation(pp.getLatitudeE6()/1E6, pp.getLongitudeE6()/1E6, 1);
  27.                     city = addresses.get(0).getLocality();
  28.                 } catch (IOException e) {
  29.                         e.printStackTrace();
  30.                 }  
  31.                 return city;
  32.         }
  33.  
  34.         public void onLocationChanged(Location location) {
  35.                 location = this.lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
  36.                 try {
  37.                         mLongitude = location.getLongitude();
  38.                         mLatitude = location.getLatitude();                                    
  39.                         Toast.makeText(this.SystemService.getBaseContext(),"Your Location:" + mLongitude + "," + mLatitude, Toast.LENGTH_SHORT).show();
  40.  
  41.                 } catch (NullPointerException e) {
  42.                         Log.i("Null pointer exception " + mLongitude + "," + mLatitude, null);
  43.                 }      
  44.         }      
  45.  
  46.         public void onProviderDisabled(String provider) {
  47.                 // TODO Auto-generated method stub
  48.                
  49.         }
  50.  
  51.         public void onProviderEnabled(String provider) {
  52.                 // TODO Auto-generated method stub
  53.                
  54.         }
  55.  
  56.         public void onStatusChanged(String provider, int status, Bundle extras) {
  57.                 // TODO Auto-generated method stub
  58.                
  59.         }
  60.  
Parsed in 0.036 seconds, using GeSHi 1.0.8.4


that contains certain methods that are invoked when gps location/status changes.
My question is, how is it possible to use the gps data outside the GeoDataGateway class?

For example, it is possible to pass a mapView instance to GeoDataGateway and within the onLocationChanged mehtod
e.g. show the users current position.

but everytime I want to pass lnt and lat to an other class instance the application crashes. E.g. I want the city of the current user position, put the city into a variable and use it in another class instance, just do things like: String city = geodgateway.getCityFromCurrentLocaion();

I worked on it the whole day-.-

When I request a location status of the user, there should be a module thats starts on request and returns the location and terminates.

hope you can help me a bit
ArtworkAD
Junior Developer
Junior Developer
 
Posts: 19
Joined: Thu Jul 29, 2010 7:56 pm

Top

Re: GPS Data Flow problem

Postby blundell » Fri Jul 30, 2010 8:33 am

Is this a service class? I don't really know your design but once your service has got it's lat long, you could start an Intent to open your activity and add the lat long into the bundle of extra's.

If this is just a class, you could make sure it is initilised then create static methods to return your lat long.

If this is an activity you can make the lat long global variables for other sub-class's to use.
User avatar
blundell
Master Developer
Master Developer
 
Posts: 1610
Joined: Tue Nov 18, 2008 12:58 pm
Location: UK

Re: GPS Data Flow problem

Postby ArtworkAD » Fri Jul 30, 2010 8:59 am

for now my project has just 2 classes:

public class GeoDataGateway implements Location Listener

and

public class MapsView extends MapActivity

MapsView inits the map and starts an instance of GeoDataGatway. A instance of MapsView is then passed to GeoDataGateway and through this Instance I can point in the onLocationChanged method e.g. to the current location on the map.

the idea with static mehtods sounds interesting. what to your really mean by that?
ArtworkAD
Junior Developer
Junior Developer
 
Posts: 19
Joined: Thu Jul 29, 2010 7:56 pm

Re: GPS Data Flow problem

Postby blundell » Fri Jul 30, 2010 9:14 am

Hmm I'm not sure on your implementation of GeoDataGateway as I haven't played with GoogleMaps much.
But since you say you instantiate your class you could just change these variables to public.
public double mLongitude;
public double mLatitude;

And then you can do:
Code: Select all
GeoDataGateway myGeoData = GeoDataGateway();
int lat = myGeoData.mLatitude;
if(lat != null){
  // Use latitude
}


A static method is one that works only on the data input and used within itself, so you could call it without the need for instantiation. Which I don't think is viable for GPS data from the android OS.
User avatar
blundell
Master Developer
Master Developer
 
Posts: 1610
Joined: Tue Nov 18, 2008 12:58 pm
Location: UK

Re: GPS Data Flow problem

Postby ArtworkAD » Fri Jul 30, 2010 9:48 am

well I actually did it this way and application crashed.
i can not access the attributes of geodatagateway this way...
ArtworkAD
Junior Developer
Junior Developer
 
Posts: 19
Joined: Thu Jul 29, 2010 7:56 pm

Re: GPS Data Flow problem

Postby blundell » Fri Jul 30, 2010 10:00 am

So what line does it crash on, instantiating GeoDataGateway, or when you attempt to use myGeoData.mLatitude did you do a null check like my code above?
User avatar
blundell
Master Developer
Master Developer
 
Posts: 1610
Joined: Tue Nov 18, 2008 12:58 pm
Location: UK

Top

Re: GPS Data Flow problem

Postby ArtworkAD » Fri Jul 30, 2010 10:20 am

ok, I use
GeoDataGateway g = new GeoDataGateway(ma);
if(g.mLongitude != 0){}

now it does not crash but g.mLongitude is empty because geodatagateway needs some secounds to set the attributes. now is it possible to make some kind of wait dialog?
ArtworkAD
Junior Developer
Junior Developer
 
Posts: 19
Joined: Thu Jul 29, 2010 7:56 pm

Re: GPS Data Flow problem

Postby blundell » Fri Jul 30, 2010 10:27 am

There's loads of forum threads discussing Threads to wait.

If you just want a quick hack to wait use:
Code: Select all
final ProgressDialog gettingWebPageDialog = ProgressDialog.show(YourClass.this, "", "Getting GPS", true);
new Thread() {
         public void run() {
            sleep(1000);
            gettingWebPageDialog.dismiss();
         }
      }.start();


You would be better writing some type of callback to get a message when the GPS co-ord's are ready instead of just sleeping for a guessed amount of time
User avatar
blundell
Master Developer
Master Developer
 
Posts: 1610
Joined: Tue Nov 18, 2008 12:58 pm
Location: UK

Re: GPS Data Flow problem

Postby ArtworkAD » Fri Jul 30, 2010 10:42 am

do you mean something like this:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. Handler updateHandler = new Handler() {
  2.         /** Gets called on every message that is received */
  3.         // @Override
  4.         public void handleMessage(Message msg) {
  5.                 switch (msg.what) {
  6.                 case UPDATE_LOCATION: {
  7.                         Log.i(TAG, "Updated location = " + mLatitude + " " + mLongitude);
  8.                         break;
  9.                 }
  10.                 }
  11.                 super.handleMessage(msg);
  12.         }
  13.  };
  14.  
Parsed in 0.033 seconds, using GeSHi 1.0.8.4


from http://2009.hfoss.org/Tutorial:Hello_Mapview,_with_GPS#Getting_GPS_Location_Updates
ArtworkAD
Junior Developer
Junior Developer
 
Posts: 19
Joined: Thu Jul 29, 2010 7:56 pm

Re: GPS Data Flow problem

Postby blundell » Fri Jul 30, 2010 10:50 am

Yes so whenever this handler is called it means the latitude has been updated and you can then use it.
User avatar
blundell
Master Developer
Master Developer
 
Posts: 1610
Joined: Tue Nov 18, 2008 12:58 pm
Location: UK

Re: GPS Data Flow problem

Postby ArtworkAD » Fri Jul 30, 2010 10:56 am

thanks it works...for now :D thanks for your help
ArtworkAD
Junior Developer
Junior Developer
 
Posts: 19
Joined: Thu Jul 29, 2010 7:56 pm

Re: GPS Data Flow problem

Postby blundell » Fri Jul 30, 2010 11:15 am

as an FYI your original problem is the number 1 error for java developers,
Will stand you in good stead if you remember all 10 of these ;-)
Top 10 Java Errors
User avatar
blundell
Master Developer
Master Developer
 
Posts: 1610
Joined: Tue Nov 18, 2008 12:58 pm
Location: UK

Re: GPS Data Flow problem

Postby ArtworkAD » Sat Jul 31, 2010 12:47 pm

hi again,

I am facing another problem.

...in MapsView extends MapActivity...
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. MapOverlay
  2. ...
  3. public boolean onTouchEvent(MotionEvent event, MapView mapView)
  4.         {  
  5.             if (event.getAction() == 1) {
  6.                 g = new GeoDataGateway(ma);
  7.                 return true;            
  8.             }else{
  9.                 return false;
  10.             }
  11.          }
  12. ...
  13.  
Parsed in 0.034 seconds, using GeSHi 1.0.8.4


by touching the display the app requests the current location....

I handle gps events in an update handler

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. case UPDATE_LOCATION: {
  2.                 Toast.makeText(getBaseContext(),"Your Location:" + g.getCurrentCity(), Toast.LENGTH_SHORT).show();
  3.                 if(te == null){
  4.                         geAdressesByLocation(g.getCurrentCity());
  5.                         Toast.makeText(getBaseContext(),"Adressess not downloaded yet", Toast.LENGTH_SHORT).show();
  6.                 }else{
  7.                         Toast.makeText(getBaseContext(),"Adresses downloaded", Toast.LENGTH_SHORT).show();
  8.                 }
  9. }
  10.  
Parsed in 0.036 seconds, using GeSHi 1.0.8.4


te is a instance of a HashMap that is set to null, geAdressesByLocation(g.getCurrentCity()) starts a async task that downloads the adresses from my mysql database and return a HashMap..

don't mind the parameter, it is the action part of an url
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. DataDownloader test = new DataDownloader();
  2. test.download("get-adresses-by-city/city/Berlin", this);
  3.  
Parsed in 0.036 seconds, using GeSHi 1.0.8.4

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public class DataDownloader {
  2.  
  3.     public void download(String action, MapsActivity act) {
  4.             DataDownloaderTask task = new DataDownloaderTask(act);
  5.             task.execute(action);
  6.     }
  7. }
  8.  
Parsed in 0.036 seconds, using GeSHi 1.0.8.4


and finally the async task class
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public class DataDownloaderTask extends AsyncTask<String, Void, HashMap>{
  2.  
  3.         private final WeakReference<MapsActivity> mapActivityRef;
  4.        
  5.         public DataDownloaderTask(MapsActivity act){
  6.                 mapActivityRef = new WeakReference<MapsActivity>(act);
  7.         }
  8.        
  9.         @Override
  10.         protected HashMap doInBackground(String... params) {
  11.                 return JsonGrabber.receiveData(params[0]);
  12.         }
  13.        
  14.         protected void onPostExecute(HashMap data){
  15.                 if(isCancelled()){
  16.                         data = null;
  17.                 }
  18.                 if(mapActivityRef != null){
  19.                         MapsActivity mact = mapActivityRef.get();
  20.                         if(mact != null){
  21.                                 mact.te = data;
  22.                         }
  23.                 }
  24.         }
  25. }
  26.  
Parsed in 0.038 seconds, using GeSHi 1.0.8.4


well my problem is that when I tap on the screen once Hashmap is not returned, but whenn I tap twice HashMap is set...
ArtworkAD
Junior Developer
Junior Developer
 
Posts: 19
Joined: Thu Jul 29, 2010 7:56 pm

Re: GPS Data Flow problem

Postby blundell » Sat Jul 31, 2010 1:38 pm

Make a new Thread. This one is done. Please delete that post if you make a new Thread.
User avatar
blundell
Master Developer
Master Developer
 
Posts: 1610
Joined: Tue Nov 18, 2008 12:58 pm
Location: UK

Top

Return to Map Problems

Who is online

Users browsing this forum: No registered users and 6 guests