GPS location returns only null (0.0.0.0)

Asked

Viewed 647 times

2

I have an application for Android devices, with several addresses. By clicking on any address, I wanted to take the user’s current location and play link google maps http://maps.google.com/maps?&saddr=", together with the destination coordinates.

However, the current "latitude" and "longitude" results only return "0.0".

Can someone help me?

public class Guaruja extends Activity implements LocationListener {

Button bttela2;
TextView TextView1;
TextView t1;
Uri uri;
    protected LocationManager locationManager;
    protected LocationListener locationListener;
    protected Context context;
String provider;
double latitude, longitude;




@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.guaruja);



    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);


    TextView1 = (TextView) findViewById (R.id.TextView1);



    TextView1.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {



            String lati = String.valueOf(latitude);
            String longi = String.valueOf(longitude);
            String firt = "http://maps.google.com/maps?&saddr=";
            String secord="&daddr=-23.990624, -46.282269";
            String virgula=",";
            String url= firt+lati+virgula+longi+secord;
               uri=Uri.parse(url);
               Intent intent = new Intent(Intent.ACTION_VIEW, uri);
               startActivity(intent);
        }


    });



}


public void onLocationChanged(Location location){

  if(location!= null){
       latitude = location.getLatitude() * 1E6;
       longitude = location.getLongitude() * 1E6;

}
    }

EDITED


I spent several days "stuck" in this code, because always the location returned me 0.0 and threw me in the Pacific Ocean. I managed to solve it, although I didn’t use the tracking code itself. As you can see in the code above, I used the Google Maps URL to take the user to the map with the coordinates set by me. After a search on this URL, I found a solution that worked very well for me, I used the link like this: "http://maps.google.com/maps?&saddr=&daddr=-23.990624,-46.282269&sensor=TRUE", i.e., I just left the starting coordinates blank ("&saddr=").

Thank you all for your help.

  • Is testing on a real device or emulator/VM?

  • On a real device. Thank you.

  • Testing Google Maps, does it work on the device? Testing on another?

  • google maps works normal to find my location. I tested on another device, the result is also null.

  • Has there been any progress? It could include what permissions of Location this using?

  • <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCES_COARSE_LOCATION"/> <uses-permission android:name="android.permission.ACCES_FINE_LOCATION"/>

  • Please take a look at the answer given in this question: http://stackoverflow.com/questions/7188105/locationmanagers-location-in-android-returns-null. The author of the answer suggests using another Provider (network) as the gps does not return data.

  • I edited my post with code that worked perfectly for what I needed. Thank you so much for your help.

  • @user3825655 Put your solution as a response and mark it as a solution :)

  • I did that, thanks for the tip!

Show 5 more comments

3 answers

0

locationManager.requestLocationUpdates(Locationmanager.GPS_PROVIDER, 1000, 1, this); lets you greatly improve your application.

0


I spent several days "stuck" in this code, because always the location returned me 0.0 and threw me in the Pacific Ocean. I managed to solve it, although I didn’t use the tracking code itself. As you can see in the code above, I used the Google Maps URL to take the user to the map with the coordinates set by me. After a search on this URL, I found a solution that worked very well for me, I used the link like this: "http://maps.google.com/maps?&saddr=&daddr=-23.990624,-46.282269&sensor=TRUE", i.e., I just left the starting coordinates blank ("&saddr=").

Thank you all for your help.

0

You need to wait for the GPS to calibrate. Creates a loading structure that alerts you after the onLocationChanged(Location location) is different from null. Example:

public void onLocationChanged(Location location){


    if(location!= null){
       latitude = location.getLatitude()*1E6;
       longitude = location.getLongitude()*1E6;
       dialog.dismiss();// Aqui você fecha o dialog. O gps foi calibrado
    }
  • Thanks for the reply Douglas. But even though I put the "if", the return is still "0.0" for both latitude and longitude. Which is odd, because the "if" condition is for when "Location" is different from null. Can you see any problems in my code?

  • Thanks for the reply Douglas. But even though I put the "if", the return is still "0.0" for both latitude and longitude. Which is odd, because the "if" condition is for when "Location" is different from null. Can you see any problems in my code?

  • I don’t see the implementation of Locationlistener in the class Guaruja.java Maybe that’s the problem. This example works perfectly :D http://javapapers.com/android/get-current-location-in-android/

  • I have tried to use "Locationlistener" in several ways, including the example you sent me, but it returns an error in the expression "this". I’m 2 days into this fight :s. I was already programming for iPhone (Objectivec), but I don’t have much experience with java. Thanks for the help friend.

  • Dude, if you can show the class that you’re making the mistake maybe I can help more. vlw

  • Hello Douglas, I edited my code in my question. It is no longer giving error, but it continues to return null. I thank you already.

  • Do a test, turn on Google Maps to move it until it finds your location... after that, test your application and see if it’s still null.

  • Done. In google maps, I found my location normally. But in my application it still returns null.

  • I edited my post with code that worked perfectly for what I needed. Thank you so much for your help.

Show 4 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.