java - 从谷歌地图中的共享位置获取长/纬度到您的应用程序
问题描述
我的问题与这个问题完全相同: 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
和lng
给你!
推荐阅读
- winapi - 使用 SetFileInformationByHandle 重命名文件,但它被删除了
- r - 如何在 R vegan 中对 CAP/capscale 进行留一交叉验证?
- azure - 如何在 Xamarin Forms iOS-App 中接收 ASPN 令牌
- ios - 如何使用节点js在paypal的单笔交易中将金额转移到两个不同的商家?
- javascript - 如何在 Gatsby.js 中更改移动浏览器上的地址栏颜色
- jenkins - 在詹金斯的常规脚本中,调用目标引发了异常
- .net-core - 如何通过私有 NuGet 服务器将 API 密钥与 dotnet 还原一起使用
- ios - StoreKit:收据验证问题
- java-8 - java 8流分组和创建地图
> 问题 - android - 图像作为菜单项中的背景