2015-12-11 49 views
1

我用離子地理位置跟蹤客戶的地方,但標記不會改變她的地方,請任何一個能幫助我,這是我的控制器代碼:離子地理位置變化標記的地方

控制器

app.controller('mapCtrl', function($scope, $ionicPlatform, $state, $http, $cordovaGeolocation, $interval, $ionicPopup, $ionicLoading) { 
    $scope.positions = [{ 
     lat: 43.07493, 
     lng: -89.381388 
    }]; 
    $scope.$on('mapInitialized', function(event, map) { 
     $scope.map = map; 
     $scope.positions = []; 
     $ionicLoading.show({ 
      template: 'Loading...' 
     }); 
     $scope.map = map; 
     var getLocation = function() { 
      marker.setVisible(false); 
      navigator.geolocation.getCurrentPosition(function(position) { 
       var pos = new google.maps.LatLng(position.coords.latitude, position.coords.longitude); 
       var marker = new google.maps.Marker({ 
        position: pos, 
        map: map, 
        animation: google.maps.Animation.DROP, 
        'icon': 'img/marker2.png' 
       }); 
       marker.setVisible(true); 
       marker.setIcon('http://maps.gstatic.com/mapfiles/ridefinder-images/mm_20_gray.png'); 
       $scope.positions.push({ 
        lat: pos.k, 
        lng: pos.B 
       }); 
       console.log(pos); 
       $scope.map.setCenter(pos); 
       $ionicLoading.hide(); 
      }); 
     }; 
     $interval(getLocation, 5000); 
    }); 
}); 

回答

-1

嘗試聲明沒有位置的標記,然後使用cordova提供的navigator.geolocation.watchPosition()函數更新標記變量,而不是使用$interval,如果用戶不移動它們可能會返回相同的位置。

該函數在檢測到更改時獲取新的位置數據集。查看全部使用這裏Cordova-Plugin-Geolocation

例子:

app.controller('mapCtrl', function($scope, $ionicPlatform, $state, $http, $cordovaGeolocation, $ionicPopup, $ionicLoading) { 
    $scope.positions = [{ 
    lat: 43.07493, 
    lng: -89.381388 
    }]; 
    $scope.$on('mapInitialized', function(event, map) { 
    $scope.map = map; 
    $scope.positions = []; 
    $ionicLoading.show({ 
     template: 'Loading...' 
    }); 
    $scope.map = map; 
    var marker = new google.maps.Marker({ 
     animation: google.maps.Animation.DROP, 
     icon: 'http://maps.gstatic.com/mapfiles/ridefinder-images/mm_20_gray.png', 
     map: map, 
     visible: false 
    }); 
    navigator.geolocation.watchPosition(function(position) { 
     var pos = new google.maps.LatLng(position.coords.latitude, position.coords.longitude); 
     marker.setPosition(pos); 
     $scope.positions.push({ 
     lat: pos.k, 
     lng: pos.B 
     }); 
     console.log(pos); 
     map.setCenter(pos); 
     if(!marker.getVisible()) { 
     marker.setVisible(true); 
     $ionicLoading.hide(); 
     } 
    }); 
    }); 

    ... 

});