1

I'm using NgMap and I need to get the directions in a controller. I've tried the following:

NgMap.getMap().then(function(map) {
    console.log(map.directionsRenderers[0].directions);
});

But it is returning undefined. I also tried using $scope.map but that is also undefined. In my view {{map.directionsRenderers[0].directions.routes[0].legs[0].distance.text}} works as expected.

My map is as follows:

<ng-map zoom="10" center="58.637381759037865,-3.0689620971679683">
                <directions 
                draggable="false"
                panel="directions-panel"
                travel-mode="WALKING"
                origin="58.637381759037865,-3.0689620971679683"
                destination="50.063062230661494,-5.714178085327148"></directions>
              <custom-marker id="start"
                position="58.637381759037865,-3.0689620971679683">
                <div> Start Here </div>
              </custom-marker>
              <custom-marker id="end"
                position="50.063062230661494,-5.714178085327148">
                <div> Ends Here </div>
              </custom-marker>
            </ng-map>

What simple thing am I missing? Thanks.

JoshH2
  • 55
  • 6
  • 1
    I've added a watch to the scope variable as follows `$scope.$watch('map.directionsRenderers[0].directions', function() {` and checked if it is defined and it is now working. Is this the best approach? – JoshH2 Mar 02 '17 at 10:06
  • I ended doing the same, but seriously it's disgusting.. What the hell is this object map from getMap that display like a JS object in console but access error on operator '.' and []... – MaxBlax360 Nov 08 '18 at 18:03

1 Answers1

0

You can use the following method, named getDirections, like that:

ngMap.getMap()
     .then(function (map) {
         var renderer = map.directionsRenderers[0];
         var directions = renderer.getDirections();
}
swstk
  • 1
  • 1