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=").