【问题标题】:leaflet doesnt load url first time传单第一次不加载网址
【发布时间】:2016-09-28 15:30:35
【问题描述】:

我有这段代码用于在 get 函数中加载非地理地图,以获取我每次都需要的地图:

$scope.tableFloors = data;

//Creamos las dimensiones del mapa e inicializamos la url de la imagen correspondiente al plano
$scope.url = $scope.tableFloors.results[0].idBluePrint;
console.log("url: " + $scope.url);
//INICIALIZACIÓN DE VARIABLES NECESARIAS PARA GENERAR EL MAPA
//zoom mínimo para el mapa
$scope.minZoom = 1;
//zoom máximo para el mapa
$scope.maxZoom = 4;
//posición del centro para el mapa
$scope.center = [0, 0];
//zoom por defecto al cargar el mapa
$scope.zoom = 3;
//sistema de referencia de coordenadas
$scope.crs = L.CRS.Simple;
//atributo para mostrar/ocultar información extra
$scope.attributionControl = false;
$scope.map = L.map('image-map',
{
    minZoom: $scope.minZoom,
    maxZoom: $scope.maxZoom,
    center: $scope.center,
    zoom: $scope.zoom,
    crs: $scope.crs,
    attributionControl: $scope.attributionControl
});

//obtenemos el ancho y el alto de la foto del plano
$scope.fotoPlano = new Image();
$scope.fotoPlano.src = $scope.url;
$scope.width = $scope.fotoPlano.width;
$scope.height = $scope.fotoPlano.height;
// Calculamos los bordes de la imagen en el espacio de coordenadas
$scope.surOeste = $scope.map.unproject([0, $scope.height], $scope.map.getMaxZoom() - 1);
$scope.norEste = $scope.map.unproject([$scope.width, 0], $scope.map.getMaxZoom() - 1);;
$scope.bounds = new L.LatLngBounds($scope.surOeste, $scope.norEste);
// lo añadimos
L.imageOverlay($scope.url, $scope.bounds).addTo($scope.map).bringToFront();
$scope.myIcon = L.icon(
{
    iconUrl: 'assets/images/ic_location.png',
    // iconRetinaUrl: 'my-icon@2x.png',
    iconSize: [34, 34],
    iconAnchor: [22, 94],
    // popupAnchor: [-3, -76],
    // shadowUrl: 'my-icon-shadow.png',
    // shadowRetinaUrl: 'my-icon-shadow@2x.png',
    // shadowSize: [68, 95],
    // shadowAnchor: [22, 94]
});

// establecemos los límites
$scope.map.setMaxBounds($scope.bounds);
$scope.yx = L.latLng;
$scope.xy = function (x, y)
{
    if (L.Util.isArray(x))
    { // When doing xy([x, y]);
        return $scope.yx(x[1], x[0]);
    }
    return $scope.yx(y, x); // When doing xy(x, y);
};

var puntoIncidencia1 = $scope.xy(100, -40);

var incidencia1 = L.marker(puntoIncidencia1,
{
    icon: $scope.myIcon,
    draggable: true
}).addTo($scope.map).bindPopup('incidencia1');
//version con mapclickevent
$scope.map.on('click',
    function mapClickListen(e)
    {
        var pos = e.latlng;
        console.log("pos: ", this.options);
        console.log('map click event');
        var marker = L.marker(
            pos,
            {
                draggable: true
            }
        );
        marker.on('drag', function (e)
        {
            console.log('marker drag event');
        });
        marker.on('dragstart', function (e)
        {
            console.log('marker dragstart event');
            $scope.map.off('click', mapClickListen);
        });
        marker.on('dragend', function (e)
        {
            console.log('marker dragend event');
            setTimeout(function ()
            {
                $scope.map.on('click', mapClickListen);
            }, 10);
        });
        marker.addTo($scope.map);
    }
);

但我第一次调用该函数时它没有正确加载图像,或者如果我第一次调用时删除缓存也不会加载它。当我调用该功能时它会正确加载,之后如果我不删除缓存它总是加载正常,它只是第一次没有正确加载......为什么?

【问题讨论】:

  • 好吧,我知道问题出在哪里,问题是我第一次这样称呼:$scope.fotoPlano = new Image(); $scope.fotoPlano.src = $scope.url; $scope.width = $scope.fotoPlano.width; $scope.height = $scope.fotoPlano.height;高度和宽度都是 0 但我不理解第二次需要很好的宽度和高度.....任何解决方案??
  • 可能是图像没有被加载。当你刷新它时,图像已经加载...检查图像的加载事件,当图像加载时,然后做你想做的事
  • 我知道问题所在,你的评论比我快 xd,它的宽度和高度第一次为 0,但我不明白为什么或如何用优雅/好的方式解决这个问题解决方案
  • $scope.fotoPlano = new Image(); $scope.fotoPlano.addEventListener('load', function() { $scope.fotoPlano.src = $scope.url; $scope.width = $scope.fotoPlano.width; $scope.height = $scope.fotoPlano.height ; console.log("ancho: " + $scope.width + " 中音: " + $scope.height); }, false);所以,如果我理解你的意思,但现在它不起作用,我将控制台日志放入加载功能,它没有进入

标签: javascript angularjs leaflet


【解决方案1】:

在您访问其大小时,您的图片并未加载。

var imageObj = new Image();
imageObj.addEventListener('load', function() { /* ... */ }, false); 
// use the load event to know that image has been loaded 

图像加载完毕后继续。

【讨论】:

  • //obtenemos el ancho y el alto de la foto del plano $scope.fotoPlano = new Image(); $scope.fotoPlano.addEventListener('load', function() { $scope.fotoPlano.src = $scope.url; $scope.width = $scope.fotoPlano.width; $scope.height = $scope.fotoPlano.height ; console.log("ancho: " + $scope.width + " 中音: " + $scope.height); }, false);所以,如果我理解你的意思,但现在它不起作用,我将控制台日志放入加载功能,它没有进入
  • 你能提供一个jsbin吗
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-07-10
  • 1970-01-01
  • 2014-12-17
  • 2019-12-02
  • 1970-01-01
相关资源
最近更新 更多