java - 我自己的应用程序中两个位置之间的谷歌地图行车路线

标签 java android google-maps

我正在使用谷歌地图,我需要在我自己的应用程序中的两个位置(我的当前位置和目的地位置)之间的谷歌地图行车方向我不想打开任何谷歌地图应用程序。所以请建议我如何做到这一点。到目前为止,我已经完成了整合谷歌地图,缩放到我当前的位置,在目的地经纬度上放置一个标记。

我的java文件:

public class GoogleMapActivity extends FragmentActivity implements
    OnMapReadyCallback, GoogleMap.OnMarkerClickListener, GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {

Location mLastLocation;
GoogleApiClient mGoogleApiClient;
private GoogleMap mMap;

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

    SupportMapFragment mapFragment =
            (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);
    Log.d("Tag", "in onc control in try");
    buildGoogleApiClient();
}

protected void onStart() {
    mGoogleApiClient.connect();
    super.onStart();
}

protected void onStop() {
    mGoogleApiClient.disconnect();
    super.onStop();
}

protected synchronized void buildGoogleApiClient() {
    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(LocationServices.API)
            .build();
    mGoogleApiClient.connect();
}
@Override
public void onConnected(@Nullable Bundle bundle) {
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        // TODO: Consider calling
        //    ActivityCompat#requestPermissions
        // here to request the missing permissions, and then overriding
        //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
        //                                          int[] grantResults)
        // to handle the case where the user grants the permission. See the documentation
        // for ActivityCompat#requestPermissions for more details.
        return;
    }
    mLastLocation = LocationServices.FusedLocationApi.getLastLocation(
            mGoogleApiClient);
    if (mLastLocation != null) {
        Log.d("TAG","lat"+mLastLocation.getLatitude());
        Log.d("TAG","lng"+mLastLocation.getLongitude());
        ToastHelper.blueToast(getApplicationContext(), "location is " + mLastLocation.getLatitude() + " " + mLastLocation.getLongitude());
        mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(mLastLocation.getLatitude(), mLastLocation.getLongitude()), 12.0f));
        LatLng destination = new LatLng(14.880499, 79.988847);
        mMap.addMarker(new MarkerOptions().position(destination).title("Destination"));
    }
    else {
        Log.d("TAG","mLastLocation is null");
    }
}

@Override
public void onConnectionSuspended(int i) {
}

/**
 * Called when the map is ready.
 */
@Override
public void onMapReady(GoogleMap map) {
    mMap = map;
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        // TODO: Consider calling
        //    ActivityCompat#requestPermissions
        // here to request the missing permissions, and then overriding
        //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
        //                                          int[] grantResults)
        // to handle the case where the user grants the permission. See the documentation
        // for ActivityCompat#requestPermissions for more details.
        return;
    }
    mMap.setMyLocationEnabled(true);
}
/**
 * Called when the user clicks a marker.
 */
@Override
public boolean onMarkerClick(final Marker marker) {
    // Retrieve the data from the marker.
    // Return false to indicate that we have not consumed the event and that we wish
    // for the default behavior to occur (which is for the camera to move such that the
    // marker is centered and for the marker's info window to open, if it has one).
    return false;
}

@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {

}

最佳答案

Google 提供了一个网络服务公共(public) API,用于指示路线。据我所知,它没有与 android API bundle 在一起。

这里是link这会让你开始

关于java - 我自己的应用程序中两个位置之间的谷歌地图行车路线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40541951/

相关文章:

java - 网络发现 Hyperledger 结构

android - soundPool.load上下文问题

Android Facebook Sdk 4.11.0 如何在没有任何链接或图像的情况下将简单的状态更新发布到墙上

android - Google maps android api 使用哪个证书?

Java设计/面向对象设计问题

java : converting hexadecimal number string to decimal

java - Gradle: 'Cannot do operations on a non-existent table' 与本地 Dynamodb

android - 如何使用PhoneRTC实现静音?

c# - Xamarin 谷歌地图空白

google-maps - 谷歌地图 V3 API 失败