谷歌地图中的方向

Posted

技术标签:

【中文标题】谷歌地图中的方向【英文标题】:directions in google map 【发布时间】:2021-10-02 02:14:07 【问题描述】:

我想在谷歌地图(android studio,java)中显示方向

package com.example.travel;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import android.Manifest;
import android.app.FragmentManager;
import android.content.DialogInterface;
import android.content.pm.PackageManager;
import android.location.Address;
import android.location.Geocoder;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;





import android.widget.Toast;

import com.directions.route.AbstractRouting;
import com.directions.route.Route;
import com.directions.route.RouteException;
import com.directions.route.Routing;
import com.directions.route.RoutingListener;
import com.example.travel.items.SavePathInput;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.maps.CameraUpdate;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.android.gms.maps.model.Polyline;
import com.google.android.gms.maps.model.PolylineOptions;
import com.google.maps.DirectionsApiRequest;
import com.google.maps.GeoApiContext;
import com.google.maps.PendingResult;
import com.google.maps.internal.PolylineEncoding;
import com.google.maps.model.DirectionsResult;
import com.google.maps.model.DirectionsRoute;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

import static androidx.constraintlayout.motion.utils.Oscillator.TAG;

public class MapActivity extends AppCompatActivity implements OnMapReadyCallback,RoutingListener,GoogleApiClient.OnConnectionFailedListener 

    private FragmentManager fragmentManager;
    private MapFragment mapFragment;
    private String email = MainActivity.useremail;

    private GoogleMap mMap;
    private Geocoder geocoder;
    private Button button , bt_searchpath , bt_savepath;
    private EditText editText;

    Polyline polyline = null;
    List<LatLng> latLngList = new ArrayList<>();
    private ArrayList<UserLocation> clickedPath = new ArrayList<>();


    private List<Polyline> polylines = null;

    private String place;

    private Retrofit retrofit;
    private RetrofitInterface retrofitInterface;
    public static String BASE_URL ;

    private GeoApiContext mGeoApiContext = null;


    @Override
    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_map);

        editText = (EditText) findViewById(R.id.editText);
        button=(Button)findViewById(R.id.button);
        bt_searchpath = (Button)findViewById(R.id.bt_pathsearch);
        bt_savepath = (Button)findViewById(R.id.bt_pathsave);

        retrofit = new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        retrofitInterface = retrofit.create(RetrofitInterface.class);

        Intent intent = getIntent();
        place = intent.getStringExtra("place");


        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);

        if(mGeoApiContext == null)
            mGeoApiContext = new GeoApiContext.Builder()
                    .apiKey(GoogleApikey)
                    .build();
        


        bt_searchpath.setOnClickListener(new View.OnClickListener() 
            @Override
            public void onClick(View v) 
                if(latLngList.size() >= 3) 
                    for (int i = 0; i < latLngList.size() - 2; i++) 
                        //calculateDirections(latLngList.get(i) , latLngList.get(i+1));
                    
                else if(latLngList.size() == 2)
                    calculateDirections(latLngList.get(0) , latLngList.get(1));
                else if(latLngList.size() == 1)
                    Toast.makeText(MapActivity.this, "click at least two places", Toast.LENGTH_SHORT).show();
                
            
        );

    

    private void calculateDirections(LatLng start , LatLng end)
        Log.d(TAG, "calculateDirections: calculating directions.");

        com.google.maps.model.LatLng destination = new com.google.maps.model.LatLng(
                end.latitude,
                end.longitude
        );
        DirectionsApiRequest directions = new DirectionsApiRequest(mGeoApiContext);

        directions.alternatives(true);
        directions.origin(
                new com.google.maps.model.LatLng(
                        start.latitude,
                        start.longitude
                )
        );
        Log.d(TAG, "calculateDirections: destination: " + destination.toString());
        directions.destination(destination).setCallback(new PendingResult.Callback<DirectionsResult>() 
            @Override
            public void onResult(DirectionsResult result) 
                Log.d(TAG, "onResult: routes: " + result.routes[0].toString());
                Log.d(TAG, "onResult: duration :" + result.routes[0].legs[0].duration);
                Log.d(TAG,  "onResult: distance :" +result.routes[0].legs[0].distance);
                Log.d(TAG, "onResult: geocodedWayPoints: " + result.geocodedWaypoints[0].toString());

                addPolylinesToMap(result);
            

            @Override
            public void onFailure(Throwable e) 
                Log.e(TAG, "onFailure: " + e.getMessage() );
            
        );
    
    private void addPolylinesToMap(final DirectionsResult result)
        new Handler(Looper.getMainLooper()).post(new Runnable() 
            @Override
            public void run() 
                Log.d(TAG, "run: result routes: " + result.routes.length);

                for(DirectionsRoute route: result.routes)
                    Log.d(TAG, "run: leg: " + route.legs[0].toString());
                    List<com.google.maps.model.LatLng> decodedPath = PolylineEncoding.decode(route.overviewPolyline.getEncodedPath());

                    List<LatLng> newDecodedPath = new ArrayList<>();

                    for(com.google.maps.model.LatLng latLng: decodedPath)


                        newDecodedPath.add(new LatLng(
                                latLng.lat,
                                latLng.lng
                        ));
                    
                    Polyline polyline = mMap.addPolyline(new PolylineOptions().addAll(newDecodedPath));
                    polyline.setColor(R.color.colorPrimaryDark);
                    polyline.setClickable(true);

                
            
        );
    
    @Override
    public void onMapReady(GoogleMap googleMap) 
        mMap = googleMap;
        geocoder = new Geocoder(this);

        button.setOnClickListener(new Button.OnClickListener()
            @Override
            public void onClick(View v)
                String str = editText.getText().toString();
                List<Address> addressList = null;
                try 
                    addressList = geocoder.getFromLocationName(
                            str,
                            10);
                
                catch (IOException e) 
                    e.printStackTrace();
                

                System.out.println(addressList.get(0).toString());

                String []splitStr = addressList.get(0).toString().split(",");
                String address = splitStr[0].substring(splitStr[0].indexOf("\"") + 1,splitStr[0].length() - 2); 
                System.out.println(address);

                String latitude = splitStr[10].substring(splitStr[10].indexOf("=") + 1); 
                String longitude = splitStr[12].substring(splitStr[12].indexOf("=") + 1); 
                System.out.println(latitude);
                System.out.println(longitude);


                LatLng point = new LatLng(Double.parseDouble(latitude), Double.parseDouble(longitude));

                MarkerOptions mOptions2 = new MarkerOptions();
                mOptions2.title(str);
                mOptions2.snippet(address);
                mOptions2.position(point);

                mMap.addMarker(mOptions2);
                mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(point,15));

                UserLocation location = new UserLocation(str , latitude , longitude);
                clickedPath.add(location);

                LatLng latLng = new LatLng(Double.parseDouble(latitude), Double.parseDouble(longitude));
                latLngList.add(latLng);

            
        );

        List<Address> addressList = null;
        try 
            addressList = geocoder.getFromLocationName(
                    place,
                    10);
        
        catch (IOException e) 
            e.printStackTrace();
        

        String []splitStr = addressList.get(0).toString().split(",");
        String latitude = splitStr[10].substring(splitStr[10].indexOf("=") + 1); 
        String longitude = splitStr[12].substring(splitStr[12].indexOf("=") + 1);
        LatLng startCity = new LatLng(Double.parseDouble(latitude), Double.parseDouble(longitude));
        mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(startCity,13));
    

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

    <uses-permission android:name="android:permission.INTERNET" />
    <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" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/travel"
        android:label="@string/app_name"
        android:roundIcon="@drawable/travel"
        android:supportsRtl="true"
        android:theme="@style/Theme.Travel"
        android:usesCleartextTraffic="true">
        <activity android:name=".SplashActivity">

        </activity>
        <activity android:name=".OtherPathActivity" />
        <activity android:name=".MapActivity" />
        <activity android:name=".UserPathActivity" />
        <activity android:name=".UserPlaceActivity" />
        <activity android:name=".MainActivity" />
        <activity android:name=".LoginActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <meta-data
            android:name="com.google.android.geo.API_KEY"
            android:value="my api key" />
        <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />
    </application>

