如何在 Android 上透過 Intent 啟動 Google Maps 導航?
介紹
在 Android 應用中,很多時候商店或特定地點的地址會以圖示的形式顯示。點選該 Google 地圖圖示後,應用會重定向到 Google Maps 應用,使用者可以在其中檢視從其當前位置到商店位置的路線。本文將探討如何在 Android 上透過 Intent 啟動 Google Maps 導航。
實現
我們將建立一個簡單的應用,其中包含一個 TextView 用於顯示應用標題。之後,我們將建立兩個文字輸入欄位,用於獲取使用者的起點和終點位置。然後,我們顯示一個按鈕,該按鈕將啟動 Google Maps Intent 並繪製這兩個位置之間的路線。現在,讓我們轉向 Android Studio 建立一個新專案。
步驟 1:在 Android Studio 中建立新專案
導航到 Android Studio,如下面的螢幕截圖所示。在下面的螢幕截圖中,點選“新建專案”以建立一個新的 Android Studio 專案。
點選“新建專案”後,您將看到下面的螢幕。
在此螢幕中,我們只需選擇“空活動”並點選“下一步”。點選“下一步”後,您將看到下面的螢幕。
在此螢幕中,我們只需指定專案名稱。然後,包名將自動生成。
注意 - 確保將語言選擇為 Java。
指定所有詳細資訊後,點選“完成”以建立一個新的 Android Studio 專案。
專案建立完成後,我們將看到 2 個開啟的檔案,即 activity_main.xml 和 MainActivity.java 檔案。
步驟 2:使用 activity_main.xml
導航到 activity_main.xml。如果此檔案不可見,要開啟此檔案,請在左側窗格中導航到 app > res > layout > activity_main.xml 以開啟此檔案。開啟此檔案後,將以下程式碼新增到其中。程式碼中添加了註釋以詳細瞭解。
<?xml version="1.0" encoding="utf-8"?>
<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:orientation="vertical"
tools:context=".MainActivity">
<!-- on below line creating a text view for displaying the heading of our application -->
<TextView
android:id="@+id/idTVHeading"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginTop="100dp"
android:layout_marginEnd="10dp"
android:gravity="center"
android:padding="4dp"
android:text="Google Maps Direction Intent in Android"
android:textAlignment="center"
android:textColor="@color/black"
android:textSize="20sp"
android:textStyle="bold" />
<!-- on below line creating edit text for source location -->
<EditText
android:id="@+id/idEdtSourceLocation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/idTVHeading"
android:layout_margin="10dp"
android:hint="Enter Source Location" />
<!-- on below line creating edit text for destination location -->
<EditText
android:id="@+id/idEdtDestinationLocation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/idEdtSourceLocation"
android:layout_margin="10dp"
android:hint="Enter Destination Location" />
<!-- on below line creating a button to show direction on maps-->
<Button
android:id="@+id/idBtnShowDirection"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/idEdtDestinationLocation"
android:layout_margin="10dp"
android:padding="4dp"
android:text="Show Direction on Maps"
android:textAllCaps="false" />
</RelativeLayout>
說明 - 在上面的程式碼中,我們建立一個根佈局作為相對佈局。在此佈局內,我們建立一個 TextView 用於顯示應用標題。之後,我們建立兩個 EditText 欄位,用於獲取使用者的起點和終點位置。然後,我們建立一個按鈕,用於開啟 Google Maps Intent 並在地圖應用上繪製路線。
步驟 3:使用 MainActivity.java 檔案
導航到 MainActivity.java。如果此檔案不可見,要開啟此檔案,請在左側窗格中導航到 app > res > layout > MainActivity.java 以開啟此檔案。開啟此檔案後,將以下程式碼新增到其中。程式碼中添加了註釋以詳細瞭解。
package com.example.java_test_application;
import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
// creating variables for edit text on below line.
private EditText sourceEdt, destinationEdt;
// creating variable for button on below line.
private Button directionBtn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// initializing variables for edit text on below line.
sourceEdt = findViewById(R.id.idEdtSourceLocation);
destinationEdt = findViewById(R.id.idEdtDestinationLocation);
// initializing variables for button on below line.
directionBtn = findViewById(R.id.idBtnShowDirection);
// adding click listener for button on below line.
directionBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (sourceEdt.getText().toString().isEmpty() && destinationEdt.getText().toString().isEmpty()) {
// displaying toast message to enter source and destination location.
Toast.makeText(MainActivity.this, "Please enter source and destination location..", Toast.LENGTH_SHORT).show();
} else {
// on below line calling open maps intent method to open maps intent and passing source and destination location to it.
openMapsIntent(sourceEdt.getText().toString(), destinationEdt.getText().toString());
}
}
});
}
// on below line creating an open maps intent method.
private void openMapsIntent(String source, String destination) {
try {
// create a uri on below line and passing a google maps url to open with source and destination location.
Uri uri = Uri.parse("https://www.google.co.in/maps/dir/" + source + "/" + destination);
// initializing a intent with action view and uri on below line.
Intent i = new Intent(Intent.ACTION_VIEW, uri);
// below line is to set maps package name for google maps.
i.setPackage("com.google.android.apps.maps");
// below line is to set flags to create a new task for google maps.
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// start activity on below line.
startActivity(i);
} catch (ActivityNotFoundException e) {
// when the google maps is not installed on users device
// we will redirect our user to google play to download google maps.
Uri uri = Uri.parse("https://play.google.com/store/apps/details?id=com.google.android.apps.maps");
// initializing intent with action view.
Intent i = new Intent(Intent.ACTION_VIEW, uri);
// set flags on below line.
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// on below line we are starting the activity.
startActivity(i);
}
}
}
說明:在上面的程式碼中,首先我們為按鈕和 EditText 建立變數。現在,我們將看到 onCreate 方法。這是每個 Android 應用的預設方法。當應用檢視建立時,會呼叫此方法。在此方法內,我們設定內容檢視,即名為 activity_main.xml 的佈局檔案,以從該檔案設定 UI。在 onCreate 方法內,我們使用我們在 activity_main.xml 檔案中指定的 id 初始化按鈕和 EditText 的變數。之後,我們為按鈕新增一個點選監聽器。在點選監聽器內,我們首先檢查起點和終點 EditText 是否為空。如果為空,則顯示一條吐司訊息,提示使用者輸入起點和終點位置。如果起點和終點 EditText 不為空,則呼叫 openMapsIntent 方法,我們向其中傳遞起點和終點位置以開啟 Google Maps Intent。
現在,在 openMapsIntent 方法中,我們建立一個 uri,我們在其中傳遞起點和終點位置。之後,我們建立一個 Intent,我們在其中設定 Google Maps 應用的包名。然後,我們呼叫 startActivity 方法以啟動 Google Maps 應用。同時,我們將其放入 try 和 catch 塊中。在 catch 塊中,我們檢查使用者裝置上是否未安裝 Google Maps 應用。在這種情況下,我們將開啟 Google Play 應用並將使用者重定向到 Google Maps 應用安裝頁面以安裝 Google Maps 應用。
新增上述程式碼後,我們只需點選頂部欄中的綠色圖示即可在移動裝置上執行應用。
注意 - 確保您已連線到您的真實裝置或模擬器。
輸出
結論
在本文中,我們探討了如何在 Android 應用中透過 Intent 啟動 Google Maps 導航。
資料結構
網路
關係型資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP