简体   繁体   English

从谷歌地图中的共享位置获取长/纬度到您的应用程序

[英]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..我的问题与这个问题完全相同: How to add your application to the "share this place" list in Google maps但不幸的是,这个问题并没有真正帮助我..

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:对于正在寻找答案的任何人:首先:在您的 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.只需将上面的 SEND 意图过滤器添加到您的活动中。 The google maps sharing simply performs a "SEND" intent with the mime type of "text/plain".谷歌地图共享只是使用“文本/纯文本”的 mime 类型执行“发送”意图。 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:现在要获取坐标,只需将其添加到您的 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而你的经纬度是lat and lng

And here you go!给你!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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