I have a issue regarding
Directions API Response: { “error_message” : “This API project is not authorized to use this API.”, “routes” : [], “status” : “REQUEST_DENIED”}
this is error i always received from my Direction API whenever i tried calling to create rout between the origin and destination
for testing i used Log in android studio
(No routes found in the response.)
but this authorization issue always occurs.
I’m am using same API in Map SDK JavaScript & also using into Map SDK Android.
even while loading map on the android that key works. location markers update perfectly,
on Map SDK JavaScript with same api the routes working.
still the problem remains same in android studio.
in Android studio it places destination marker successfully but no route while creating the route.
Cross checked everything.
Enabling all api key
No ristriction
Generated new key’s multiple times.
even created new project to generate new api. will anyone help me out.
Thanks & Regards
Stuck from last one Week.
This code for DirectionApi.java
public class DirectionsApi {
public static String getDirectionsUrl(LatLng origin, LatLng destination, String apiKey) {
String url = "https://maps.googleapis.com/maps/api/directions/json?" +
"origin=" + origin.latitude + "," + origin.longitude +
"&destination=" + destination.latitude + "," + destination.longitude +
"&mode=driving&key=" + apiKey;
System.out.println("Directions API URL: " + url);
return url;
}
public static String requestDirections(String url) throws IOException {
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setRequestMethod("GET");
int responseCode = con.getResponseCode();
System.out.println("Response Code: " + responseCode);
if (responseCode == 200) {
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuilder response = new StringBuilder();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
System.out.println("Response: " + response.toString());
return response.toString();
} else {
return null;
}
}
public static List<LatLng> parseDirectionsResponse(String response) throws JSONException {
JSONObject jsonObject = new JSONObject(response);
JSONArray routes = jsonObject.getJSONArray("routes");
if (routes.length() == 0) {
return null;
}
JSONObject route = routes.getJSONObject(0);
JSONArray legs = route.getJSONArray("legs");
JSONObject leg = legs.getJSONObject(0);
JSONArray steps = leg.getJSONArray("steps");
List<LatLng> routeCoordinates = new ArrayList<>();
for (int i = 0; i < steps.length(); i++) {
JSONObject step = steps.getJSONObject(i);
JSONObject polyline = step.getJSONObject("polyline");
String polylineString = polyline.getString("points");
List<LatLng> polylineCoordinates = decodePolyline(polylineString);
routeCoordinates.addAll(polylineCoordinates);
}
return routeCoordinates;
}
private static List<LatLng> decodePolyline(String polylineString) {
List<LatLng> polylineCoordinates = new ArrayList<>();
int index = 0;
int lat = 0;
int lng = 0;
while (index < polylineString.length()) {
int b;
int shift = 0;
int result = 0;
do {
b = polylineString.charAt(index++) - 63;
result |= (b & 0x1f) << shift;
shift += 5;
} while (b >= 0x20);
int dlat = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
lat += dlat;
shift = 0;
result = 0;
do {
b = polylineString.charAt(index++) - 63;
result |= (b & 0x1f) << shift;
shift += 5;
} while (b >= 0x20);
int dlng = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1));
lng += dlng;
polylineCoordinates.add(new LatLng(lat * 1e-5, lng * 1e-5));
}
return polylineCoordinates;
}
}
This is code of MapsActivity.java
private void getDirectionsToUser(LatLng userLatLng) {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
&& ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION}, REQUEST_LOCATION_PERMISSION);
return;
}
// Show the progress dialog
progressDialog = new ProgressDialog(this);
progressDialog.setMessage("Fetching route...");
progressDialog.setCancelable(false);
progressDialog.show();
fusedLocationClient.getLastLocation().addOnSuccessListener(this, location -> {
if (location != null) {
serviceProviderLatLng = new LatLng(location.getLatitude(), location.getLongitude());
String apiKey = getString(R.string.google_api_key); // Use the correct API key
String url = DirectionsApi.getDirectionsUrl(serviceProviderLatLng, userLatLng, apiKey);
executorService.execute(() -> {
try {
String result = DirectionsApi.requestDirections(url);
runOnUiThread(() -> {
// Dismiss the progress dialog
if (progressDialog != null && progressDialog.isShowing()) {
progressDialog.dismiss();
}
if (result != null) {
System.out.println("Directions API Response: " + result);
try {
List<LatLng> routeCoordinates = DirectionsApi.parseDirectionsResponse(result);
if (routeCoordinates != null && !routeCoordinates.isEmpty()) {
addPolylinesToMap(routeCoordinates);
} else {
Log.e("MapsActivity", "No routes found in the response.");
Toast.makeText(MapsActivity.this, "No routes found.", Toast.LENGTH_SHORT).show();
}
} catch (JSONException e) {
Log.e(TAG, "Failed to parse directions", e);
Toast.makeText(MapsActivity.this, "Failed to parse directions.", Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(MapsActivity.this, "Failed to get directions.", Toast.LENGTH_SHORT).show();
}
});
} catch (IOException e) {
Log.e(TAG, "Failed to fetch directions", e);
runOnUiThread(() -> {
if (progressDialog != null && progressDialog.isShowing()) {
progressDialog.dismiss();
}
Toast.makeText(MapsActivity.this, "Failed to get directions.", Toast.LENGTH_SHORT).show();
});
}
});
}
});
}
private void addPolylinesToMap(final List<LatLng> routeCoordinates) {
// Use a handler to run the code on the main thread
new Handler(Looper.getMainLooper()).post(() -> {
if (routeCoordinates != null && !routeCoordinates.isEmpty()) {
Polyline polyline = mMap.addPolyline(new PolylineOptions().addAll(routeCoordinates));
polyline.setColor(ContextCompat.getColor(MapsActivity.this, R.color.blue));
polyline.setClickable(true);
} else {
Log.w(TAG, "run: No routes found.");
}
});
}
I want to create route between Origin and destination
Directions API Response: { “error_message” : “This API project is not authorized to use this API.”, “routes” : [], “status” : “REQUEST_DENIED”}
Gautam chibi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.