Ganesh Gadekar
Ganesh Gadekar

Reputation: 21

Get Gps Location Using Android

I want to get latitude and Longitude from the following Android code.

location = locationManager.getLastKnownLocation(provider);

Here i am getting error location is null.

Here is the code:

package com.p;
import android.app.Activity;
import android.os.Bundle;
import android.os.Bundle;

import android.content.Context;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.TextView;
 import android.widget.Toast;
 public class GooglemapActivity extends Activity implements LocationListener {
  private TextView latituteField;
        private TextView longitudeField; 
   private LocationManager locationManager;
    private String provider;


 /** Called when the activity is first created. */

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    latituteField = (TextView) findViewById(R.id.TextView02);
    longitudeField = (TextView) findViewById(R.id.TextView04);

    // Get the location manager
    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    // Define the criteria how to select the locatioin provider -> use
    // default
    Criteria criteria = new Criteria();
    provider = locationManager.getBestProvider(criteria, false);
    Location location = locationManager.getLastKnownLocation(provider);

    // Initialize the location fields
    if (location != null) {
        System.out.println("Provider " + provider + " has been selected.");
        int lat = (int) (location.getLatitude());
        int lng = (int) (location.getLongitude());
        latituteField.setText(String.valueOf(lat));
        longitudeField.setText(String.valueOf(lng));
    } else {
        latituteField.setText("Provider not available");
        longitudeField.setText("Provider not available");
    }
}

/* Request updates at startup */
@Override
protected void onResume() {
    super.onResume();
    locationManager.requestLocationUpdates(provider, 400, 1, this);
}

/* Remove the locationlistener updates when Activity is paused */
@Override
protected void onPause() {
    super.onPause();
    locationManager.removeUpdates(this);
}

public void onLocationChanged(Location location) {
    int lat = (int) (location.getLatitude());
    int lng = (int) (location.getLongitude());
    latituteField.setText(String.valueOf(lat));
    longitudeField.setText(String.valueOf(lng));
}

public void onStatusChanged(String provider, int status, Bundle extras) {
    // TODO Auto-generated method stub

}

public void onProviderEnabled(String provider) {
    Toast.makeText(this, "Enabled new provider " + provider,
            Toast.LENGTH_SHORT).show();

}

public void onProviderDisabled(String provider) {
    Toast.makeText(this, "Disabled provider " + provider,
            Toast.LENGTH_SHORT).show();
}
}  

I have added the following permisions:

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

But I am getting the following error:

Provider not available

Upvotes: 0

Views: 11480

Answers (5)

HawleyTronics
HawleyTronics

Reputation: 1

I think you may want to add the following permission as well, since I am pretty sure that the FINE relies on the COARSE...

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>

Upvotes: 0

ehanoc
ehanoc

Reputation: 2217

Think you need to specify from which provider you want to receive updates.

You have two options:

  • GPS_PROVIDER
  • NETWORK_PROVIDER

You're probably missing something like this:

locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this);

Upvotes: 0

Antony
Antony

Reputation: 603

You haven't define a valid Criteria. Define that as follows

    Criteria criteria = new Criteria();
    criteria.setAccuracy(Criteria.ACCURACY_FINE);
    criteria.setAltitudeRequired(false);//true if required
    criteria.setBearingRequired(false);//true if required
    criteria.setCostAllowed(true);
    criteria.setPowerRequirement(Criteria.POWER_LOW);
    provider = locationManager.getBestProvider(criteria, true);//search for enabled provider

Upvotes: 1

lenik
lenik

Reputation: 23498

  1. have you switched on GPS in Settings -> Location ?
  2. why do you use (int) (location.getLatitude()) -- latitude/longitude is a float

Upvotes: 0

Ollie C
Ollie C

Reputation: 28509

If the last known location is null, it's because the phone has no last known location. If there is no location cached, you need to request an updated location, either via network, or GPS. Note that this process can take time, so must be done asynchronously.

You need to read this document:

http://developer.android.com/guide/topics/location/obtaining-user-location.html

I also recommend reading this:

http://android-developers.blogspot.co.uk/2011/06/deep-dive-into-location.html

Upvotes: 1

Related Questions