简体   繁体   中英

Google Maps Error: not a LatLng or LatLngLiteral: not an Object - seems to be 'bounds' related

I import addresses from a google-spreadsheet and put them on a google map. The Location (latitude & longitude) are stored in a variable. I loop through all entries and create the markers. The connection between Google Spreadsheet and the website works, but it only shows the first entry. So it seem to be an issue with the loop.

First I thought it is a problem with the Values in the _location.latitude/_location.longitude and I tried setting the.value there, but this isn't the issue.

The Loop is in the _setLocations Function and from my search, it seems that var _bounds or the _bounds.extend has some issues

_createGoogleMap: function(_mapID){

          var _this = this;

          var _locations = [];

          var _sheetUrl = 'https://sheets.googleapis.com/v4/spreadsheets/xxxxxxxxxxxxxxxxxxx/values/Sheet1!A2:U?key=xxxxxxxxxxxxxxxxxxxxxx';

          var _map = new google.maps.Map(document.getElementById(_mapID), this._mapProp);

          $.getJSON(_sheetUrl, function(data) {
            $(data.values).each(function() {
              var _location = {};
                _location.latitude = parseFloat(this[8]);
                _location.longitude = parseFloat(this[9]);
                _locations.push(_location);
            });

            _this._setLocations(_map, _locations);

          });
        },


_setLocations: function(_map, _locations) {

          var _this = this;

          var _bounds = new google.maps.LatLngBounds();

          var _infowindow = new google.maps.InfoWindow({
            content: "Content String"
          });
          for (var i = 0; i < _locations.length; i++) {
            var _new_marker = _this._createMarker(_map, _locations[i], _infowindow);

            _bounds.extend(_new_marker._position);
          }
          _map.fitBounds(_bounds);

        },

When I 'console.log(_locations);' before the loop, I get all the addresses, but after the loop I got nothing anymore.

Any help appreciated.

try using a proper LatLng object

   my_location  = new google.maps.LatLng(_locations[i].latitude, _locations[i].longitude) ;
   var _new_marker = _this._createMarker(_map, _my_location, _infowindow);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM