简体   繁体   English

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

[英]Google Map driving directions between two locations in my own application

I was working on Google maps, I need Google map driving direction between two locations(my current location and destination location) in my own application I don't want to open any google maps application. 我正在使用Google地图,我需要在自己的应用程序中的两个位置(我当前的位置和目的地位置)之间使用Google地图行车路线,但我不想打开任何Google Maps应用程序。 so please suggest me how to do this. 所以请建议我该怎么做。 up to now i have completed integrating google maps, zoom to my current location, placing a marker in destination lat-long. 到现在为止,我已经完成了Google地图的整合,缩放到当前位置,并在目标纬度位置放置了一个标记。

my java file: 我的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) {

}

} }

There is a webservice public API for directions by google. Google提供了一个网络服务公共API,用于指示路线。 It is not bundled with android API as far as I know. 据我所知,它没有与android API捆绑在一起。

Here you go with the link which will get you started 在这里,您将获得链接 ,它将使您入门

Check out this Awesome and easy to use Library: https://github.com/jd-alexander/Google-Directions-Android 看看这个很棒且易于使用的库: https : //github.com/jd-alexander/Google-Directions-Android

  • This library allows you to calculate the route between two locations and displays it on a map. 该库使您可以计算两个位置之间的路线并将其显示在地图上。
  • It provides you a list of routing point which you can plot in Google Map to show routes and directions. 它为您提供了一个路由点列表,您可以在Google Map中进行绘制以显示路线和方向。

I don't know the end goal of your project. 我不知道您的项目的最终目标。 But if your intention is to get directions from point A to point B. Google's Roads Api can provide you with the data you need to draw the directions on a map or whatever you want. 但是,如果您要获取从A点到B点的路线,则Google的Roads Api可以为您提供所需的数据,以便在地图上或任何您想要的地方绘制路线。

It provides you the raw points even the speed in that particular road segment. 它为您提供原始点,甚至是该特定路段的速度。 It takes care of the redundant data that might lead you to draw directions through buildings for instance. 例如,它会处理多余的数据,这些数据可能会导致您绘制建筑物的方向。

Finally got the result: use this API link: http://maps.googleapis.com/maps/api/directions/json?origin=(origin)&destination=(destination ) 最终得到结果:使用此API链接: http : //maps.googleapis.com/maps/api/directions/json?origin=(origin )& destination=(destination

where it will return JSON as a response which contains latitudes and longitudes, and store them in an array and draw polylines using those lat-long points in google maps 它将返回JSON作为包含纬度和经度的响应,并将其存储在数组中,并使用Google地图中的那些经纬度点绘制折线

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Google Map Android API V2中两个位置之间的行驶距离和行驶时间 - Driving distance and travel time duration between two locations in Google Map Android API V2 如何为Android Google Maps在两个位置之间实施路线搜索 - How to implement directions search between two locations for android google maps 使用Google Directions API在Android应用程序中获取驾驶指示 - Getting driving instructions in android application using Google Directions API 两个地点之间的行驶距离和行驶时间 - Driving distance and travel time duration between two locations 谷歌地图中的路线 - directions in google map 使用 MapBox 生成两个位置的路线和方向 - Using MapBox to generate a route and directions for two locations 使用 Google 地图 API v2 获取行车路线 - Get driving directions using Google Maps API v2 谷歌地图导出行车路线到kml文件 - java geogoogle - google maps export driving directions to kml file - java geogoogle 位置模拟-Google Map检测到移动,但我自己的应用程序未触发位置更改 - Location mocking - google map detects the movement but my own application not triggering location change Google Map路线图APi替代品? - Google Map directions APi Alternitives?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM