Google Maps API:鼠标悬停仅适用于一天中的特定时段

时间:2015-01-12 20:56:33

标签: javascript google-maps maps

我必须说这是我在编程冒险过程中遇到过的最奇怪的错误。我真的不知道为什么会这样。

基本上,我的应用程序必须从Mysql数据库接收一些数据,当用户在一个国家/地区点击时,infowindow必须出现一些信息(来自数据库)。数据库通信工作正常,问题是:当我将鼠标移到某个国家/地区时,有时鼠标移除事件不会被调用,鼠标输出也是如此。

但有时呢?用什么频率?这是最奇怪的部分:在一天中的特定时间。我是巴西人,我在这里使用UTC -3,如果我在18:00访问我的应用程序,它不起作用,但是如果我在早上访问它,就像早上7点一样,它可以工作。

我真的不知道为什么会发生这种情况,试图在网上搜索但却一无所获......

有人能帮助我吗?这是我使用的代码(在js控制台中没有出现错误,错误发生在chrome,firefox,IE ......):

<script type="text/javascript" >
      var colors = ['#FF0000', '#00FF00', '#0000FF', '#FFFF00'];
      var map
      var infowindow;

      function initialize() {
        var myOptions = {
          zoom: 2,
          center: new google.maps.LatLng(10, 0),
          mapTypeId: google.maps.MapTypeId.ROADMAP
        };

        map = new google.maps.Map(document.getElementById('map-canvas'),
            myOptions);

    geocoder = new google.maps.Geocoder();


        // Initialize JSONP request
        var script = document.createElement('script');
        var url = ['https://www.googleapis.com/fusiontables/v1/query?'];
        url.push('sql=');
        var query = 'SELECT name, kml_4326 FROM ' +
            '1foc3xO9DyfSIF6ofvN0kp2bxSfSeKog5FbdWdQ';
        var encodedQuery = encodeURIComponent(query);
        url.push(encodedQuery);
        url.push('&callback=drawMap');
        url.push('&key=AIzaSyAm9yWCV7JPCTHCJut8whOjARd7pwROFDQ');
        script.src = url.join('');
        var body = document.getElementsByTagName('body')[0];
        body.appendChild(script);
      }

      function drawMap(data) {
        var rows = data['rows'];
        for (var i in rows) {
          if (rows[i][0] != 'Antarctica') {
            var newCoordinates = [];
            var geometries = rows[i][1]['geometries'];
            if (geometries) {
              for (var j in geometries) {
                newCoordinates.push(constructNewCoordinates(geometries[j]));
              }
            } else {
              newCoordinates = constructNewCoordinates(rows[i][1]['geometry']);
            }
            //var randomnumber = Math.floor(Math.random() * 4);
            var country = new google.maps.Polygon({
              paths: newCoordinates,
              //strokeColor: colors[randomnumber],
              strokeOpacity: 0,
              strokeWeight: 1,
              //fillColor: colors[randomnumber],
              fillOpacity: 0
            });

        infowindow =  new google.maps.InfoWindow({
            content: ''
        });

            google.maps.event.addListener(country, 'mouseover', function() {
              alert("HIIIIII JOW");
              this.setOptions({fillOpacity: 0.3});
            });
            google.maps.event.addListener(country, 'mouseout', function() {
              this.setOptions({fillOpacity: 0});
            });
            google.maps.event.addListener(country, 'click', function(event) {
                var lat = event.latLng.lat();
                var lng = event.latLng.lng();

                initialize2(lat,lng);

            });
            country.setMap(map);
          }
        }
      }

      function constructNewCoordinates(polygon) {
        var newCoordinates = [];
        var coordinates = polygon['coordinates'][0];
        for (var i in coordinates) {
          newCoordinates.push(
              new google.maps.LatLng(coordinates[i][1], coordinates[i][0]));
        }
        return newCoordinates;
      }


      function initialize2(lat,lng){
        var url = "http://maps.googleapis.com/maps/api/geocode/json?latlng=" + lat + "," + lng+ "&sensor=true&language=en";
        jQuery.getJSON(url, function (data) {

        for (var i = 0; i < data.results[0].address_components.length; i++){
            //var formatted = results[0].formatted_address;//.short_name
            var countryName
            var shortname = data.results[0].address_components[i].short_name;
            var longname = data.results[0].address_components[i].long_name;
            var type = data.results[0].address_components[i].types;
            if (type.indexOf("country") != -1){
                if (isNullOrWhitespace(shortname)){
                    countryName = shortname;
                }
                else{
                    countryName = longname;
                }
            }
        }
        var lat2;
        var lng2
        geocoder.geocode( {'address' : countryName}, function(results, status) {
            if (status == google.maps.GeocoderStatus.OK) {
                lat2 = results[0].geometry.location.lat();
                lng2 = results[0].geometry.location.lng();
                //alert(results[0].geometry.location);//map.setCenter(results[0].geometry.location);
                //alert(lat2+','+lng2);
                var array = <?php echo json_encode($arr); ?>;

                var str = 'Nenhuma exchange cadastrada neste país!';

                for(var i = 0;i<array.length;i++){

                    if(countryName==array[i][0]){
                        var string = 'country_volume.php?country=' + countryName;
                      str = '<center><strong>'+array[i][0]+'</strong></center>' +
                            '<strong>Número de Exchanges: </strong>' + array[i][1] + '</br>' +
                            '<strong>Volume: </strong>' + array[i][2] + '</br>' +
                            '<strong>Ranking Mundial: </strong> ' + array[i][3] + 'º' + '</br>' +
                            '<strong>Porcentagem Mundial (%): </strong> ' + array[i][4] + '%' + '</br>' +
                            '<a href='+string+'>' + 'Mais Informações' + '</a>';
                    }

                }

                if (infowindow) infowindow.close();
                infowindow =  new google.maps.InfoWindow({
                    content: str,
                    map: map,
                position: new google.maps.LatLng(lat2,lng2)
                });

            }
        });

        });
      }

    function isNullOrWhitespace(text) {
        if (text == null) {
            return true;
        }
        return text.replace(/\s/gi, '').length < 1;
    }


      google.maps.event.addDomListener(window, 'load', initialize);
    </script>

1 个答案:

答案 0 :(得分:0)

您可能超过Fusion Tables API的每日quota

默认请求配额限制如下:

每个API项目每天25,000个请求,其中读取计为一个请求,写入计数为五个请求。

每个表每分钟写入30次请求 您可以在“配额”标签下的Google APIs Console申请更多配额。

存储配额限制为:

  • 每桌100 MB
  • 所有表格中总共250 MB

有关详细信息,请参阅here