角度指令是't更新谷歌地图值
Angular Directive isn't updating google map values
我有一个名为"myMap"的指令,用于添加谷歌地图。当我试图使用控制器功能在不同的位置再次更新经度和纬度时,它不会更新指令值。显示的是名称地图。
这是我的指令:
directive('myMap', function () {
// directive link function
var link = function (scope, element, attrs) {
var map, infoWindow;
var markers = [];
// map config
var mapOptions = {
center: new google.maps.LatLng(attrs.latitude, attrs.longitude),
zoom: 12,
mapTypeId: google.maps.MapTypeId.ROADMAP,
scrollwheel: false
};
// init the map
function initMap() {
if (map === void 0) {
map = new google.maps.Map(element[0], mapOptions);
}
}
// place a marker
function setMarker(map, position, title, content) {
var marker;
var markerOptions = {
position: position,
map: map,
title: title,
icon: 'https://maps.google.com/mapfiles/ms/icons/green-dot.png',
animation: google.maps.Animation.Bounce
};
marker = new google.maps.Marker(markerOptions);
markers.push(marker); // add marker to array
google.maps.event.addListener(marker, 'click', function () {
// close window if not undefined
if (infoWindow !== void 0) {
infoWindow.close();
}
// create new window
var infoWindowOptions = {
content: content
};
infoWindow = new google.maps.InfoWindow(infoWindowOptions);
infoWindow.open(map, marker);
});
}
// show the map and place some markers
initMap();
setMarker(map, new google.maps.LatLng(attrs.latitude, attrs.longitude), attrs.restname, attrs.address);
};
return {
restrict: 'A',
template: '<div id="gmaps"></div>',
replace: true,
link: link
};
});
在那之后,我从我的HTML调用这个指令。这是我的HTML:
<div class="gmaps" my-map="" latitude="{{home.latitude}}" longitude="{{home.longitude}}"></div>
我受够了。我使用了不同的方法,但在我的情况下不起作用。如何检查指令参数的不同变化,以便分析经度和纬度的更新值?请帮帮我?
在指令中添加观察程序。
attrs.$observe("value", function (data) {},true);
它观察指令参数的变化,并在发生变化时进行更新
在你的情况下,它将是
lahorefoodsWebSiteModule.directive('myMap', function () {
// directive link function
var link = function (scope, element, attrs) {
attrs.$observe("latitude", function (latitude) {
//This gets called when data changes.
var map, infoWindow;
var markers = [];
// map config
var mapOptions = {
center: new google.maps.LatLng(attrs.latitude, attrs.longitude),
zoom: 12,
mapTypeId: google.maps.MapTypeId.ROADMAP,
scrollwheel: false
};
// init the map
function initMap() {
if (map === void 0) {
map = new google.maps.Map(element[0], mapOptions);
}
}
// place a marker
function setMarker(map, position, title, content) {
var marker;
var markerOptions = {
position: position,
map: map,
title: title,
icon: 'https://maps.google.com/mapfiles/ms/icons/green-dot.png',
animation: google.maps.Animation.Bounce
};
marker = new google.maps.Marker(markerOptions);
markers.push(marker); // add marker to array
google.maps.event.addListener(marker, 'click', function () {
// close window if not undefined
if (infoWindow !== void 0) {
infoWindow.close();
}
// create new window
var infoWindowOptions = {
content: content
};
infoWindow = new google.maps.InfoWindow(infoWindowOptions);
infoWindow.open(map, marker);
});
}
// show the map and place some markers
initMap();
setMarker(map, new google.maps.LatLng(attrs.latitude, attrs.longitude), attrs.restname, attrs.address);
},true);
};
return {
restrict: 'A',
template: '<div id="gmaps"></div>',
replace: true,
link: link
};
});
Html代码将相同。希望它能解决你的问题。
相关文章:
- 角度指令是't更新谷歌地图值
- 用sql数据库更新谷歌地图标记
- 仅更新谷歌地图中的标记
- 在不加载页面的情况下更新谷歌地图上的标记位置
- 使用 AJAX 动态更新谷歌地图
- 如何更新谷歌地图引擎表
- 页面加载时从邮政编码更新谷歌地图
- 如何在用户滚动时更新谷歌地图中心
- 如何更新谷歌地图,如果它的地图画布改变了它的尺寸
- 如何更新谷歌地图大头针而不刷新整个地图
- 更新谷歌地图制作不透明度,无需重新加载页面
- 无论如何,限制更新谷歌地图时平移
- 更新谷歌地图的缩放矩形
- 更新谷歌地图标记没有重新加载地图和眨眼
- 更新谷歌地图位置通过javascript
- 通过它更新谷歌地图标记位置's的ID
- 更新谷歌地图标记位置
- ASP:使用新坐标动态更新谷歌地图引擎实例
- 如何更新谷歌地图中多个标记的位置
- 不断更新谷歌地图与用户位置