2012-04-01 84 views
4

我有以下代码来显示网页视图:如何在android中的webview中显示本地谷歌地图?

webview.xml

<?xml version="1.0" encoding="utf-8"?> 
<WebView xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/webView1" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" /> 

WebViewActivity.java

public class WebViewActivity extends Activity { 

private WebView webView; 

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.webview); 

    webView = (WebView) findViewById(R.id.webView1); 
    webView.getSettings().setJavaScriptEnabled(true); 
    webView.setWebViewClient(new WebViewClient()); 
    webView.loadUrl("http://maps.google.com/maps?" +"saddr=43.0054446,-87.9678884" + "&daddr=42.9257104,-88.0508355"); 




} 

} 

我想在网页视图中打开下面的代码:

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); 

我该怎么做?

+0

你究竟想做什么,因为你的问题有点让我困惑 – Ishu 2012-04-01 07:26:37

+0

只是想打开webview中上面的代码的最后一部分! – captaindroid 2012-04-02 07:06:18

+0

嗨captainpirate,我有同样的问题。如果你已经解决了这个问题,请让我知道。 – AB1209 2012-04-05 13:34:35

回答

2

尝试......

private String url = "http://maps.google.com/maps"; 

    webView.setWebViewClient(new WebViewClient() { 
     @Override 
     public boolean shouldOverrideUrlLoading(WebView view, String url) { 

      if (url.equals("http://maps.google.com/maps")) { 

       Intent intent = new Intent(android.content.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); 
      } else { 
       //else part 
      } 

      return true; 
     } 
    }); 
+0

只显示一个白色屏幕! – captaindroid 2012-04-03 06:00:18

+1

您是否在Google地图API密钥和相关的XML代码从这里http://code.google.com/android/maps-api-signup.html – AnujAroshA 2012-04-03 06:50:33

+0

我不在这里使用MapActivity,为什么我需要Google Map API密钥?如果需要,我应该在哪里放置API密钥?我的完整课程代码如上。 – captaindroid 2012-04-03 08:34:30