如何在webview android中显示谷歌地图导航网址 [英] How to show google map navigation url in webview android
本文介绍了如何在webview android中显示谷歌地图导航网址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
final Intent intent = new Intent(Intent.ACTION_VIEW,
Uri.parse(
http://maps.google.com/maps?+
saddr = 43.0054446,-87.9678884+
& daddr = 42.9257104, - 88.0508355\" ));
intent.setClassName(
com.google.android.apps.maps,
com.google.android.maps.MapsActivity);
startActivity(intent);
我该如何做?
解决方案
这会将URL加载到webview中:
WebView webview =(WebView)findViewById (R.id.webView1);
webview.setWebViewClient(新的WebViewClient());
webview.getSettings()。setJavaScriptEnabled(true);
webview.loadUrl(http://maps.google.com/maps?+saddr = 43.0054446,-87.9678884+& daddr = 42.9257104,-88.0508355);
编辑:
以下是使用上述代码的截图。在网络视图中的Google地图导航:
编辑:
活动:
package com.example.webview;
导入android.app.Activity;
导入android.os.Bundle;
导入android.view.Menu;
导入android.webkit.WebView;
导入android.webkit.WebViewClient;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView webview =(WebView)findViewById(R.id.webView1);
webview.setWebViewClient(新的WebViewClient());
webview.getSettings()。setJavaScriptEnabled(true);
webview.loadUrl(http://maps.google.com/maps?+saddr = 43.0054446,-87.9678884+& daddr = 42.9257104,-88.0508355);
}
@Override
public boolean onCreateOptionsMenu(菜单菜单){
//膨胀菜单;这会将项目添加到操作栏(如果存在)。
getMenuInflater()。inflate(R.menu.main,menu);
返回true;
$ / code $ / pre
$ b 布局:
< RelativeLayout xmlns:android =http://schemas.android.com/apk/res/android
xmlns:tools =http: //schemas.android.com/tools
android:layout_width =match_parent
android:layout_height =match_parent
android:paddingBottom =@ dimen / activity_vertical_margin
android:paddingLeft =@ dimen / activity_vertical_margin
android:paddingRight =@ dimen / activity_horizontal_margin
android:paddingTop =@ dimen / activity_vertical_margin
tools:context =com.example .webview.MainActivity $ PlaceholderFragment>
< WebView
android:id =@ + id / webView1
android:layout_width =match_parent
android:layout_height =match_parent
android:layout_alignParentLeft =true
android:layout_alignParentTop =true/>
< / RelativeLayout>
清单:
<?xml version =1.0encoding =utf-8?>
< manifest xmlns:android =http://schemas.android.com/apk/res/android
package =com.example.webview
android:versionCode = 1
android:versionName =1.0>
< uses-sdk
android:minSdkVersion =8
android:targetSdkVersion =19/>
<使用权限android:name =android.permission.INTERNET/>
< application
android:allowBackup =true
android:icon =@ drawable / ic_launcher
android:label =@ string / app_name
android:theme =@ style / AppTheme>
< activity
android:name =com.example.webview.MainActivity
android:label =@ string / app_name>
< intent-filter>
< category android:name =android.intent.category.LAUNCHER/>
< / intent-filter>
< / activity>
< / application>
< / manifest>
I need the following code to load in webview.
final Intent intent = new Intent(Intent.ACTION_VIEW,
Uri.parse(
"http://maps.google.com/maps?" +
"saddr=43.0054446,-87.9678884" +
"&daddr=42.9257104,-88.0508355"));
intent.setClassName(
"com.google.android.apps.maps",
"com.google.android.maps.MapsActivity");
startActivity(intent);
How do I do this?
解决方案 This will load the URL to your webview:
WebView webview = (WebView) findViewById(R.id.webView1);
webview.setWebViewClient(new WebViewClient());
webview.getSettings().setJavaScriptEnabled(true);
webview.loadUrl("http://maps.google.com/maps?" + "saddr=43.0054446,-87.9678884" + "&daddr=42.9257104,-88.0508355");
Edit:
Here are some screenshots using the code above. Google Map Navigation in a web view:
Edit:
Activity:
package com.example.webview;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView webview = (WebView) findViewById(R.id.webView1);
webview.setWebViewClient(new WebViewClient());
webview.getSettings().setJavaScriptEnabled(true);
webview.loadUrl("http://maps.google.com/maps?" + "saddr=43.0054446,-87.9678884" + "&daddr=42.9257104,-88.0508355");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.webview.MainActivity$PlaceholderFragment" >
<WebView
android:id="@+id/webView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" />
</RelativeLayout>
Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.webview"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.webview.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
这篇关于如何在webview android中显示谷歌地图导航网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文