2014-06-30 3 views
1

Мне нужен следующий код для загрузки в webview.Как показать google map navigation url в 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); 

Как это сделать?

+0

Это не будет открывать URL в WebView. Если он будет работать, пользователь может выбрать одно из установленных приложений/браузеров. – greenapps

ответ

9

Это позволит загрузить URL в ваш 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:

Вот некоторые скриншоты, используя код, приведенный выше. Google Map Navigation в веб-просмотра:

Directions

Map

Edit:

активность:

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

Планировка:

<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> 
+0

Показывает пустую страницу. – Kirthiga

+0

@Kirthiga Вы добавили в манифест следующее разрешение: todd

+0

да, я добавил интернет-разрешение. Ваши скриншоты выглядят как загрузка в браузере. Мне нужно для загрузки навигационного url в webview. – Kirthiga

Смежные вопросы