如何使用库在 android 中实施 Google 方向 API?

How to implement Google Directions API in android using Libraries?

我已经在互联网上搜索了将近 10 个小时来寻找这个东西,但没有运气......关于如何在 google 地图 v2 中使用 google 方向 api 的清晰明了的解释真的很有帮助....

虽然我经历了整个漫长的过程,但我想使用一个库,因为我懒得用我的代码实现那么大的代码....

我已经从 polok 成功实现了这个库。

但是正如您在屏幕截图中看到的那样,方向不准确,折线在转了几圈后就偏离了道路....

我也通过 tyczj 找到了这个库。

但是没有实现....

使用此库的示例将非常有帮助,或者以任何其他方式实现 google 方向 API.....

这是我的 MapActivity 的一个片段:

public class MapActivity extends FragmentActivity implements GoogleApiClient.ConnectionCallbacks,
        GoogleApiClient.OnConnectionFailedListener, LocationListener {

    private GoogleMap mMap; // Might be null if Google Play services APK is not available
    double latitude, longitude;
    Bundle extras;
    String add;
    Geocoder geocoder;
    LatLng position;

    private Location mLastLocation;

    // Google client to interact with Google API
    private GoogleApiClient mGoogleApiClient;
    private boolean mInProgress;


    // boolean flag to toggle periodic location updates

    private LocationRequest mLocationRequest;

    // Location updates intervals in sec
    private static int UPDATE_INTERVAL = 10000; // 10 sec
    private static int FATEST_INTERVAL = 5000; // 5 sec
    private static int DISPLACEMENT = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_map);
        setUpMapIfNeeded();
        extras = getIntent().getExtras();
        mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
        mMap.setMyLocationEnabled(true);

        mGoogleApiClient = new GoogleApiClient.Builder(this)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .addApi(LocationServices.API)
                .build();

        mLocationRequest = LocationRequest.create()
                .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
                .setInterval(UPDATE_INTERVAL)
                .setFastestInterval(FATEST_INTERVAL)
                .setSmallestDisplacement(DISPLACEMENT);

        if (!mGoogleApiClient.isConnected() || !mGoogleApiClient.isConnecting() && !mInProgress) {
            mInProgress = true;
            mGoogleApiClient.connect();
        }


        add = extras.getString("address");

        geocoder = new Geocoder(this);

        try {
            List<Address> addresses = geocoder.getFromLocationName(add,1);

            if(addresses.size()>0){

                latitude = addresses.get(0).getLatitude();
                longitude = addresses.get(0).getLongitude();

            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        position = new LatLng(latitude,longitude);

        Marker marker = mMap.addMarker(new MarkerOptions().position(position).title(add));

        CameraUpdate cu = CameraUpdateFactory.newLatLngZoom(marker.getPosition(), 12F);

        mMap.animateCamera(cu);


    }

    @Override
    protected void onDestroy() {

        mInProgress = false;
        if (mGoogleApiClient.isConnected()) {
            LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
            mGoogleApiClient.disconnect();
        }

        super.onDestroy();
    }

    @Override
    protected void onResume() {
        super.onResume();
        setUpMapIfNeeded();
    }

    private void setUpMapIfNeeded() {
        // Do a null check to confirm that we have not already instantiated the map.
        if (mMap == null) {
            // Try to obtain the map from the SupportMapFragment.
            mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
                    .getMap();

            // Check if we were successful in obtaining the map.
            if (mMap != null) {

            }
        }
    }

    @Override
    public void onConnected(Bundle bundle) {

        LocationServices.FusedLocationApi.requestLocationUpdates(
                mGoogleApiClient, mLocationRequest, this);

        displayLocation();


    }

    @Override
    public void onConnectionSuspended(int i) {

        mGoogleApiClient.connect();

    }

    @Override
    public void onLocationChanged(Location location) {

        mLastLocation = location;

        displayLocation();


    }

    @Override
    public void onConnectionFailed(ConnectionResult connectionResult) {

    }

    private LatLng displayLocation() {

        mLastLocation = LocationServices.FusedLocationApi
                .getLastLocation(mGoogleApiClient);

        if (mLastLocation != null) {
            double myLatitude = mLastLocation.getLatitude();
            double myLongitude = mLastLocation.getLongitude();

            mMap.addMarker(new MarkerOptions().position(new LatLng(myLatitude, myLongitude))
                    .icon(BitmapDescriptorFactory.fromResource(R.drawable.current))
                    .title("Me"));

            return new LatLng(myLatitude,myLongitude);

        }

        return null;
    }
}

谢谢大家的回复.....

我自己找的.....

抱歉,这不是一个好问题,但如果其他人正在寻找类似的答案,这里也是:

我找到了 this 库以及实现,检查这个...通过精确路由实现简单...