当我使用数据绑定时,angular地理编码器失败
angular geocoder fails when I use data binding
我使用的是:http://jasonwatmore.com/post/2014/02/15/AngularJS-Reverse-Geocoding-Directive.aspx
基本上,当我使用时,它工作得很好
<reverse-geocode lat="40.730885" lng="-73.997383" />
但是,问题是。。
当我有这样的:
<reverse-geocode lat="{{vm.user.location[1]}}" lng="{{vm.user.location[0]}}"></reverse-geocode>
它不适用于{{vm.user.location[1]}}和{vm.user.location[0]}的值,因为出于某种原因,它传递的是0而不是实际值。。
当我把它输入到一个跨度中时,值会显示得很好。
<span> Lat:{{vm.user.location[1]}}, Long:{{vm.user.location[0]}} </span>
我不知道发生了什么:(
为什么它认为我传递的是0的值,而不是数据库中的实际lat/long值?
您需要观察属性的变化,因为您是动态加载位置数据的,所以当这些值发生变化时,您还需要更新指令。
以下是如何添加观察行为:
.directive('reverseGeocode', function ($timeout) {
return {
restrict: 'E',
template: '<div></div>',
link: function (scope, element, attrs) {
attrs.$observe('lat', geoCode);
attrs.$observe('lng', geoCode);
function geoCode() {
$timeout(function() {
var geocoder = new google.maps.Geocoder();
var latlng = new google.maps.LatLng(attrs.lat, attrs.lng);
geocoder.geocode({ 'latLng': latlng }, function (results, status) {
if (status == google.maps.GeocoderStatus.OK) {
if (results[1]) {
element.text(results[1].formatted_address);
} else {
element.text('Location not found');
}
} else {
element.text('Geocoder failed due to: ' + status);
}
});
});
}
},
replace: true
}
})
演示:http://plnkr.co/edit/PehccH32xMzlbhsRD0FI?p=info
相关文章:
- 地理编码器 - IE8 中的编码问题
- 谷歌地图API地理编码器,受边界限制
- 将地理编码器的位置保存到数组中
- 在运行时从Angular注入器检索实例
- 在一个模式AjAX编码器中显示不同的Gallery
- 当点击一个标记时,地理编码器返回先前存储的值
- 谷歌地理编码器找到佐治亚州,而不是国家
- 如何防止谷歌地理编码器从其他国家/地区返回结果
- 防止在地理编码器谷歌地图API中循环
- 地理编码器仅适用于chrome,不适用于Firefox / IE / android
- 净化编码器中的输出
- 谷歌地理编码器返回错误的地址坐标
- 列出使用 Google 地图地理编码器 API 获取的一组地址
- 编码器点火器未验证输入文本
- 多个 Angular 验证器独立工作,但不能一起工作
- 地理编码器 API 仅返回街道地址
- 如何在编码器中使用具有相同名称的多个输入插入数据
- 谷歌地图地理编码器,计算距离,
- Windows媒体编码器9插入(链接,脚本)处理
- 当我使用数据绑定时,angular地理编码器失败