【发布时间】:2020-03-03 04:13:33
【问题描述】:
我从谷歌电子表格导入地址并将它们放在谷歌地图上。位置(纬度和经度)存储在变量中。我遍历所有条目并创建标记。 谷歌电子表格和网站之间的连接有效,但它只显示第一个条目。所以这似乎是循环的问题。
首先我认为_location.latitude/_location.longitude 中的值有问题,我尝试在那里设置 .value,但这不是问题。
循环在 _setLocations 函数中,从我的搜索来看,var _bounds 或 _bounds.extend 似乎有一些问题
_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);
},
当我 'console.log(_locations);'在循环之前, 我得到了所有的地址,但是在循环之后我什么也没得到 没有了。
任何帮助表示赞赏。
【问题讨论】:
-
谷歌地图
LatLngLiteral具有属性.lat和.lng(不是.latitude和.longitude)。
标签: javascript json google-maps google-maps-api-3