Get a Jumpstart with our Sample Apps
Results 1 to 6 of 6

Thread: mapControl.plotRoute does not return total miles

  1. #1
    Member
    Real Name
    Joe Lavely
    Join Date
    Jun 2013
    Posts
    322

    Default mapControl.plotRoute does not return total miles

    When plotting a route with more than two locations, I would expect that plotRoute() would return the total mileage, including all locations. But it only shows miles from the first to the second location, then from the second to the next, etc.

    I'm not sure if this is a bug, but every other map program I've used returns the total miles. This includes Google Maps.

    I've attached an UX, based on the example in the documentation for mapControl.plotRoute Method

    https://www.alphasoftware.com/docume...e%20method.xml



    MapTest.a5wcmp

  2. #2
    Member
    Real Name
    Joe Lavely
    Join Date
    Jun 2013
    Posts
    322

    Default Re: mapControl.plotRoute does not return total miles

    I have looked into this a little further. As I understand it, Google Maps returns a DirectionsResult object that provides the distance for each leg. It would be up to the requester to extract and then summarize the distance for each leg.

    Here is an example:

    https://stackoverflow.com/questions/...with-waypoints

    http://www.geocodezip.com/v3_GoogleE...totalDist.html


    Is it possible to do this easily in Alpha?

    Code:
    var directionDisplay;
    var directionsService = new google.maps.DirectionsService();
    var map;
    
    function initialize() {
      directionsDisplay = new google.maps.DirectionsRenderer();
      var chicago = new google.maps.LatLng(41.850033, -87.6500523);
      var myOptions = {
        zoom: 6,
        mapTypeId: google.maps.MapTypeId.ROADMAP,
        center: chicago
      }
      map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
      directionsDisplay.setMap(map);
      calcRoute();
    }
    google.maps.event.addDomListener(window, 'load', initialize);
    
    function calcRoute() {
    
      var request = {
        // from: Blackpool to: Preston to: Blackburn
        origin: "Blackpool,UK",
        destination: "Blackburn,UK",
        waypoints: [{
          location: "Preston,UK",
          stopover: true
        }],
        optimizeWaypoints: true,
        travelMode: google.maps.DirectionsTravelMode.WALKING
      };
      directionsService.route(request, function(response, status) {
        if (status == google.maps.DirectionsStatus.OK) {
          directionsDisplay.setDirections(response);
          var route = response.routes[0];
          var summaryPanel = document.getElementById("directions_panel");
          summaryPanel.innerHTML = "";
          // For each route, display summary information.
          for (var i = 0; i < route.legs.length; i++) {
            var routeSegment = i + 1;
            summaryPanel.innerHTML += "<b>Route Segment: " + routeSegment + "</b><br />";
            summaryPanel.innerHTML += route.legs[i].start_address + " to ";
            summaryPanel.innerHTML += route.legs[i].end_address + "<br />";
            summaryPanel.innerHTML += route.legs[i].distance.text + "<br /><br />";
          }
          computeTotalDistance(response);
        } else {
          alert("directions response " + status);
        }
      });
    }
    
    function computeTotalDistance(result) {
      var totalDist = 0;
      var totalTime = 0;
      var myroute = result.routes[0];
      for (i = 0; i < myroute.legs.length; i++) {
        totalDist += myroute.legs[i].distance.value;
        totalTime += myroute.legs[i].duration.value;
      }
      totalDist = totalDist / 1000.
      document.getElementById("total").innerHTML = "total distance is: " + totalDist + " km<br>total time is: " + (totalTime / 60).toFixed(2) + " minutes";
    }

  3. #3
    Member
    Real Name
    Michael Cornford
    Join Date
    Oct 2011
    Location
    East Sussex, UK
    Posts
    846

    Default Re: mapControl.plotRoute does not return total miles

    The office opens on the 28th. We have simple code for this.

    I am on holiday and don't have the code on this machine.

    Please remind me on the 27th and I will post it.

    cornford.michael@gmail.com

  4. #4
    "Certified" Alphaholic
    Real Name
    David Kates
    Join Date
    Apr 2008
    Location
    Unionville, ON
    Posts
    6,368

    Default Re: mapControl.plotRoute does not return total miles

    The distances are all inside the map control... in mapControl1._dirR.directions.routes[0]. There is a array property named legs which contains each leg of the route and all it's information.

    Loop through the legs getting what you need.

    Have a look at the Javascript function named getTotalDistance(). It's run after each of your 2 and 3 Locations code. I've had to put a setTimeout around calling getTotalDistance - not happy about that - but the route isn't available for a bit. I'm not sure if there is a good event that could be used instead... I'm sure something could be done... so you can have a look for that.
    Attached Files Attached Files

  5. #5
    Member
    Real Name
    Joe Lavely
    Join Date
    Jun 2013
    Posts
    322

    Default Re: mapControl.plotRoute does not return total miles

    Thanks David. That is just what I was looking for. I have not found an event that fires after the route is returned. I am running plotRoute after geocoding some locations, and Google Maps does not always successfully geocode. Sometimes it requires several tries. So I can live with the SetTimeout.

  6. #6
    "Certified" Alphaholic peteconway's Avatar
    Real Name
    Peter Conway
    Join Date
    Oct 2005
    Location
    Melbourne, Australia
    Posts
    2,379

    Default Re: mapControl.plotRoute does not return total miles

    Nice work David
    Insanity: doing the same thing over and over again and expecting different results.
    Albert Einstein, (attributed)
    US (German-born) physicist (1879 - 1955)

Similar Threads

  1. How to determine miles from Starting point to Destination
    By JetLi in forum Mobile & Browser Applications
    Replies: 11
    Last Post: 06-17-2016, 02:43 AM
  2. plotRoute variable problem
    By sjenkins in forum Mobile & Browser Applications
    Replies: 2
    Last Post: 08-24-2013, 05:26 AM
  3. Map Functionality On Dialog (UX) Componet - No plotRoute method available
    By Al Buck in forum Application Server Version 11 - Web/Browser Applications
    Replies: 9
    Last Post: 04-25-2013, 06:56 AM
  4. order minus return = invoice total
    By Justin.p in forum Alpha Five Version 10 - Desktop Applications
    Replies: 2
    Last Post: 03-31-2010, 08:49 AM
  5. Total fails to total on the form
    By Chris A in forum Alpha Five Version 5
    Replies: 1
    Last Post: 07-28-2004, 01:07 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •