``I can't find directions on Google Maps``

I'm having trouble displaying a route between two markers on the map. Currently, the map keeps defaulting to the location of Ireland and not showing the intended route.

public string DrawMapDirections(string Start,string End,string[] WayPoints)
{
    // Map code here 
}

The start and end points are passed through this function:

GoogleMap gm = new GoogleMap();
html += gm.DrawMapDirections(start, end, waypoints.ToArray());

For example, the start point could be: Treloggan Ind Est, Newquay, TR7 2SX, Cornwall, UNITED KINGDOM.

No errors are being displayed, so it's unclear why the route is not showing up.

Here's what the map script returns:

<script>
// Map script content
</script>

Answer №1

It seems like your code is correct. I tested it with a snippet and received a "NOT FOUND" status from the route request. The snippet below shows the route when changing the starting point to an address that Google Maps can locate.

If you have the latitude and longitude of the starting point, you can use that directly. Otherwise, you'll need to input an address known to the direction service.

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <title>Direction Service Example</title>
</head>

<body>
  <div id="map_canvas" style="width: 600px;height: 600px"></div>
  <script>
    var map;
    var directionsDisplay;
    var directionsService;

    function initialize() {
      var ireland = new google.maps.LatLng(53.085222, -7.558594);
      var mapOptions = {
        zoom: 7,
        mapTypeId: google.maps.MapTypeId.ROADMAP,
        center: ireland
      };
      directionsDisplay = new google.maps.DirectionsRenderer({
        draggable: true
      });
      directionsService = new google.maps.DirectionsService();
      map = new google.maps.Map(document.getElementById('map_canvas'), mapOptions);
      directionsDisplay.setMap(map);
      directionsDisplay.setPanel(document.getElementById('directionsPanel'));
      google.maps.event.addListener(directionsDisplay, 'directions_changed',

        function() {
          computeTotalDistance(directionsDisplay.directions);
        });
      calcRoute();
    }

    function calcRoute() {
      var start = 'Hendy Industrial Estate, Hendy, Pontarddulais, Swansea, Carmarthenshire SA4 0XP, UK';
      var end = 'Treloggan Ind Est, Newquay, TR7 2SX, Cornwall, UNITED KINGDOM';
      var waypts = [];
      var request = {
        origin: start,
        destination: end,
        waypoints: waypts,
        optimizeWaypoints: false,
        durationInTraffic: true,
        provideRouteAlternatives: true,
        avoidHighways: false,
        avoidTolls: false,
        travelMode: google.maps.DirectionsTravelMode.DRIVING
      };
      directionsService.route(request, function(response, status) {
        if (status == google.maps.DirectionsStatus.OK) {
          directionsDisplay.setDirections(response);
          var route = response.routes[0];
        }
      });
    }

    function computeTotalDistance(result) {
      var total = 0;
      var myroute = result.routes[0];
      for (i = 0; i < myroute.legs.length; i++) {
        total += myroute.legs[i].distance.value;
      }
      total = total / 1000;
      document.getElementById('total').innerHTML = total + ' km';
    }
  </script>
  <script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false&callback=initialize"></script>
</body>

</html>

Answer №2

Please check out the following aspx code:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>  

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
...
...

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Highchart mortgage calculator for accurate financial planning

I am looking to create a dynamic high chart similar to the one on bankrate.com, showcasing principal, balance, and interest over various years. Despite my efforts, I have been struggling to organize the data by year as demonstrated in the example above: ...

Creating a Node.js application using Express requires careful consideration of file structure and communication methods

