Hi

This is what I tried based on the suggestions in the below chain mail

MyLocationListener myListener = new MyLocationListener();
LocationManager myManager =  
(LocationManager)getSystemService(LOCATION_SERVICE);
myManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000,  
0, myListener);

This is the LocationListener class

public class MyLocationListener implements LocationListener {

   private static double latitude;
   private static double longitude;

   @Override
   public void onLocationChanged(Location arg0) {
     latitude = arg0.getLatitude();
     longitude = arg0.getLongitude();
   }

   @Override
   public void onProviderDisabled(String provider) {

     LocationManager myManager =  
(LocationManager)delegate.getSystemService(Context.LOCATION_SERVICE);
     LocationProvider name = myManager.getProvider("gps");
     if(name != null){
       Location lastGps = myManager.getLastKnownLocation("gps");
       if(lastGps == null){
         latitude = 0.0;
         longitude = 0.0;
       }
       else{
         latitude = lastGps.getLatitude();
         longitude = lastGps.getLongitude();
       }
     }
   }

   @Override
   public void onProviderEnabled(String provider) {
   }

   @Override
   public void onStatusChanged(String provider, int status, Bundle  
extras) {}

   public static double getLatitude() {
     return latitude;
   }

   public static double getLongitude() {
     return longitude;
   }

}

I waited for as long as 30 seconds but none of the listener methods  
were called. I am testing on a G1 with 1.5 SDK.
Can someone please tell me whats wrong with the code? Thanks



On 01-Jun-09, at 11:08 PM, Mark Murphy wrote:

>
>> I know this has been discussed over and over now but I just got it
>> workign and it works perfectly on the emulator when I mock the
>> location with long and lat however, I am still getting NULL in the
>> location when I run the code on my phone.
>
> I'm pretty sure I answered your question when you asked it a few  
> days ago.
>
>> here's the main code
>>
>> ...
>>
>> // Get my current location
>>       locationListener = new MyLocationListener();
>>       LocationManager manager = (LocationManager) getSystemService
>> (LOCATION_SERVICE);
>>       manager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
>> 0, 0,locationListener);
>>       Location loc = manager.getLastKnownLocation("gps");
>
> loc will generally be null at this point.
>
> In the real world, GPS takes time to get a fix, perhaps tens of  
> seconds,
> perhaps never depending on where the device is. You only asked for the
> radio to be turned on in the previous statement -- the fix will not be
> ready one line later in your code.
>
>>       if(loc != null) {
>>          latPoint = manager.getLastKnownLocation("gps").getLatitude
>> ();
>>          lngPoint = manager.getLastKnownLocation("gps").getLongitude
>> ();
>>
>>          // Create a point of it
>>          GeoPoint point = new GeoPoint((int) (latPoint * 1E6), (int)
>> (lngPoint * 1E6));
>>          // Animate the map to the location
>>          mc.animateTo(point);
>>          mc.setZoom(20);
>>          mapView.setSatellite(true);
>>          mapView.invalidate();
>>
>>
>>       } else {
>>
>>               latPoint = 111.1111111; // means location is Null
>>       }
>
> You will need to delay this work until you get your first fix, such  
> as by
> putting it in your locationListener. And you will need to deal with  
> the
> possibility that you will never get a fix, either because GPS is  
> disabled,
> or because the device is someplace where it cannot get a fix.
>
> -- 
> Mark Murphy (a Commons Guy)
> http://commonsware.com
> _The Busy Coder's Guide to Android Development_ Version 2.0 Available!
>
>
>
> >


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to