小编use*_*219的帖子

Google使用javascript在循环中对多个地址进行地理编码,如何知道何时完成所有操作?

我有一个表格要求提供一个位置列表(不是很多,通常只有3或4但这个数字是动态的).提交表单时,我必须解析数据,使用Google地理编码获取位置,然后绘制一条连接点的行.我解析工作,但我坚持使用地理编码部分,主要是因为它的异步性质.假设我的地址字符串存储在数组'addresses'中,这是我得到的:

function someFunction(addresses) {
  var coords = [];
  for(var i = 0; i < addresses.length; i++) {
    currAddress = addresses[i];
    var geocoder = new google.maps.Geocoder();
    if (geocoder) {
      geocoder.geocode({'address':currAddress}, function (results, status)
        if (status == google.maps.GeocoderStatus.OK) {
          coords.push(results[0].geometry.location);
        } 
        else {
          throw('No results found: ' + status);
        }
      });
    }
  }
  // Initially I tried to work with the data here, but it wasn't all present yet.
}
Run Code Online (Sandbox Code Playgroud)

绘制线条很容易,我已经在用户提供地理纬度/经度坐标之前完成了这项工作.我的问题是,因为坐标只是在回调中添加,我怎么知道它什么时候完成?我不能将它转储到函数中并放入回调因为我需要等到所有坐标都被处理完毕.

我还读到了一些有问题的人没有按顺序返回,但我不理解提供的回复.如果某人有答案可以帮助我解决我的具体问题,并确保结果恢复正常,我将不胜感激.

注意:我手动轰炸了那段代码,所以可能会出现错别字.到目前为止,我的实际代码"有效",我只是不知道在处理完所有地址后,我将从做什么转移到做什么.此外,目前正在开发这种测试的内部应用程序.测试完成后,它将完全符合Google的服务条款.这意味着我没有可以链接到的页面.整个应用程序还有超过2,000行代码,并且此时包含一些专有的公司信息,最终将逐步淘汰,因此粘贴整个内容或将其发送出去是不可行的.我希望这不会构成太大的问题.

javascript asynchronous callback google-maps-api-3 geocode

13
推荐指数
2
解决办法
2万
查看次数

C++在前缀重载中返回引用变量

当我找到的所有代码显示在重载前缀运算符时返回引用变量时,我感到困惑.我浏览了parashift.com常见问题解答(http://www.parashift.com/c++-faq-lite/operator-overloading.html#faq-13.14)并不清楚,尽管它说它会很清楚当你读它.我把他们的例子改编成了一个荒谬的,毫无意义的小程序来测试.

#include<iostream>
using namespace std;

class Number {
   public:
      Number& operator++ ();    // prefix ++
      Number  operator++ (int); // postfix ++
      int value() { return value_; }
      void setValue(int value) { value_ = value; }
   private:
      int value_;
};

Number& Number::operator++ () {
   ++value_;
   return *this;
}

Number Number::operator++ (int unused) {
   Number temp;
   temp.setValue(value_);
   ++value_;
   return temp;
}

int main()
{
   Number someNum;
   someNum.setValue(20);
   cout << "someNum : " << someNum.value() << "\n";
   someNum++;
   ++someNum;
   cout << "someNum : …
Run Code Online (Sandbox Code Playgroud)

c++ overloading reference prefix

1
推荐指数
1
解决办法
1694
查看次数