13

I need to share location from my app using the share intent activity, I have gone through some examples and know how to implement share intent. However I am stuck at setType. I need my application to share location details as well the users location on a map.

By the way i copied a users code with a very similar question"no offence"

Any assistance would be really appreciated.

Intent intent1 = new Intent(Intent.ACTION_SEND); 
intent1.setType("text/plain"); 
intent1.putExtra(Intent.EXTRA_TEXT, "The status update text"); 
startActivity(Intent.createChooser(intent1, "Select prefered Service")); 
Adil Soomro
  • 37,609
  • 9
  • 103
  • 153

3 Answers3

15

Here is the code to fire an intent to the map with a location:

String uri = "geo:" + latitude + ","
                    +longitude + "?q=" + latitude
                    + "," + longitude;
startActivity(new Intent(android.content.Intent.ACTION_VIEW,
                    Uri.parse(uri)));
Tiago Almeida
  • 14,081
  • 3
  • 67
  • 82
Ankit Thakkar
  • 422
  • 1
  • 4
  • 10
  • 2
    Thanks a lot but its not what i wanted, your code opens up google maps. i dont want to start google maps i just need my app to be able to share my current location more like google maps share location – Princewill Obinna Iwuorie Oct 01 '12 at 07:13
  • 1
    Just adding a note for anybody taking this example and running with it. If you customize the "q" param, make sure you build the Uri safely: `String uri = Uri.Builder().scheme("geo").appendPath(lat +","+ lng).appendQueryParameter("q", name).build();` – Joel Rosenberg Nov 14 '14 at 09:11
  • You need to use `Intent.createChooser` to open an selection dialog with all apps that support that intent. See other answer. – IgniteCoders May 07 '18 at 09:48
10
Double latitude = user_loc.getLatitude();
Double longitude = user_loc.getLongitude();

String uri = "http://maps.google.com/maps?saddr=" +latitude+","+longitude;

Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
sharingIntent.setType("text/plain");
String ShareSub = "Here is my location";
sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, ShareSub);
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, uri);
startActivity(Intent.createChooser(sharingIntent, "Share via"));
darkosj
  • 119
  • 1
  • 3
0

for Kotlin Users

val uri = "https://www.google.com/maps/?q=$latitude,$longitude"
        val sharingIntent = Intent(Intent.ACTION_SEND)
        sharingIntent.type = "text/plain"
        sharingIntent.putExtra(Intent.EXTRA_TEXT, uri)
        startActivity(Intent.createChooser(sharingIntent, "Share in..."))
Kamran Khan
  • 408
  • 2
  • 10