这是因为它使用谷歌地图初始化指令的方式
您的文本“59.325, 18.07”已加载到 html 上,这意味着在指令上编译它已经存在的值,否则,将对象 map.center 以它创建的指令的方式传递它不会获得值,架构应该是重构只是为了从对象中获取你的价值,当我创建自己的地图指令时,我发现了这些不同
这是我的指令的一部分:
http://plnkr.co/edit/IIGZ0jKIFna2adRqxZpx?p=preview
mapModule.directive("map", ['$timeout', function ($timeout) {
return {
restrict: "E",
priority: 2000,
scope: {
centerLatitude: "=",
centerLongitude: "=",
refresh: "=",
width: "@",
height: "@",
zoom: "&",
mapTypeId: "@",
panControl: "@",
zoomControl: "@",
scaleControl: "@",
show: "="
},
compile: function (tElem, tAttrs) {
return {
pre: function (scope, element, attrs, ctrl) {
var toResize, toCenter;
var map;
var currentMarkers;
var el = document.createElement("div");
el.style.width = "100%";
if (scope.height) {
el.style.height = scope.height;
}
else {
el.style.height = "450px";
}
element.prepend(el);
carregaMapa();
// update the control
function carregaMapa() {
//// update size
//if (scope.width) element.width(scope.width);
//if (scope.height) element.height(scope.height);
// get map options
var options =
{
center: new google.maps.LatLng(0, 0),
zoom: 3,
mapTypeId: "roadmap",
//styles: [{ featureType: "all", stylers: [{ hue: "#0000ff" }, { saturation: -75 }] }, { featureType: "poi", elementType: "label", stylers: [{ visibility: "off" }] }]
styles: [{ "featureType": "administrative", "elementType": "labels.text.fill", "stylers": [{ "color": "#444444" }] }, { "featureType": "administrative.land_parcel", "elementType": "labels.text.stroke", "stylers": [{ "color": "#d7caca" }] }, { "featureType": "landscape", "elementType": "all", "stylers": [{ "color": "#f2f2f2" }] }, { "featureType": "poi", "elementType": "all", "stylers": [{ "visibility": "off" }] }, { "featureType": "road", "elementType": "all", "stylers": [{ "saturation": -100 }, { "lightness": 45 }] }, { "featureType": "road.highway", "elementType": "all", "stylers": [{ "visibility": "simplified" }] }, { "featureType": "road.arterial", "elementType": "labels.icon", "stylers": [{ "visibility": "off" }] }, { "featureType": "transit", "elementType": "all", "stylers": [{ "visibility": "off" }] }, { "featureType": "water", "elementType": "all", "stylers": [{ "color": "#d7d2d2" }, { "visibility": "on" }] }]
};
if (scope.centerLatitude) options.center = getLocation(scope.centerLatitude, scope.centerLongitude);
if (scope.zoom()) options.zoom = scope.zoom();
//if (scope.mapTypeId) options.mapTypeId = scope.mapTypeId;
//if (scope.panControl) options.panControl = scope.panControl;
//if (scope.zoomControl) options.zoomControl = scope.zoomControl;
//if (scope.scaleControl) options.scaleControl = scope.scaleControl;
// create the map
map = new google.maps.Map(el, options);
ctrl.mapaInstacia = map;
//google.maps.event.addListener(map, 'bounds_changed', function () {
// ctrl.lastBound = map.getBounds();
//});
}
scope.$watch("show", function () {
if (!angular.isDefined(scope.show))
return;
if (!map || !ctrl.carregado) {
ctrl.carregado = true;
ctrl.fitBounds();
return;
}
if (ctrl.eventoZoomAdicionado == false) {
ctrl.eventoZoomAdicionado = true;
google.maps.event.addListener(map, 'zoom_changed', function () {
ctrl.lastBound = map.getBounds();
});
$timeout(function () {
google.maps.event.trigger(map, "resize");
ctrl.mapaInstacia.fitBounds(ctrl.lastBound);
});
}
//bug for some reason, google maps goes to zoom zero
if (map.getZoom() == 0) {
if (ctrl.markers.length > 1) {
$timeout(function () {
ctrl.fitBounds();
}, 400);
}
}
});
scope.$watch("refresh", function () {
if (map && scope.refresh == true) {
scope.refresh = false;
if (ctrl.markers.length > 1) {
ctrl.fitBounds();
}
else {
$timeout(function () {
var mapCenter = map.getCenter();
var mapZoom = map.getZoom();
google.maps.event.trigger(map, "resize");
map.setZoom(mapZoom);
map.setCenter(mapCenter);
}, 100);
}
}
});
// convert current location to Google maps location
function getLocation(lat, log) {
return new google.maps.LatLng(lat, log);
}
}
}
},
controller: mapModule.mapController
};
}]);