Skip to content Skip to sidebar Skip to footer

Get Gps Location Instantly Via Android App

all. I am writing an android app about GPS locations. I tried it on emulator and entered the latitude and longitude manually, and it worked fine. However, my problem is: on the rea

Solution 1:

The getLastKnownLocation method does not trigger an onLocationChanged event. One way to refactor your code would be to move the logic that acts on a Location to a separate method and then call that method both after you call getLastKnownLocation, and from your onLocationChanged method.

Bear in mind that there is no guarantee that getLastKnownLocation will provide a meaningful Location, since you the device might have moved since the last location update.

Example code:

publicvoidonCreate(Bundle savedInstanceState){
    ....
    Stringprovider= LocationManager.GPS_PROVIDER;
    Locationlocation= locationManager.getLastKnownLocation(provider);
    updateLocation(location);
}

publicclassMyLocationUpdaterimplementsLocationListener{ //change location interface@OverridepublicvoidonLocationChanged(Location location) {
        updateLocation(location);
    }
    ...
}

voidupdateLocation(Location location) {
    Doublelat= location.getLatitude();
    Doublelon= location.getLongitude();

    // the rest of the code from onLocationChanged
}

Solution 2:

getLastKnownLocation() is faster after a connection has been established with the GPS satellite. For the first time, it will return null, or no value till no connection is established. You can add a GpsListener to know when the location is obtained. Search about "how to get a gps fix" and you might get answer to your question

Solution 3:

I have same problem before..but I have got the solution..this is the simplest way to get location instantly.

publicclassLocationFinderextendsActivity {

    TextView textView1;
    Location currentLocation;
    double currentLatitude,currentLongitude;


    publicvoidonCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        textView1 = (TextView) findViewById(R.id.textView1);
        Log.i("@@@@@@@@@@ Inside LocationFinder onCreate", "LocationFinder onCreate");

        FindLocation();

    }

    publicvoidFindLocation() {
        LocationManager locationManager = (LocationManager) this
                .getSystemService(Context.LOCATION_SERVICE);

        LocationListener locationListener = newLocationListener() {
            publicvoidonLocationChanged(Location location) {
                updateLocation(location);

                Toast.makeText(
                        LocationFinder.this,
                        String.valueOf(currentLatitude) + "\n"
                                + String.valueOf(currentLongitude), 5000)
                        .show();

                }

            publicvoidonStatusChanged(String provider, int status,
                    Bundle extras) {
            }

            publicvoidonProviderEnabled(String provider) {
            }

            publicvoidonProviderDisabled(String provider) {
            }
        };
        locationManager.requestLocationUpdates(
                LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);

    }


    voidupdateLocation(Location location) {
            currentLocation = location;
            currentLatitude = currentLocation.getLatitude();
            currentLongitude = currentLocation.getLongitude();
            textView1.setText(String.valueOf(currentLatitude) + "\n"
                    + String.valueOf(currentLongitude));

        }
}

Don't forget to Give permission in Manifeast.

<uses-permissionandroid:name="android.permission.ACCESS_FINE_LOCATION" /><uses-permissionandroid:name="android.permission.ACCESS_MOCK_LOCATION" /><uses-permissionandroid:name="android.permission.ACCESS_COARSE_LOCATION" /><uses-permissionandroid:name="android.permission.INTERNET"/>

Solution 4:

