admin管理员组文章数量:1391955
I am trying to make a simple call to Maps API to get direction between two addresses. Have reviewed all the answers here and tried the suggestions provided, but none works. To summarize, when I access the following in a browser it succeeds. But in code that uses the new Routes API I get the above error message.
This works, .0000,133.0000&key=<API_KEY>
The below code does not work,
!pip install googlemaps
import googlemaps
def get_optimal_route(origin, destination, waypoints):
gmaps = googlemaps.Client(key='<API_KEY>')
try:
directions_result = gmaps.directions(
origin,
destination,
waypoints=waypoints,
optimize_waypoints=True,
mode="driving"
)
if directions_result:
route = directions_result[0]
ordered_waypoints = [waypoints[i] for i in route['waypoint_order']]
total_distance = sum(leg['distance']['value'] for leg in route['legs'])
total_duration = sum(leg['duration']['value'] for leg in route['legs'])
return {
"optimized_waypoints": ordered_waypoints,
"total_distance_meters": total_distance,
"total_duration_seconds": total_duration,
"full_route_details": route
}
else:
return {"error": "No route found"}
except Exception as e:
return {"error": str(e)}
origin = "1600 Amphitheatre Parkway, Mountain View, CA"
destination = "48 Wall Street, New York, NY"
waypoints = None
optimal_route = get_optimal_route(origin, destination, waypoints)
if "error" not in optimal_route:
print("Optimal Route:")
print("Ordered Waypoints:", optimal_route["optimized_waypoints"])
print("Total Distance:", optimal_route["total_distance_meters"], "meters")
print("Total Duration:", optimal_route["total_duration_seconds"], "seconds")
#print("Full Route Details:", optimal_route["full_route_details"]) # Print full details if needed
else:
print("Error:", optimal_route["error"])
I have tried the following,
- Checked that all the required APIs are enabled. I have even removed all API restrictions.
- Removed Application restriction.
- Recycled my API Key
- Shutdown and restarted my App
- I saw recommendations to enable Direction API, but this has been phased out starting March 1, 2025
Anything else I should try?
I am trying to make a simple call to Maps API to get direction between two addresses. Have reviewed all the answers here and tried the suggestions provided, but none works. To summarize, when I access the following in a browser it succeeds. But in code that uses the new Routes API I get the above error message.
This works, https://maps.googleapis/maps/api/geocode/json?latlng=-27.0000,133.0000&key=<API_KEY>
The below code does not work,
!pip install googlemaps
import googlemaps
def get_optimal_route(origin, destination, waypoints):
gmaps = googlemaps.Client(key='<API_KEY>')
try:
directions_result = gmaps.directions(
origin,
destination,
waypoints=waypoints,
optimize_waypoints=True,
mode="driving"
)
if directions_result:
route = directions_result[0]
ordered_waypoints = [waypoints[i] for i in route['waypoint_order']]
total_distance = sum(leg['distance']['value'] for leg in route['legs'])
total_duration = sum(leg['duration']['value'] for leg in route['legs'])
return {
"optimized_waypoints": ordered_waypoints,
"total_distance_meters": total_distance,
"total_duration_seconds": total_duration,
"full_route_details": route
}
else:
return {"error": "No route found"}
except Exception as e:
return {"error": str(e)}
origin = "1600 Amphitheatre Parkway, Mountain View, CA"
destination = "48 Wall Street, New York, NY"
waypoints = None
optimal_route = get_optimal_route(origin, destination, waypoints)
if "error" not in optimal_route:
print("Optimal Route:")
print("Ordered Waypoints:", optimal_route["optimized_waypoints"])
print("Total Distance:", optimal_route["total_distance_meters"], "meters")
print("Total Duration:", optimal_route["total_duration_seconds"], "seconds")
#print("Full Route Details:", optimal_route["full_route_details"]) # Print full details if needed
else:
print("Error:", optimal_route["error"])
I have tried the following,
- Checked that all the required APIs are enabled. I have even removed all API restrictions.
- Removed Application restriction.
- Recycled my API Key
- Shutdown and restarted my App
- I saw recommendations to enable Direction API, but this has been phased out starting March 1, 2025
Anything else I should try?
Share edited Mar 14 at 18:22 user2517962 asked Mar 14 at 14:05 user2517962user2517962 295 bronze badges 3- Are you URL encoding the strings in the URL? – geocodezip Commented Mar 14 at 17:01
- I am directly calling the Directions API. I assumed it will do the required URL encoding. – user2517962 Commented Mar 14 at 18:20
- 1 I found the solution. Even though Google says that Direction API is deprecated and it does not show up when you search for it in your Google Cloud project, this API needs to be enabled. I found a link to the Direction API in the Google Maps Documentation. Once enabled my code worked. Below is the link to the documentation page. developers.google/maps/documentation/javascript/directions – user2517962 Commented Mar 15 at 2:35
1 Answer
Reset to default 0You can solve this by enabling the Direction API. Currently, the only way to do this is by going to this link (because its in Legacy).
本文标签: Google Maps API fails quotThis API project is not authorized to use this APIquotStack Overflow
版权声明:本文标题:Google Maps API fails "This API project is not authorized to use this API." - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744652008a2617740.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论