2

我有一个 socket.io,它每 15-20 毫秒 ping 一个新地址。对于这个地址,我必须获取经纬度并将标记放在谷歌地图中。所以在这 15-20 毫秒内(如果不是,可能在 50-60 毫秒内)我必须得到 GeoLocation。目前我正在使用geocoder = new google.maps.Geocoder();然后geocoder.geocode({address: data}, myFunction(){});

但是这个用于地图的 API 非常慢。它在 400-500 毫秒内返回 GeoLocation,这使我的中间地址请求为空。我需要一个非常快的 API。

作为参考,下面是 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
};
4

1 回答 1

0

正如评论中所提到的,您实际上不能期望互联网上的 20 毫秒内得到响应,它只是不能那样工作。但是,您可以做的是使用地址创建一个池,并让地理编码器(或者可能是 4 个中的 3 个)按照自己的步调处理它。

这可能看起来有点像这样(只是在这里给出一个方向,不要指望它立即起作用):

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();
于 2013-02-25T21:44:08.887 回答