📜  如何在Android的Google地图上绘制轨迹?

📅  最后修改于: 2021-05-13 15:14:08             🧑  作者: Mango

在许多android应用中,我们已经看到从源位置到目标位置之间有一个路线标记。在本文中,我们将介绍如何在Android的Google Maps上绘制轨迹。

我们将在本文中构建什么?

我们将构建一个简单的应用程序,在其中将显示两个EditText字段,在这些字段中,我们必须输入源位置和目标位置,并且将显示一个按钮以显示路线。单击该按钮后,我们将在Google Maps上显示该位置的路线以及时间。下面提供了一个示例视频,以使您对我们在本文中将要做的事情有个大概的了解。注意,我们将使用Java语言实现该项目。

分步实施

步骤1:创建一个新项目

要在Android Studio中创建新项目,请参阅如何在Android Studio中创建/启动新项目。请注意,选择Java作为编程语言。

步骤2:使用activity_main.xml文件

导航到应用程序> res>布局> activity_main.xml,然后将以下代码添加到该文件中。以下是activity_main.xml文件的代码。

XML


  
    
    
  
    
    
  
    
    


Java
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
  
import androidx.appcompat.app.AppCompatActivity;
  
public class MainActivity extends AppCompatActivity {
  
    // creating variables for 
    // edit texts and button.
    EditText sourceEdt, destinationEdt;
    Button trackBtn;
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
          
        // initializing our edit text and buttons
        sourceEdt = findViewById(R.id.idEdtSource);
        destinationEdt = findViewById(R.id.idEdtDestination);
        trackBtn = findViewById(R.id.idBtnTrack);
  
        // adding on click listener to our button.
        trackBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // calling a method to draw a track on google maps.
                drawTrack(sourceEdt.getText().toString(), destinationEdt.getText().toString());
            }
        });
    }
  
    private void drawTrack(String source, String destination) {
        try {
            // create a uri
            Uri uri = Uri.parse("https://www.google.co.in/maps/dir/" + source + "/" + destination);
              
            // initializing a intent with action view.
            Intent i = new Intent(Intent.ACTION_VIEW, uri);
              
            // below line is to set maps package name
            i.setPackage("com.google.android.apps.maps");
              
            // below line is to set flags
            i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
              
            // start activity
            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
            i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
             
            // to start activity
            startActivity(i);
        }
    }
}


步骤3:使用MainActivity。 Java文件

转到MainActivity。 Java文件并参考以下代码。下面是MainActivity的代码。 Java文件。在代码内部添加了注释,以更详细地了解代码。

Java

import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
  
import androidx.appcompat.app.AppCompatActivity;
  
public class MainActivity extends AppCompatActivity {
  
    // creating variables for 
    // edit texts and button.
    EditText sourceEdt, destinationEdt;
    Button trackBtn;
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
          
        // initializing our edit text and buttons
        sourceEdt = findViewById(R.id.idEdtSource);
        destinationEdt = findViewById(R.id.idEdtDestination);
        trackBtn = findViewById(R.id.idBtnTrack);
  
        // adding on click listener to our button.
        trackBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // calling a method to draw a track on google maps.
                drawTrack(sourceEdt.getText().toString(), destinationEdt.getText().toString());
            }
        });
    }
  
    private void drawTrack(String source, String destination) {
        try {
            // create a uri
            Uri uri = Uri.parse("https://www.google.co.in/maps/dir/" + source + "/" + destination);
              
            // initializing a intent with action view.
            Intent i = new Intent(Intent.ACTION_VIEW, uri);
              
            // below line is to set maps package name
            i.setPackage("com.google.android.apps.maps");
              
            // below line is to set flags
            i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
              
            // start activity
            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
            i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
             
            // to start activity
            startActivity(i);
        }
    }
}

运行您的应用程序,然后查看该应用程序的输出。

输出:

想要一个节奏更快,更具竞争性的环境来学习Android的基础知识吗?
单击此处,前往由我们的专家精心策划的指南,以使您立即做好行业准备!