离子和角度谷歌地图获取路线

时间:2015-05-13 15:05:05

标签: angularjs google-maps ionic-framework

目前我的地图可以提供从预设点A到预设点B的路线,但我希望从用户位置到预设点的路线,如下例所示: http://jquery-ui-map.googlecode.com/svn/trunk/demos/jquery-google-maps-mobile.html#directions_map

我的控制器代码是:

.controller('ContactCtrl', function($scope, $document, $ionicLoading) {
// map object
$scope.map = {
control: {},
center: {
    latitude: 51.51139,
    longitude: -0.2237284
},
zoom: 14
 };

 // marker object
 $scope.marker = {
 center: {
    latitude: 51.51139,
    longitude: -0.2237284
 }
}

// instantiate google map objects for directions

var directionsDisplay = new google.maps.DirectionsRenderer();
var directionsService = new google.maps.DirectionsService();
var geocoder = new google.maps.Geocoder();


// directions object -- with defaults
$scope.directions = {
origin: "this shoud be user ",
destination: "White City, London W12 7RQ",
 showList: false
 }

 // get directions using google maps api
 $scope.getDirections = function () {
  var request = {
  origin: $scope.directions.origin,
  destination: $scope.directions.destination,
  travelMode: google.maps.DirectionsTravelMode.DRIVING
 };
  directionsService.route(request, function (response, status) {
   if (status === google.maps.DirectionsStatus.OK) {
    directionsDisplay.setDirections(response);
    directionsDisplay.setMap($scope.map.control.getGMap());
    directionsDisplay.setPanel(document.getElementById('directionsList'));
    $scope.directions.showList = true;
  } else {
    alert('Google route unsuccesfull!');
  }
  });
}
})

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

在开始指导服务之前,请获取用户的位置:

$scope.centerOnMe = function() {
  if (!$scope.map) {
    return;
  }

  $scope.loading = $ionicLoading.show({
    content: 'Getting current location...',
  });

  navigator.geolocation.getCurrentPosition(function(position) {
    var pos = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
    $scope.positions.push({
      lat: pos.k,
      lng: pos.B
    });
    console.log(pos);
    $scope.map.setCenter(pos);
    $ionicLoading.hide();
  });
};

然后访问您在方向请求中获得的位置,例如:

// directions object -- with defaults
$scope.directions = 
  origin: pos.k + "," + pos.B,
  destination: "White City, London W12 7RQ",
  showList: false
}