</manifest>
plugins 
    id 'com.android.application'


android 
    compileSdkVersion 30
    buildToolsVersion "30.0.3"

    defaultConfig 
        applicationId "com.example.travel"
        minSdkVersion 26
        targetSdkVersion 30
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    

    buildTypes 
        release 
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        
    
    compileOptions 
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    


dependencies 

    implementation 'com.github.jd-alexander:library:1.1.0'
    implementation 'androidx.appcompat:appcompat:1.3.0'
    implementation 'com.google.android.material:material:1.4.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
    implementation 'com.google.android.gms:play-services-maps:17.0.1'
    implementation 'com.google.android.libraries.places:places:2.4.0'
    implementation 'com.google.maps.android:android-maps-utils:2.2.5'
    implementation 'com.google.android.gms:play-services-location:18.0.0'
    testImplementation 'junit:junit:4.+'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
    //google directions
    implementation 'com.google.maps:google-maps-services:0.18.2'
    implementation 'org.slf4j:slf4j-simple:1.7.25'

    // Retrofit
    implementation 'com.squareup.retrofit2:retrofit:2.6.2' //
    implementation 'com.squareup.retrofit2:converter-gson:2.6.2' 
    implementation 'com.google.code.gson:gson:2.8.5' //GSON 


    implementation 'com.github.john990:WaveView:v0.9'
    implementation 'com.github.bumptech.glide:glide:4.11.0'

在这个活动中,用户选择了两个以上的地方,这些地方被添加到 List latLngList 中。 单击“bt_searchpath”时,会调用“calculateDirections(LatLng start, LatLng end)”。 但是我看不到 direction.destination(destination).setCallback(new PendingResult.Callback() 中的日志,当然当我运行应用程序时,方向不会出现。

如何解决这个问题? 感谢您的回答。

【问题讨论】:

你好,你找到解决办法了吗? @pljni613 【参考方案1】:

您是否已生成并连接了路线的 google api 密钥?并为此付出了代价?

【讨论】:

是的,我生成了 google api 密钥,它还包含方向 api(没有 API 密钥限制)。我没有付费,因为我注册了免费试用版。 我们可以免费创建和生成 api 密钥,但我们无法访问它,除非我们创建一个计费帐户.. 更多信息请参阅此处cloud.google.com/maps-platform/pricing***.com/questions/57313285/…***.com/questions/58665836/…

以上是关于谷歌地图中的方向的主要内容,如果未能解决你的问题,请参考以下文章

谷歌地图方向 V3:地图上的多个方向

谷歌地图 API 方向

谷歌方向 API 行程时间与实时谷歌地图不同

如何使用谷歌地图 api v3 旋转地图方向

iPhone : 谷歌地图方向

谷歌地图方向api:python vs javascript