简体   繁体   中英

Get DIRECTIONS in Ionic Framework Google Map

How can I include directions in my map?

I'm working on ionic framework and this is my js that refer to a ionic.html file (index html just include scripts and position of ionic view). I have installed map and geolocation cordova plugin.

app.controller('MapController', function($scope, $ionicLoading) {

     // var myLatlng = new google.maps.LatLng(37.3000, -120.4833);

      var mapOptions = {
          //center: myLatlng,
          zoom: 16,
          mapTypeId: google.maps.MapTypeId.ROADMAP
      };

      var map = new google.maps.Map(document.getElementById("map"), mapOptions);

      navigator.geolocation.getCurrentPosition(function(pos) {
          map.setCenter(new google.maps.LatLng(pos.coords.latitude, pos.coords.longitude));
          var myLocation = new google.maps.Marker({
              position: new google.maps.LatLng(pos.coords.latitude, pos.coords.longitude),
              map: map,
              title: "My Location"
          });
     // var newLatlng = new google.maps.LatLng(37.3000, -120.4833);
      var torriLocation = new google.maps.Marker({
              position: new google.maps.LatLng(44.823889, 10.335894000000053),
              map: map,
              title: "My Location"
      });

      });


      $scope.map = map;
 });

For directions I mean "how I can reach a specific location by car, walking or bus?"

If you want to get direction of your movement then you want to check your position timely (after some time or position change). So use GetCurrentPostion with SetIntervale or WatchPosition method .Code will we like

setInterval( function(){
      navigator.geolocation.getCurrentPosition(success_loc, error_loc, opti)
    },(1000 * 60 * 1));

or navigator.geolocation.watchPosition(showPosition);

Refer https://developers.google.com/maps/documentation/javascript/examples/map-geolocation

https://developer.mozilla.org/en-US/docs/Web/API/Geolocation/watchPosition

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM