user1722880
user1722880

Reputation: 69

Get current latitude and longitude in the string

I am trying to get latitude and longitude but sometime network is available but I am not getting value of latitude and longitude. I am using MyLocationListener class and put condition all but some time value is not getting.

protected void showCurrentLocation() 
{
    Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

    if (location != null) 
    {
        counter++;          
        latitude=location.getLatitude();
        longitude=location.getLongitude();
        altitude=location.getAltitude();
    }
 }   

private class MyLocationListener implements LocationListener
{
    @Override
    public void onLocationChanged(Location location)
    {
         counter++;
         latitude=location.getLatitude();
         longitude=location.getLongitude();
         altitude=location.getAltitude();
    }

    @Override
    public void onStatusChanged(String s, int i, Bundle b) 
    {
    }
    @Override
    public void onProviderDisabled(String s) 
    {   
    }
    @Override
    public void onProviderEnabled(String s) 
    {
    }
}

Upvotes: 2

Views: 3907

Answers (2)

Pratik Butani
Pratik Butani

Reputation: 62419

Here Best way to get Longitude Latitude:

/** PROCESS for Get Longitude and Latitude **/
locationManager     = (LocationManager) getSystemService(LOCATION_SERVICE);

// Define a listener that responds to location updates

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

        longitude = String.valueOf(location.getLongitude());
        latitude = String.valueOf(location.getLatitude());
        Log.d("msg", "changed Loc : "+longitude + ":"+latitude);
    }

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

    public void onProviderEnabled(String provider) {}

    public void onProviderDisabled(String provider) {}
};

// getting GPS status
isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);

// check if GPS enabled     
if(isGPSEnabled){

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

    if(location != null)
    {
        longitude = String.valueOf(location.getLongitude());
        latitude = String.valueOf(location.getLatitude());
        Log.d("msg", "Loc : "+longitude + ":"+latitude);

        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
    }else
    {
        /*
        Criteria criteria = new Criteria();
        criteria.setAccuracy(Criteria.ACCURACY_FINE);
        criteria.setCostAllowed(true);
        String provider = locationManager.getBestProvider(criteria, true);
         */

        location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

        if(location != null)
        {
            longitude = String.valueOf(location.getLongitude());
            latitude = String.valueOf(location.getLatitude());

            locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
        }else
        {
            longitude   = "0.00";
            latitude    = "0.00";
        }
    }
}

if device is not able to get currentLocation using GPS then it'll be take fron NetworkProvider or else it'll take 0,0 as my requirement but you can modify as per your requirement

May it'll helpful to you.

Happy Coding :D

Upvotes: 5

Jitender Dev
Jitender Dev

Reputation: 6925

See, this is a very common problem. In order to have exact Latitude and Longitude ,You must use GPS also it must me activated on the mobile device but GPS too have some limitations. GPS works most efficiently under open sky. you will not have a clear range inside a building. So try your code under open sky and check the response.

Upvotes: 0

Related Questions