Angularjs 地理位置获取城市
我在app.js中有以下代码。我想在$scope.city变量中捕获位置城市。我已经在geocode函数之外声明了city,但是city并没有得到任何值。需要你的帮助吗Angularjs 地理位置获取城市,angularjs,Angularjs,我在app.js中有以下代码。我想在$scope.city变量中捕获位置城市。我已经在geocode函数之外声明了city,但是city并没有得到任何值。需要你的帮助吗 // code as below app.controller('ydmoreCtrl', function($scope, $timeout, $ionicModal, $ionicSideMenuDelegate, Categories) { (navigator.geolocation) { navigato
// code as below
app.controller('ydmoreCtrl', function($scope, $timeout, $ionicModal, $ionicSideMenuDelegate, Categories) {
(navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position){
var latlng = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
var geocoder = new google.maps.Geocoder();
var city;
geocoder.geocode({'latLng': latlng}, function(results, status)
{
if (status == google.maps.GeocoderStatus.OK)
{
if (results[0])
{
var add= results[0].formatted_address ;
var value=add.split(",");
count=value.length;
country=value[count-1];
state=value[count-2];
city=value[count-3];
callback(latlng);
// alert(city);
}
else
{
alert("address not found");
}
}
});
$scope.$apply(function(){
$scope.city = "Pune";
});
});
}
/*
*/
你可以试着打电话
$scope.apply
在城市被设置为
$scope.city
内部到geocoder.geocode调用