-1

My question is exactly the same as this question: How to add your application to the "share this place" list in Google maps But unfortunately this question doesn't really help me..

I want to get the long/lat when i share a location in google maps to my app.for now my app appears in the list for sharing locations but i have no idea what to do next.How can i bring the longitude or latitude after sharing the place to my app?

zyngot
  • 275
  • 1
  • 4
  • 19

1 Answers1

0

For anyone who is looking for the answer: First of all: in your mainfest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.yourapp" android:versionCode="1"
    android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".YourApp" android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
            <intent-filter>
                <action android:name="android.intent.action.SEND"></action>
                <category android:name="android.intent.category.DEFAULT" />
                <data android:mimeType="text/plain" />
            </intent-filter>
        </activity>
    </application>
    <uses-sdk android:minSdkVersion="4" />
</manifest>

Simply add the SEND intent filter just as it is above to your activity. The google maps sharing simply performs a "SEND" intent with the mime type of "text/plain". If you register an intent filter for that type, then your app will show up in the list.

Now for getting the coordinates just add this in your MainActivity.java:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Intent intent = getIntent();
        String action = intent.getAction();
        String type = intent.getType();

         if (Intent.ACTION_SEND.equals(action) && type != null) {
        if ("text/plain".equals(type)) {
            handleSendText(intent); // Handle text being sent
        }
    }
}


 void handleSendText(Intent intent) {
    String sharedText = intent.getStringExtra(Intent.EXTRA_TEXT);
    if (sharedText != null) {
        System.out.println(sharedText);

        Geocoder coder = new Geocoder(this);
        List<Address> address;

        try {

            address = coder.getFromLocationName(sharedText, 5);
            Address location = address.get(0);
           double lat =  location.getLatitude();
            double lng =  location.getLongitude();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

And your longitude and latitude is lat and lng

And here you go!

zyngot
  • 275
  • 1
  • 4
  • 19
  • How can I get coordinates in React native? this is what I got from google share: Object { "data": "MacDonald 0114 029 6313 https://maps.app.goo.gl/xUdcZKkXupinq9269", "mimeType": "text/plain", } – Bassel Turky May 14 '22 at 14:45
  • Hey @BasselTurky how are you receiving the send intent in your screen ? I tried Linking.addEventListener("url",handleEventChanges) but it's not working. I have setup whatever he has mentioned in the answer. And to answer you question you could use google maps sdk to reverse geocode the link – Madhav mishra Apr 27 '23 at 16:06