2013-03-19 66 views
0

我正在創建一個應用程序,它有一個按鈕,點擊後必須打開谷歌地圖。但是onclick其空白!我有使用意圖,有沒有其他的方式來加載谷歌地圖到我的應用程序?我的代碼有什麼問題?我錯過了什麼?我無法打開我的應用程序內的谷歌地圖

activity_main.xml中

<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=".MainActivity" > 
<Button 
    android:id="@+id/button1" 
    android:layout_width="100dp" 
    android:layout_height="100dp" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentTop="true" 
    android:layout_marginLeft="14dp" 
    android:text="MAPS" /> 


</RelativeLayout> 

MainaActivity.java

package com.stayconnected; 

import android.net.Uri; 
import android.os.Bundle; 
import android.app.Activity; 
import android.content.Context; 
import android.content.Intent; 
import android.view.Menu; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 

public class MainActivity extends Activity { 
    Button button1; 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     addListenerOnButton(); 
    } 
     public void addListenerOnButton() { 

      final Context context = this; 

      button1 = (Button) findViewById(R.id.button1); 

      button1.setOnClickListener(new OnClickListener() { 

       @Override 
       public void onClick(View arg0) { 

        Intent intent = new Intent(context, MainActivity2.class); 
           startActivity(intent); 



       } 

      }); 


    } 


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

} 

MainActivity.java

package com.stayconnected; 

import android.app.Activity; 
import android.content.Intent; 
import android.net.Uri; 
import android.os.Bundle; 
import android.view.Menu; 

public class MainActivity2 extends Activity { 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     setContentView(R.layout.activity_main2); 
     String url = "http://maps.google.com/maps?saddr=" + "9982878"+","+"76285774"+"&daddr="+"9992084"+","+"76286455"; 
     Intent intent1 = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse(url)); 
     startActivity(intent1); 
    } 
    @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; 
    } 


} 

AndroiManifest.xml

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.stayconnected" 
    android:versionCode="1" 
    android:versionName="1.0" > 

    <uses-sdk 
     android:minSdkVersion="8" 
     android:targetSdkVersion="17" /> 

    <application 
     android:allowBackup="true" 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme" > 
     <activity 
      android:name="com.stayconnected.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> 
     <activity 
      android:name="com.stayconnected.MainActivity2" 
      android:label="@string/app_name" > 
      </activity> 
    </application> 
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> 
<uses-permission android:name="android.permission.INTERNET" /> 

</manifest> 

我必須下載谷歌地圖關鍵還是什麼?因爲我在互聯網上閱讀,在我的應用程序活動中加載谷歌地圖,我必須下載密鑰,然後才能使用地圖!這就是爲什麼我有這個問題?

+0

停止使用您的完整的問題文本**粗體文字**!它意味着_highlight_部分... – WarrenFaith 2013-03-19 17:30:54

+0

你有清單文件的權限嗎?你在手機或模擬器上測試嗎? – Raghunandan 2013-03-19 17:36:26

回答

1
String url = "http://maps.google.com/maps?saddr=" + "9982878"+","+"76285774"+"&daddr="+"9992084"+","+"76286455"; 
    Intent intent1 = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse(url)); 
    startActivity(intent1); 

如果你使用上面的你的操作系統將找到可用的應用程序來顯示地圖。我在三星Galaxy S3上測試了上述內容。我可以選擇使用默認的手機瀏覽器,Chrome和Google Maps。所有的應用程序已經安裝在我的手機上。

它可能是一個瀏覽器或手機可用的地圖。但是這會從您的活動中打開一個新應用程序。

如果你想通過定製在你的應用程序中顯示地圖,然後使用谷歌地圖API可用於Android。

http://www.youtube.com/watch?v=dSqLKIow7sY。您可以按照教程獲取Google地圖API密鑰。如果您使用Google地圖API在手機上顯示地圖,則需要API密鑰。

<uses-permission android:name="android.permission.INTERNET"/>//permission in manifest 

後你會得到你的map.xml關鍵粘貼鍵

<com.google.android.maps.MapView 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/mapview" 
android:layout_width="fill_parent" 
android:layout_height="350dp" 
android:clickable="true" 
android:apiKey="paste your key here" //paste key 
/> 

您也可以使用網頁視圖。這和打開瀏覽器來顯示地圖一樣好。

public class MainActivity extends Activity { 

WebView wv; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    wv= (WebView) findViewById(R.id.wv); 
    wv.getSettings().setPluginsEnabled(true); 
    WebSettings webSettings = wv.getSettings(); 
    webSettings.setJavaScriptEnabled(true); 
    wv.loadUrl("http://maps.google.com/maps?saddr=" + "9982878"+","+"76285774"+"&daddr="+"9992084"+","+"76286455"); 
} 
} 

XML佈局

<LinearLayout 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" 
tools:context=".MainActivity" > 

<WebView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/wv"></WebView> 
</LinearLayout> 
+0

你是否嘗試過以上?我嘗試了你的意圖顯示地圖的方式。它適用於我的手機。 – Raghunandan 2013-03-19 18:02:20