Come faccio a trovare (velocemente - 15-20ms) LatLong per un indirizzo utilizzando Javascript?

voti
2

Ho un socket.io cui ping un nuovo indirizzo ogni 15-20ms. Per questo Indirizzo, devo ottenere il Lat-Long e posizionare il marcatore in Google Maps. Quindi, all'interno di quelle 15-20ms (se non, possono essere situati all'interno 50-60ms) devo ottenere la geolocalizzazione. Attualmente sto usando geocoder = new google.maps.Geocoder();e poigeocoder.geocode({address: data}, myFunction(){});

Ma questa API per mappe è molto lenta. Si ritorna in GeoLocation 400-500ms che rende il mio indirizzo intermedio richiede nulla. Ho bisogno di un'API che è molto veloce.

Per riferimento, qui di seguito è il frammento di codice per socket.io:

geocoder = new google.maps.Geocoder();
    var socket = io.connect('http://localhost');
    socket.on('new_address', function (data) {
        //Gets called everytime a new request for GeoLocation comes
        geocoder.geocode({address: data}, placeMarker);
    });

var placeMarker = function(){
    //Add Marker to GoogleMaps
};
È pubblicato 25/02/2013 alle 13:29
fonte dall'utente
In altre lingue...                            


1 risposte

voti
0

Come accennato nei commenti non si può effettivamente aspettare una risposta entro 20 ms su internet, semplicemente non funziona in questo modo. Che cosa si può però fare è fare una sorta di piscina con gli indirizzi e lasciare che il geocoder (o forse 3 di 4) lavorare su di esso su di essa la propria andatura.

Questo sarebbe probabilmente guardare un po 'come questo (solo dare una direzione qui, non si aspettano di lavorare fin da subito):

var addresses = [];
var socket = io.connect('http://localhost');
socket.on('new_address', function (data) {
    //Gets called everytime a new request for GeoLocation comes
    //Adds an address to the list when it comes in from the backend
    adresses.push(data);
});

var geocoder = new google.maps.Geocoder();
//This function is called in a loop.
var addressCheck = function() {
    //When the list of addresses is empty, because we haven't received anything from the backend, just wait for a bit and call this function again.
    if(addresses.length == 0) {
        setTimeout(addressCheck, 400);
        return;
    }
    //Get the first one on the list.
    var data = addresses[0];
    //Process it.
    geocoder.geocode({address: data}, function() {
        placeMarker();
            //remove the first element from the adresses list.
        addresses.shift();
            //Call the entire function again, so it starts with a new address.
        addressCheck();
    });
}
var placeMarker = function(){
    //Add Marker to GoogleMaps
};

addressCheck();
Risposto il 25/02/2013 a 22:44
fonte dall'utente

Cookies help us deliver our services. By using our services, you agree to our use of cookies. Learn more