GPS location returns only null (0.0,0.0)

2

I have an Android app with multiple addresses. When you click on an address, you want to get the user's current location and play on link from google maps http://maps.google.com/maps?&saddr=" , along with destination coordinates.

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

Can anyone 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 the location always returned me 0.0 and I was playing in the Pacific Ocean. I managed to resolve it even though I did not use the location 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 defined by me. After a search on this URL, I found a solution that worked very well for me, I used the link like this: " , that is, I just left the starting coordinates blank (" & saddr=").

asked by anonymous 10.07.2014 / 15:36

3 answers

0

I spent several days "stuck" in this code, because the location always returned me 0.0 and I was playing in the Pacific Ocean. I managed to resolve it even though I did not use the location 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 defined by me. After a search on this URL, I found a solution that worked very well for me, I used the link like this: " , that is, I just left the starting coordinates blank (" & saddr=").

17.07.2014 / 18:48
0

You need to wait for the GPS to calibrate. Creates a loading structure that alerts you after 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
    }
    
10.07.2014 / 16:11
0

locationManager.requestLocationUpdates (LocationManager.GPS_PROVIDER, 1000, 1, this); leaves so it will greatly improve your application.

    
17.07.2014 / 14:53