Struggling to pass login form credentials to existing JavaScript files for logic and info retrieval. Login page contains a form with POST method. main.js handles the login: main.js module.exports = { returnSessionToken: function(success, error) { ...

Change a space-separated string containing coordinates into an array of JavaScript objects

Here is a string separated by spaces: const coordinates = "42.44492,75.637764 42.445503,75.64534 42.433681,75.6604" Now, we want to convert it into an array of objects: const coordinatesArray = [ { lat: 42.44492, lng: 75.637764 }, { lat: 42.4455 ...

I could really use some guidance on how to begin with the Node.JS app file in BlueMix

After delving into JS tutorials, I feel comfortable with the syntax and approach. Now, my focus is on utilizing Node.JS to develop an app using BlueMix. While I consider myself proficient in Java, web programming is uncharted territory for me, leaving me s ...

Automating the process of uploading a file to a Github repository using JavaScript and HTML

Recently, I've been tinkering with updating a file in my GitHub repository using code to set up an automated system for pushing changes without manual intervention. My approach involved creating a function that utilizes a GitHub access token to ' ...

Using Selenium WebDriver to handle Angular requests in Java

I am currently developing tests for an angular-based application and I find myself in need of assistance. The specific task at hand involves creating a mechanism that will wait until all pending requests within the application have been processed before pr ...

Ways to stop the transmission of the Origin HTTP header in the Chrome browser?

Scenario: I am managing a production web server, such as https://example.com, which is configured with CORS restrictions that do not allow localhost origins. Developers working on localhost are creating a new page/module that requires making AJAX calls t ...

using a function as an argument in the map method within a React component

I have a challenge where I am trying to display blog posts retrieved from my database. However, for each item, I also need to execute a download image function. I attempted to include the function within the .map function but encountered some errors. I am ...

Ensure that an input field on the PHP form is required

Currently working on a form with multiple input fields. I'm wondering if there's a way to prevent the form from being submitted to the server if a specific field is left empty. I'd like to use a JavaScript pop-up box to notify the user inst ...

Display HTML content using JavaScript only when a checkbox is selected

Currently, I am updating an HTML form to show additional subsets of questions based on the selection of a "parent" checkbox. The current implementation works well, but I am wondering if there is a more efficient way to achieve this without having to rewrit ...

When the CSS animation has finished in JavaScript

I am currently developing a game using HTML/JavaScript, and I have implemented a "special ability" that can only be activated once every x seconds. To indicate when this ability is available for use, I have created a graphical user interface element. Since ...

Error encountered in C# ML.NET ProjectToPrincipalComponents - System.ArgumentOutOfRangeException: 'Input column schema does not match'

My current challenge involves transforming a dataview by performing a PCA calculation using the ProjectToPrincipalComponents method. Here is the definition of each object: public class thisItem { public int itemName { get; set; } [ColumnName(& ...

What could be the reason behind the child component updating without triggering a re-render in Reactjs?

I am encountering an issue with my main component and child chart component. Even though the main component updates the state of the child chart component upon connecting to a websocket, the chart does not redraw as expected. Interestingly, when I click on ...

"Unlocking the Power of jQuery: A Guide to Accessing XML Child

I'm struggling to extract the xml in its current format. While I can easily retrieve the InventoryResponse, I am facing difficulties when trying to access the child node a:Cost. Despite conducting research, I have been unable to find a solution that w ...

Issue with Three.js: The mouse hover effect does not revert back to the previous color

Currently, I am working on creating a pattern using Three.js. The goal is to change the color of a face to gray when the user hovers over it with the mouse, and then revert it back to its original light blue color when the mouse moves away. Unfortunately, ...

What is the best way to make an image expand when clicked, align it in the center of the webpage, and have it return to its original position with just one more click by utilizing

Currently, this is the code I am working with. The JavaScript functionality is working well, however, there seems to be an issue where the image does not return to its original size on a second click. Additionally, I am having trouble getting the CSS to ce ...

The response from Ajax is being duplicated with each click

Upon clicking the "Add More" button, I dynamically display two dropdowns. The first dropdown is displayed instantly, while the second dropdown requires an Ajax call to fetch and display its content. The functionality of displaying the second dropdown also ...

Implementing a dual hover effect on a Div element

I am working with HTML code that includes an image and text <div class="subcontainer1"> <img src="a.png" alt="" class="imgcolumn"> <h3 class="header3">Hello</h3> </div> This setup places the content above the image. ...

Exploring the process of updating the NavigateURL of a HyperLink when clicked

Everyone is familiar with the fact that HyperLinks have a navigateURL property which redirects you to the linked destination when clicked, which works well. In certain visual cases, LinkButtons are similar to Hyperlinks but do not have a navigateURL prope ...

Sending database data from PHP to JavaScript - mysql_fetch_array behaving unexpectedly

Forgive me if there is already an answer out there to my question, but after a week of searching online and experimenting, I decided to turn to the experts for help. Purpose: My goal is to query an SQL database using server-side code (specifically PHP), t ...