The call to requestLocationUpdates is where you've got trouble. The
first argument should be the name of the provider sending you updates
(GPS or cell network). To get regular updates from GPS, you want
something like the following:

        LocationManager locMgr = (LocationManager)
getSystemService(Context.LOCATION_SERVICE);
        LocationListener locListener = new LocationListener()
        {
            public void    onLocationChanged(Location location)
                {
                         if (location != null)
                         {
                                  Toast.makeText(getBaseContext(),
                                      "New location latitude [" +
location.getLatitude() +
                                      "] longitude [" +
location.getLongitude()+"]",
                                      Toast.LENGTH_SHORT).show();
                         }
                }
                public void    onProviderDisabled(String provider)
                {
                }
                public void    onProviderEnabled(String provider)
                {
                }
                public void    onStatusChanged(String provider,
int status, Bundle extras)
                {
                }
        };
        locMgr.requestLocationUpdates(
            LocationManager.GPS_PROVIDER,
            0,        // minTime in ms
            0,        // minDistance in meters
            locListener);

In other words, you setup a callback handler to receive the update
messages (locListener in this case), and you tell the LocationManager
to send updates to that callback. You can call getLastKnownLocation
with a provider name (like GPS_PROVIDER) but the value returned could
be null if there is no last known location. Note that the zeros in
that last call really should be reasonable values; I only use zeros as
part of a demonstration.

We cover this topic in our new book "Pro Android 2" in chapter 7, and
go into much more detail on working with the location providers as
well as overlays and maps.

- dave
www.androidbook.com

On Apr 7, 8:43 am, ckloch <ckl...@gmail.com> wrote:
> As part of an application for registrer whether a vehicle can pass the
> next traffic light without having stop for red light, I am trying to
> get the GPS Location.
>
> So far I have tried the code below, but the HTC Tattoo telephone keeps
> stopping the application.
> The reason is that the List providers remains empty.
>
> Is there any way that I can extend my code, so I can understand why
> providers remains empty.
> I know I get a NullPointerException, but I don't know why as GPS is
> enabled.
>
> I will really appreciate your help on how to get
> - status of the GPS
> - content of providers.
>
> The code is listed below (I don't get any compilation errors):
>
> protected void onResume() {
>         List providers;
>         final String tag = "readdata";
>
>         super.onResume();
>
>         locationManager = (LocationManager)
> getSystemService(Context.LOCATION_SERVICE);
>         providers = locationManager.getProviders(true);
>
>         if (!providers.isEmpty()) {
>          location = locationManager.getLastKnownLocation((String)
> providers
>             .get(0));
>          locationManager.requestLocationUpdates((String)
> providers.get(0),
>              15000, 1, this);
>          }
>          GeoPoint p = new GeoPoint((int) (location.getLatitude() * 1E6),
>             (int) (location.getLongitude() * 1E6));
>
>              mc.animateTo(p);
>              mc.setZoom(17);
>              mapView.setSatellite(true);
>              mapView.setStreetView(true);
>              mapView.invalidate();
>
>     }

-- 
You received this message because you are subscribed to the Google
Groups "Android Beginners" group.

NEW! Try asking and tagging your question on Stack Overflow at
http://stackoverflow.com/questions/tagged/android

To unsubscribe from this group, send email to
android-beginners+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-beginners?hl=en

To unsubscribe, reply using "remove me" as the subject.

Reply via email to