publicclassHomeActivityextendsActivityimplementsLocationListener{
publicstatic Context mContext;
privatedouble latitude, longitude;
 public LocationManager mLocManager;
// *******This is the new Code start on 11/4/2011 at 3 o'clock/**
 * This is the Home Button if user Login then it is move to TennisAppActivity otherwise move to Login  
 *
 */
@Override
protectedvoidonCreate(Bundle savedInstanceState) {
    mContext=this;
    super.onCreate(savedInstanceState);
    setContentView(R.layout.homelayout);


    mLocManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

    mLocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,
            this);
    mLocManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0,
            0, this);
    locationUpdate();
    ((Button) this.findViewById(R.id.ButtonHome))
            .setOnClickListener(new OnClickListener() {
                publicvoidonClick(View arg0) {

                        startActivity(new Intent(HomeActivity.this,
                                DefaultDisplay.class));

                }
            });

    ((Button) this.findViewById(R.id.ButtonProfile))
            .setOnClickListener(new OnClickListener() {

                publicvoidonClick(View arg0) {
                    if (GUIStatics.boolLoginStatus) {
                        startActivity(new Intent(HomeActivity.this,
                                MyProfile.class));
                    } else {
                        Intent intent=new Intent(HomeActivity.this,
                                Login.class);
                        intent.putExtra("moveTo","MyProfile");
                        startActivity(intent);
                    }
                }
            });

    ((Button) this.findViewById(R.id.ButtonNotifications))
            .setOnClickListener(new OnClickListener() {

                publicvoidonClick(View arg0) {
                    if (GUIStatics.boolLoginStatus) {
                        startActivity(new Intent(HomeActivity.this,
                                ShowAllNotificationActiviry.class));
                    } else {
                        Intent intent=new Intent(HomeActivity.this,
                                Login.class);
                        intent.putExtra("moveTo","ShowAllNotificationActiviry");
                        startActivity(intent);
                    }
                }
            });

    ((Button) this.findViewById(R.id.ButtonFavorites))
            .setOnClickListener(new OnClickListener() {

                publicvoidonClick(View arg0) {
                    if (GUIStatics.boolLoginStatus) {
                        startActivity(new Intent(HomeActivity.this,
                                FavoritesActivity.class));
                    } else {
                        Intent intent=new Intent(HomeActivity.this,
                                Login.class);
                        intent.putExtra("moveTo","FavoritesActivity");
                        startActivity(intent);
                    }
                }
            });

            ((Button) this.findViewById(R.id.ButtonMore))
            .setOnClickListener(new OnClickListener() {
                publicvoidonClick(View arg0) {
                        startActivity(new Intent(HomeActivity.this,
                                MoreListActivity.class));
                }
            });

}

publicvoidlocationUpdate()
{
    CellLocation.requestLocationUpdate();
}


publicvoidgetAddress(double lat, double lng) {
    Geocoder geocoder = new Geocoder(HomeActivity.mContext, Locale.getDefault());
    try {
        List<Address> addresses = geocoder.getFromLocation(lat, lng, 1);
        Address obj = addresses.get(0);
        String add = obj.getAddressLine(0);
        GUIStatics.currentAddress = obj.getSubAdminArea() + ","
                + obj.getAdminArea();
        GUIStatics.latitude = obj.getLatitude();
        GUIStatics.longitude = obj.getLongitude();
        GUIStatics.currentCity= obj.getSubAdminArea();
        GUIStatics.currentState= obj.getAdminArea();
        add = add + "\n" + obj.getCountryName();
        add = add + "\n" + obj.getCountryCode();
        add = add + "\n" + obj.getAdminArea();
        add = add + "\n" + obj.getPostalCode();
        add = add + "\n" + obj.getSubAdminArea();
        add = add + "\n" + obj.getLocality();
        add = add + "\n" + obj.getSubThoroughfare();

        Log.v("IGA", "Address" + add);
        // Toast.makeText(this, "Address=>" + add,// Toast.LENGTH_SHORT).show();// TennisAppActivity.showDialog(add);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
    }
}



publicvoidonLocationChanged(Location location) {
    latitude = location.getLatitude();
    longitude = location.getLongitude();
    GUIStatics.latitude=location.getLatitude();
    GUIStatics.longitude= location.getLongitude();
    Log.v("Test", "IGA" + "Lat" + latitude + "   Lng" + longitude);
    //mLocManager.r

    getAddress(latitude, longitude);
    if(location!=null)
    {

    mLocManager.removeUpdates(this);
    }
    // Toast.makeText(this, "Lat" + latitude + "   Lng" + longitude,// Toast.LENGTH_SHORT).show();
}


publicvoidonProviderDisabled(String arg0) {
    // TODO Auto-generated method stub
    Toast.makeText(HomeActivity.this, "Gps Disabled", Toast.LENGTH_SHORT).show();
    Intent intent = new Intent(
            android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
    startActivity(intent);
}


publicvoidonProviderEnabled(String arg0) {
    // TODO Auto-generated method stub

}


publicvoidonStatusChanged(String arg0, int arg1, Bundle arg2) {
     if(arg1 == 
            LocationProvider.TEMPORARILY_UNAVAILABLE) { 
                                    Toast.makeText(HomeActivity.this, 
            "LocationProvider.TEMPORARILY_UNAVAILABLE", 
            Toast.LENGTH_SHORT).show(); 
                        } 
                        elseif(arg1== LocationProvider.OUT_OF_SERVICE) { 
                                    Toast.makeText(HomeActivity.this, 
            "LocationProvider.OUT_OF_SERVICE", Toast.LENGTH_SHORT).show(); 
                        } 

}

}

This is the code i have use it for finding the device location with the help of latitude and longitude and we also call getLastLocationUpdate in this code. I hope this is very help full to you.

Post a Comment for "Get Gps Location Instantly Via Android App"