The Below code you can easily use in controllers,
first download geolocation plugin
Paste the below code in your controller..
$ionicPlatform.ready(function() {
$scope.pickCurrentLocation = function() {
var postOptions = {
timeout: 50000,
enableHighAccuracy: false
};
$cordovaGeolocation.getCurrentPosition(postOptions).then(function(position) {
$scope.location = {};
$scope.lat = position.coords.latitude;
$scope.lng = position.coords.longitude;
if ($scope.lat && $scope.lng) {
$http.get("http://maps.google.com/maps/api/geocode/json?latlng=" + $scope.lat + "," + $scope.lng).then(function(response) {
$scope.data.address = response.data.results[0].formatted_address;
});
}
});
};
$scope.pickCurrentLocation();
});
first download geolocation plugin
cordova plugin add cordova-plugin-geolocation
Paste the below code in your controller..
$ionicPlatform.ready(function() {
$scope.pickCurrentLocation = function() {
var postOptions = {
timeout: 50000,
enableHighAccuracy: false
};
$cordovaGeolocation.getCurrentPosition(postOptions).then(function(position) {
$scope.location = {};
$scope.lat = position.coords.latitude;
$scope.lng = position.coords.longitude;
if ($scope.lat && $scope.lng) {
$http.get("http://maps.google.com/maps/api/geocode/json?latlng=" + $scope.lat + "," + $scope.lng).then(function(response) {
$scope.data.address = response.data.results[0].formatted_address;
});
}
});
};
$scope.pickCurrentLocation();
});
0 comments:
Post a Comment