簡體   English   中英

從谷歌地圖中的共享位置獲取長/緯度到您的應用程序

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

我的問題與這個問題完全相同: How to add your application to the "share this place" list in Google 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();
        }
    }

而你的經緯度是lat and lng

給你!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM