我发现您必须使用我认为的解决方法来从浏览器 API 获取地理位置数据。我的问题是,当我可以在控制台日志中看到输出数据时,为什么不能以正常方式访问它?
例如,在本例中输出纬度和经度,但“ map 输出”从未更新:
function get_location() {
navigator.geolocation.getCurrentPosition(show_map);
console.log('nav: ', navigator);
}
function show_map(position) {
var latitude = position.coords.latitude;
var longitude = position.coords.longitude;
self.mapoutput = "http://maps.googleapis.com/maps/api/staticmap?center=" + latitude + "," + longitude + "&zoom=13&size=300x300&sensor=false";
console.log("latitude: ", latitude);
console.log("longitude: ", longitude);
}
get_location();
我认为 show_map 是异步回调,那么为什么 self.mapoutput 没有正确更新?
最佳答案
Angular 仅在运行 $digest
时更新数据绑定(bind)。对于通过 Angular 函数设置的回调,这已经由 Angular 代码完成。如果您不在 Angular 之外,则必须自己通过将回调正文包装在对 $apply
的调用中来完成此操作。 .
关于javascript - 在 angularjs 中使用地理定位,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25750971/