简体   繁体   中英

Get long/lat from shared place in google maps to your app

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?

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM