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?
– Wakim
On a real device. Thank you.
– Rene Sá
Testing Google Maps, does it work on the device? Testing on another?
– Wakim
google maps works normal to find my location. I tested on another device, the result is also null.
– Rene Sá
Has there been any progress? It could include what permissions of
Location
this using?– Wakim
<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"/>
– Rene Sá
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.
– Wakim
I edited my post with code that worked perfectly for what I needed. Thank you so much for your help.
– Rene Sá
@user3825655 Put your solution as a response and mark it as a solution :)
– Matt S
I did that, thanks for the tip!
– Rene Sá