首页 > 解决方案 > 从谷歌地图中的共享位置获取长/纬度到您的应用程序

问题描述

我的问题与这个问题完全相同: How to add your application to the "share this place" list in Google maps 但不幸的是,这个问题并没有真正帮助我..

当我将谷歌地图中的位置共享到我的应用程序时,我想获得经度/纬度。现在我的应用程序出现在共享位置的列表中,但我不知道下一步该做什么。我怎样才能在之后带上经度或纬度将地点分享给我的应用程序?

标签: javaandroidgoogle-maps

解决方案


对于正在寻找答案的任何人:首先:在您的 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>

只需将上面的 SEND 意图过滤器添加到您的活动中。谷歌地图共享只是使用“文本/纯文本”的 MIME 类型执行“发送”意图。如果您为该类型注册了一个意图过滤器,那么您的应用程序将显示在列表中。

现在要获取坐标,只需在 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();
        }
    }

你的经度和纬度是latlng

给你!


推荐阅读