Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problem using GPS_PROVIDER for location

Tags:

android

gps

I am developing an Android app which uses the current user location for result.

I am using the following code to get the location:

LocationManager locationManager =
    (LocationManager) getSystemService(LOCATION_SERVICE);

Location currentLocation =
    locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

When I run it on device it gives me a null value for location.

But when I use the NETWORK_PROVIDER it works fine is there any extra setting for the device to use GPS?

My device is Galaxy Tab P-1000.

like image 831
Mahesh Avatar asked Aug 16 '11 07:08

Mahesh


2 Answers

Calling getLastKnownLocation() with GPS_PROVIDER returns a cached value. If there is no recent location it will return null. Use the LocationListener's onLocationChanged() callback instead.

It may take a while but you will get a fresh position fix.

NETWORK_PROVIDER's location is not always accurate because it is based on cell-site triangulation

like image 164
Reno Avatar answered Oct 12 '22 09:10

Reno


Use:

locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
                MINIMUM_TIME_BETWEEN_UPDATES,
                MINIMUM_DISTANCE_CHANGE_FOR_UPDATES, locationListener);

For locationListener you may use this:

LocationListener locationListener = new LocationListener() {
        public void onLocationChanged(Location location) {
            // Called when a new location is found by the network location
            // provider.

            String message = String.format(
                    "New Location \n Longitude: %1$s \n Latitude: %2$s",
                    location.getLongitude(), location.getLatitude());
            Toast.makeText(LbsGeocodingActivity.this, message,
                    Toast.LENGTH_LONG).show();

            if (location != null) {
                GeoPoint point2 = new GeoPoint(
                        (int) (location.getLatitude() * 1E6), (int) (location
                                .getLongitude() * 1E6));

                // Toast.makeText(getBaseContext(),
                // "Latitude: " + location.getLatitude() +
                // " Longitude: " + location.getLongitude(),
                // Toast.LENGTH_SHORT).show();
                //                 
                mapView.getController().animateTo(point2);
                mapView.getController().setZoom(15);

                List<Overlay> mapOverlays = mapView.getOverlays();
                Drawable drawable = LbsGeocodingActivity.this.getResources()
                .getDrawable(R.drawable.new3pin);
                HelloItemizedOverlay itemizedoverlay = new HelloItemizedOverlay(
                        drawable, LbsGeocodingActivity.this);
                point = new GeoPoint((int) (location.getLatitude() * 1E6),
                        (int) (location.getLongitude() * 1E6));

                String address = convertPointToLocation(point);
                String delims = ",";
                String[] tokens = address.split(delims);
                OverlayItem overlayitem = null;
                if(tokens.length==1)
                {

                    overlayitem = new OverlayItem(point,
                            "New Location", "Locality:"+"\nCity:"+"\nCountry: "+tokens[0]);
                }
                else if(tokens.length==2)
                {

                    overlayitem = new OverlayItem(point,
                            "New Location", "Locality:"+"\nCity: "+tokens[0]+"\nCountry: "+tokens[1]);
                }
                else if(tokens.length==3)
                {

                    overlayitem = new OverlayItem(point,
                            "New Location", "Locality: "+tokens[0]+"\nCity: "+tokens[1]+"\nCountry: "+tokens[2]);
                }
                else
                {
                    overlayitem = new OverlayItem(point,
                            "New Location", address);
                }

                itemizedoverlay.addOverlay(overlayitem);
                mapOverlays.clear();
                mapOverlays.add(itemizedoverlay);

                // mapView.invalidate();
            }

        }

I have used this in my code and it works well.

like image 44
Mohit Verma Avatar answered Oct 12 '22 07:10

Mohit Verma