You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Google Directions API调用失败求助(JavaScript拼车网站开发)

Fixing "NOT_FOUND" Error with Google Directions API for Your Ride-Sharing Site

Hey there! Let's troubleshoot that frustrating NOT_FOUND error you're hitting when trying to generate routes with the Google Directions API. Based on your code snippet and problem description, here are the most likely issues and fixes:

1. Make Sure You're Properly Capturing Coordinates from Autocomplete

Your code initializes latorigine and latdestination, but it looks like you might be missing the place change event listeners for your Autocomplete inputs. Without these, those variables won't get populated with valid LatLng data.

Add these listeners to capture the selected place's coordinates:

// Capture departure coordinates
departure_autocomplete.addListener('place_changed', function() {
  const place = departure_autocomplete.getPlace();
  // Verify the place has valid geometry data
  if (!place.geometry || !place.geometry.location) {
    alert("Couldn't find a valid location for your departure address");
    return;
  }
  latorigine = place.geometry.location; // This is a LatLng object (no need to convert to string!)
});

// Capture arrival coordinates
arrival_autocomplete.addListener('place_changed', function() {
  const place = arrival_autocomplete.getPlace();
  if (!place.geometry || !place.geometry.location) {
    alert("Couldn't find a valid location for your destination address");
    return;
  }
  latdestination = place.geometry.location;
});

2. Use the Correct Format for Origin/Destination Parameters

The Directions API accepts three valid formats for origin and destination:

  • A google.maps.LatLng object (simplest and most reliable)
  • A string in the format "latitude,longitude" (no spaces!)
  • A human-readable address string

If you were converting the LatLng to a string incorrectly (e.g., adding spaces or extra characters), that would trigger NOT_FOUND. Using the raw LatLng object avoids formatting mistakes entirely.

Here's how to call directionsService.route() correctly:

// Example function to trigger route generation (attach this to a button click)
function calculateRoute() {
  if (!latorigine || !latdestination) {
    alert("Please select both departure and destination addresses first");
    return;
  }

  directionsService.route({
    origin: latorigine,
    destination: latdestination,
    travelMode: google.maps.TravelMode.DRIVING // Adjust to your needs (WALKING, TRANSIT, etc.)
  }, function(response, status) {
    if (status === google.maps.DirectionsStatus.OK) {
      directionsDisplay.setDirections(response);
    } else {
      console.error(`Directions request failed: ${status}`);
      // Handle other errors here (e.g., OVER_QUERY_LIMIT, REQUEST_DENIED)
    }
  });
}

3. Double-Check API Key Permissions

While NOT_FOUND usually relates to invalid locations, it's worth confirming your API key has both:

  • Places API enabled (for Autocomplete)
  • Directions API enabled (for route calculation)
    If either is missing, you might run into unexpected errors.

Quick Checklist to Verify

  • Did you test that latorigine and latdestination are actually populated with valid data before calling route()? (Log them to the console to check!)
  • Are you using the correct travel mode for your region? (e.g., TRANSIT might not be available in all areas)
  • Is your component restriction (country: 'fr') working as expected? Make sure users aren't entering addresses outside France accidentally.

Give these fixes a try—they should resolve the NOT_FOUND error and get your route generation working!

内容的提问来源于stack exchange,提问作者user9539310

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 03:58:47