every Time i try to use google direction api i got this error message "onRoutingFailure: Keyless access to Google Maps Platform is deprecated. Please use an API key with all your API calls to avoid service interruption. For further details please refer to http://g.co/dev/maps-no-account"
the Main class is here
package com.mostafazaghloul.checkroute2;
import android.content.pm.PackageManager;
import android.location.Location;
import android.os.Looper;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.util.Log;
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.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.LocationCallback;
import com.google.android.gms.location.LocationListener;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationResult;
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
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.MarkerOptions;
import com.google.android.gms.maps.model.Polyline;
import com.google.android.gms.maps.model.PolylineOptions;
import java.util.ArrayList;
import java.util.List;
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback, GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener
, LocationListener, RoutingListener {
private GoogleMap mMap;
private List<Polyline> polylines;
private static final int[] COLORS = new int[]{R.color.colorPrimary};
private LatLng start, waypoint, end;
private Location location;
private MarkerOptions marker;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
polylines = new ArrayList<>();
end = new LatLng(30.7148343, 31.2605547);
marker = new MarkerOptions().position(end).title("reciver");
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
/**
* Manipulates the map once available.
* This callback is triggered when the map is ready to be used.
* This is where we can add markers or lines, add listeners or move the camera. In this case,
* we just add a marker near Sydney, Australia.
* If Google Play services is not installed on the device, the user will be prompted to install
* it inside the SupportMapFragment. This method will only be triggered once the user has
* installed Google Play services and returned to the app.
*/
@Override
public void onMapReady(GoogleMap googleMap) {
try {
start = new LatLng( 30.714580, 31.260591 );
mMap = googleMap;
if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
bulidGoogleApiClient();
mMap.setMyLocationEnabled(true);
// adding marker
googleMap.addMarker(marker);
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(marker.getPosition(),15));
MakeRoute();
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void onRoutingFailure(RouteException e) {
if (e != null) {
Toast.makeText(this, "Error: " + e.getMessage(), Toast.LENGTH_LONG).show();
Log.e("onRoutingFailure",e.getMessage().toString());
} else {
Toast.makeText(this, "Something went wrong, Try again", Toast.LENGTH_SHORT).show();
Log.e("onRoutingFailure",e.getMessage().toString());
}
}
@Override
public void onRoutingStart() {
}
@Override
public void onRoutingSuccess(ArrayList<Route> route, int shortestRouteIndex) {
try {
if (polylines.size() > 0) {
for (Polyline poly : polylines) {
poly.remove();
}
}
polylines = new ArrayList<>();
//add route(s) to the map.
// for (int i = 0; i < route.size(); i++) {
//In case of more than 5 alternative routes
int colorIndex = 1 % COLORS.length;
PolylineOptions polyOptions = new PolylineOptions();
polyOptions.color(getResources().getColor(COLORS[colorIndex]));
polyOptions.width(10 + 1 * 3);
polyOptions.addAll(route.get(1).getPoints());
Polyline polyline = mMap.addPolyline(polyOptions);
polylines.add(polyline);
//Toast.makeText(getApplicationContext(),"Route "+ (i+1) +": distance - "+ route.get(i).getDistanceValue()+": duration - "+ route.get(i).getDurationValue(),Toast.LENGTH_SHORT).show();
// }
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void onRoutingCancelled() {
}
@Override
public void onConnected(@Nullable Bundle bundle) {
LocationRequest mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(100000);
mLocationRequest.setFastestInterval(100000);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
LocationServices.getFusedLocationProviderClient(this).requestLocationUpdates(mLocationRequest, new LocationCallback() {
@Override
public void onLocationResult(LocationResult locationResult) {
onLocationChanged(locationResult.getLastLocation());
}
}, Looper.myLooper());
}
@Override
public void onConnectionSuspended(int i) {
}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
}
LatLng latLng1;
@Override
public void onLocationChanged(Location location) {
this.location = location;
latLng1 = new LatLng(location.getLatitude(), location.getLongitude());
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng1, 11), 1000, null);
}
protected synchronized void bulidGoogleApiClient() {
GoogleApiClient mgooGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this).addOnConnectionFailedListener(this).addApi(LocationServices.API).build();
mgooGoogleApiClient.connect();
}
/*
To make a route between sender and receiver.
*/
Routing routing;
private void MakeRoute() {
try {
// LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
waypoint = new LatLng(18.01455, -77.499333);
routing = new Routing.Builder()
.travelMode(AbstractRouting.TravelMode.DRIVING)
.withListener(this)
.alternativeRoutes(true)
.waypoints(start, end)
.build();
routing.execute();
} catch (Exception e) {
e.printStackTrace();
}
}
}
i get the key from the google cloud and i restrict it with Android apps and map google api and direction api i don't pay any thing for this . I searched a lot without any solution can any one help ?
Routing.Builder()
accept an API key? Internally it calls web service that requires an API key. You should provide it somewhere. An API key that you use for Maps Android SDK is not valid for web service calls. – xomena