0

Problem

I'm using the Google Maps API Nearest Road to find the nearest road given a lat/lon coordinate. I've run into a lat/lon coordinate that does not return anything and I have no idea why. The location of this lat/lon coordinate is right next to a road in North Carolina.

https://roads.googleapis.com/v1/nearestRoads?points=35.77747,-82.4603&key=`{GOOGLE_API_KEY}`

Why does this return {}?

Google Map Location

enter image description here

oguz ismail
  • 1
  • 16
  • 47
  • 69
Vedda
  • 7,066
  • 6
  • 42
  • 77
  • Who knows. Data issues can't be dealt with by the SO community. – MrUpsidown Feb 18 '21 at 23:22
  • @MrUpsidown this question can also be fielded by Google Support. https://developers.google.com/maps/documentation/directions/support#contact-maps-support – Vedda Feb 19 '21 at 00:01

1 Answers1

1

My guess is your point is too far from the road.

You might want to use the DirectionsService (Web Service), setting the origin and destination to your input point.

That seems to have a larger tolerance (although still limited) to finding the nearest road.

https://maps.googleapis.com/maps/api/directions/json?origin=35.77747,-82.4603&destination=35.77747,-82.4603&key=`{GOOGLE_API_KEY}`

returns:

// snip
   "end_location" : {
      "lat" : 35.777998,
      "lng" : -82.4598565
   },
// snip
   "start_location" : {
      "lat" : 35.777998,
      "lng" : -82.4598565
   },
// snip

Related questions:

proof of concept fiddle

screenshot of original marker and closest point on road

code snippet:

const point = {
  lat: 35.77747,
  lng: -82.4603
}

function initMap() {
  const directionsService = new google.maps.DirectionsService();
  const directionsRenderer = new google.maps.DirectionsRenderer({
    preserveViewport: true
  });
  const map = new google.maps.Map(document.getElementById("map"), {
    zoom: 18,
    center: point,
  });
  const marker = new google.maps.Marker({
    map: map,
    position: point,
    title: "input point"
  })
  directionsRenderer.setMap(map);

  calculateAndDisplayRoute(directionsService, directionsRenderer);
}

function calculateAndDisplayRoute(directionsService, directionsRenderer) {
  directionsService.route({
      origin: point,
      destination: point,
      travelMode: google.maps.TravelMode.DRIVING,
    },
    (response, status) => {
      if (status === "OK") {
        directionsRenderer.setDirections(response);
      } else {
        window.alert("Directions request failed due to " + status);
      }
    }
  );
}
/* Always set the map height explicitly to define the size of the div
       * element that contains the map. */

#map {
  height: 100%;
}


/* Optional: Makes the sample page fill the window. */

html,
body {
  height: 100%;
  margin: 0;
  padding: 0;
}
<!DOCTYPE html>
<html>

<head>
  <title>Directions Service</title>
  <script src="https://polyfill.io/v3/polyfill.min.js?features=default"></script>
  <!-- jsFiddle will insert css and js -->
</head>

<body>
  <div id="map"></div>

  <!-- Async script executes immediately and must be after any DOM elements used in callback. -->
  <script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk&callback=initMap&libraries=&v=weekly" async></script>
</body>

</html>
geocodezip
  • 158,664
  • 13
  • 220
  • 245