0

クリックするとGoogleマップを開く必要があるボタンを持つアプリケーションを作成しています。しかしonclick、その空白!! インテントを使用しましたが、Google マップをアプリケーションにロードする他の方法はありますか? コードの何が問題になっていますか? 私は何が欠けていますか?

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>

Google マップ キーなどをダウンロードする必要がありますか? アプリケーション アクティビティで Google マップを読み込むにはインターネットで読んだので、キーをダウンロードする必要があります。そうして初めてマップが機能します。それが私がこの問題を抱えている理由ですか??

4

1 に答える 1

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

上記を使用すると、OS はマップを表示するために利用可能なアプリを見つけます。上記をsamsung galaxy s3でテストしました。デフォルトの電話ブラウザ、クロム、マップから選択するオプションがありました. すべてのアプリはすでに私の電話にインストールされていました。

電話で使用できるブラウザまたはマップの場合があります。ただし、これにより、アクティビティから新しいアプリケーションが開きます。

カスタマイズしてアプリケーションに地図を表示したい場合は、android で利用可能な google map api を使用します。

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
/>

Web ビューを使用することもできます。これは、ブラウザを開いて地図を表示するのと同じです。

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>
于 2013-03-19T17:17:27.823 に答える