标签:String uri mWebView 选择 intent 坐标 address Android latng
效果图
准备工作
主要代码
布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".activity.MapSelectionActivity">
<include layout="@layout/nav_bar" />
<WebView
android:id="@+id/web_view"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
逻辑
切记别忘了申请定位权
String mUrl = "https://mapapi.qq.com/web/mapComponents/locationPicker/v/index.html?search=1&type=0&backurl=http://callback&key=你注册的app的key&referer=你注册的app的名字";
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
mWebView.setWebChromeClient(new WebChromeClient() {
public void onGeolocationPermissionsShowPrompt(String origin, GeolocationPermissions.Callback callback) {
callback.invoke(origin, true, false);
}
});
mWebView.getSettings().setGeolocationDatabasePath( MapSelectionActivity.this.getFilesDir().getPath() );
mWebView.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (!url.startsWith("http://callback")) {
view.loadUrl(url);
} else {
try {
//转utf-8编码
String decode = URLDecoder.decode(url, "UTF-8");
//转uri,然后根据key取值
Uri uri = Uri.parse(decode);
String[] latng=uri.getQueryParameter("latng").split(",");
String address = uri.getQueryParameter("name");//地址
if (address.equals("我的位置")){
address = uri.getQueryParameter("addr");
}
Intent intent = new Intent();//把数据返回到上个页面
intent.putExtra("address", address);
intent.putExtra("latitude",latng[0]);
intent.putExtra("longitude",latng[1]);
setResult(RESULT_OK, intent);
finish();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
return true;
}
});
mWebView.loadUrl(mUrl);
有疑问私信我gzh
标签:String,uri,mWebView,选择,intent,坐标,address,Android,latng 来源: https://blog.csdn.net/Y74364/article/details/122433011
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。