2012-03-11 9 views
8

Ho un errore davvero strano che continua a causare il blocco del mio browser solo quando provo a ridimensionare una mappa per adattarla ai marcatori che ho inserito.map.fitBounds (limiti) che causano il blocco del browser

Il mio codice di ridimensionamento per riferimento:

function sizeMap() { 
      // create a new bounds object to define the new boundry 
      var bounds = new google.maps.LatLngBounds(); 
      // loop through each the string latlang values held in the latlng 
      // aray 
      for (var i = 0; i <latlngArray.length ; i++) { 
       // create a new LatLng object based on the string value 
       var tempLatLng = new google.maps.LatLng(latlngArray[i]); 
       // extend the latlng bounds based on the new location 
       bounds.extend(tempLatLng); 
      } 
      // map.fitBounds(bounds); <== everything seems to work fine until this line is uncommented 
     } 

Il problema è che non posso eseguire il debug correttamente utilizzando Firebug perché che congela a! Qualcuno ha qualche idea su quale potrebbe essere il problema?

Grazie in anticipo.

Aggiornamento:

In risposta alla puckheads in discussione il seguente codice mostra dove la matrice LatLng è popolato di iniziare con:

function geocodeAddress (address) { 

      var geocoder = new google.maps.Geocoder(); 
      geocoder.geocode({'address': address}, function(results, status) { 
       if (status == google.maps.GeocoderStatus.OK) { 
        loadMarker(results[0].geometry.location,"Info here",address); 
        latlngArray.push(results[0].geometry.location); <== latlngs are first added here they are as a result of a call to the google geocode service with a standard string address. 
       } else { 
        alert("Geocode was not successful for the following reason: " +" "+ status); 
       } 
      }); 
     } 
+1

Hai risolto la domanda? - In tal caso dovresti accettare o scrivere la tua risposta corretta! – levi

+0

Hai provato con Chrome e la console javascript? – MrUpsidown

risposta

-1

non si crea un nuovo LatLng da una stringa. Viene creato da 2 numeri, anche se sembra che funzionino anche 2 stringhe, una per la latitudine e una per la longitudine. Una stringa non funzionerà.

Quindi si può dire new google.maps.LatLng(-25.363882, 131.044922) o addirittura new google.maps.LatLng('-25.363882', '131.044922') ma non new google.maps.LatLng('-25.363882, 131.044922') Quindi è necessario passare 2 valori separati per nuova google.maps.LatLng ma sembra siete solo di passaggio uno.

http://code.google.com/apis/maps/documentation/javascript/reference.html#LatLng

+0

Ho provato il suggerimento di Marks e il risultato è lo stesso, ho anche provato a convertire le due stringhe risultanti in float usando parseFloat() e ancora nessuna gioia. – user1079178

+1

Puoi fornire un esempio di cosa c'è in latLngArray? – puckhead

0

Per quanto ne so, non è possibile creare un oggetto LatLng da un singolo parametro stringa. Se hai una stringa nel formato "lat, lng", quindi dividi le parti prima di creare l'oggetto LatLng. L'interno del vostro ciclo sarebbe simile a questa:

 var tempLatLng = latlngArray[i].split(','); 
     bounds.extend(new google.maps.LatLng(tempLatLng[0], tempLatLng[1])); 
+0

Il suo array contiene già oggetti LatLng. Non stringhe. – MrUpsidown

-1

Sulla base della sua codice aggiornato, non è necessario creare tempLatLng a tutti. La matrice contiene già oggetti LatLng così si dovrebbe essere in grado di dire bounds.extend(latlngArray[i]) nel ciclo for ...

+0

Provato il tuo suggerimento e l'attenzione non è stata impostata ma ha smesso di congelare grazie a Puckhead, almeno posso eseguire il debug ora. – user1079178

+0

Cosa intendi per messa a fuoco non impostata? Il problema in questione è risolto? – puckhead

+0

Scusate probabilmente non ho spiegato bene, quello che intendo è che il browser non si blocca più ma che la porta della vista non sta ridimensionando i limiti dei marker come dovrebbe. – user1079178

-1

Timer Usa:

var Timer=""; 
    $(window).resize(function() { 
     clearInterval(Timer); 
     run_timer(); 
    }); 
    function run_timer() { 
     Timer = setInterval(function(){sizeMap();}, 1500); 
    } 
    function sizeMap() { 
     clearInterval(Timer); 
     // create a new bounds object to define the new boundry 
     var bounds = new google.maps.LatLngBounds(); 
     // loop through each the string latlang values held in the latlng 
     // aray 
     for (var i = 0; i <latlngArray.length ; i++) { 
      // create a new LatLng object based on the string value 
      var tempLatLng = new google.maps.LatLng(latlngArray[i]); 
      // extend the latlng bounds based on the new location 
      bounds.extend(tempLatLng); 
     } 
     // map.fitBounds(bounds); <== everything seems to work fine until this line is uncommented 
    } 
4

Il problema è come altri hanno già detto, che si tenta di creare un LatLng oggetto con un altro oggetto LatLng come argomento. Accetta solo 2 o 3 argomenti, in cui i primi due sono numeri. https://developers.google.com/maps/documentation/javascript/reference#LatLng

Tuttavia è possibile saltare completamente la parte in cui si crea un nuovo oggetto LatLng e utilizzare quello corrente dall'array all'interno del ciclo.

questo risolve il problema: http://jsfiddle.net/y9ze8a1f/1/

function sizeMap() { 
    // create a new bounds object to define the new boundry 
    var bounds = new google.maps.LatLngBounds(); 
    // loop through each the string latlang values held in the latlng 
    // aray 
    for (var i = 0; i <latlngArray.length ; i++) { 
     // This is causing the issue, you can only create a LatLng with two separate latitude and longitude arguments 
     //var tempLatLng = new google.maps.LatLng(latlngArray[i]); 

     bounds.extend(latlngArray[i]); 
    } 
    map.fitBounds(bounds); 
} 

Se si vuole creare un nuovo LatLng basato su quello vecchio, però, si potrebbe fare che utilizzando i metodi LatLng lat() e lng()

http://jsfiddle.net/q7sh4put/

function sizeMap() { 
    // create a new bounds object to define the new boundry 
    var bounds = new google.maps.LatLngBounds(); 
    // loop through each the string latlang values held in the latlng 
    // aray 
    for (var i = 0; i <latlngArray.length ; i++) { 
     // create a new LatLng object based on the string value 
     var tempLatLng = new google.maps.LatLng(latlngArray[i].lat(), latlngArray[i].lng()); 

     // extend the latlng bounds based on the new location 
     bounds.extend(tempLatLng); 
    } 
    map.fitBounds(bounds); 
} 
+0

Ma qui il problema principale era "sta bloccando il browser". Ed è stato a causa della sceneggiatura che durava a lungo nel ciclo ... L'ho segnato con audacia, per vederlo meglio. – Legionar

+1

Lo so, e il motivo per cui si bloccava era il massimo numero di callstack raggiunto più volte, derivante dall'istanza di'google.maps.LatLng' con l'argomento sbagliato. Quindi il codice vero e proprio che ha fatto accadere questo è stato nel codice di Google, ma è stato causato dal codice che lo chiama. (Il tuo codice originale dove viene raggiunto il callstack: http://jsfiddle.net/y9ze8a1f/2/) – thebreiflabb

+0

ok, grazie ... – Legionar

Problemi correlati