Reputation: 3
I am developing an android application, which has one module called user location sharing. Though am able to share the location as a message, its not actually showing any coordinates i.e. latitude or longitude.
Whenever I try to share the location as a message I get "http://maps.google.com/maps?daddr=0.0.0.0"
Using location manager:
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, new LocationListener() {
@Override
public void onLocationChanged(Location location) {
double latitude = location.getLatitude();
double longitude = location.getLongitude();
LatLng latlng = new LatLng(latitude,longitude);
Geocoder geocoder = new Geocoder(getApplicationContext());
try {
List<Address> addressList = geocoder.getFromLocation(latitude,longitude,1);
String str = addressList.get(0).getLocality()+",";
str += addressList.get(0).getCountryName();
mMap.addMarker(new MarkerOptions().position(latlng).title(str));
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latlng,10.2f));
} catch (IOException e) {
e.printStackTrace();
}
}
I am using menu option format in the toolbar for sharing
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.item1:
Toast.makeText(this, "Sharing Location", Toast.LENGTH_SHORT).show();
String uri = "http://maps.google.com/maps?daddr=" +latitude+","+longitude;
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("text/plain");
share.putExtra(Intent.EXTRA_TEXT,uri);
startActivity(Intent.createChooser(share,"Share Location Via"));
return true;
}
return super.onOptionsItemSelected(item);
}
Upvotes: 0
Views: 73
Reputation: 38
The location wasn't update so it is initialized by default to 0.0.0.0.
So you must get the last location:
Add these lines
LocationManager locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
double latitude = location.getLatitude();
double longitude = location.getLongitude();
Before
String uri = "http://maps.google.com/maps?daddr=" +latitude+","+longitude;
Upvotes: 0