我正在使用Google Fusion Tables API v1来请求以JSON格式加载到融合表中的点kml。我使用这个请求:
<script src="https://www.googleapis.com/fusiontables/v1/query?sql=SELECT * FROM 1CNJWjLDYgBkJGZVslJ67Fak4DyqadEFIabzQ60&callback=dataHandler&key=AIzaSyKBzv-rs32Zat-8fDZuWwOXo7hCtpBR78"></script>
回调函数使用GeoJSON to Google Maps Utility来解析请求中的标记。几何数据位于名为“geometry”的列idx 2中:
function dataHandler(results) {
var rows = results['rows'];
for (var i in rows){
var geometries = rows[i][2]['geometry'];
//alert(geometries.coordinates)
if (geometries) {
var options = {
"icon": "http://labs.google.com/ridefinder/images/mm_20_red.png"
};
var overlay = new GeoJSON(geometries, options);
if (overlay.error){
alert("error in GeoJSON")
}else{
alert(map) // return undefined
overlay.setMap(map); //not working
}
}else {
alert('No geometry column')
}
}
}
JSON或GeoJSON Utility创建的叠加层中没有错误,但即使全局定义,google map var“map”在函数中也是未定义的。这就是为什么overlay.setMap(map)不起作用。我想在定义var map之前调用了回调函数。我该如何解决这个问题?
答案 0 :(得分:0)
<script src="https://www.googleapis.com/fusiontables/v1/query?sql=SELECT * FROM
TABLE_ID&callback=dataHandler&key=API_KEY"></script>
var map;
var overlays = [];
function initialize(){
map = new google.maps.Map(document.getElementById('map'), {
center: new google.maps.LatLng(49.75, -71.25),
zoom: 5,
mapTypeId: 'roadmap'
});
for (var i = 0; i < overlays.length; i++){
overlays[i].setMap(map);
}
}
function dataHandler(results) {
var rows = results['rows'];
var cols = results['columns'];
for (var i = 0; i < cols.length; i++){
if (cols[i] == 'geometry') {
var ColIndex = i;
}
}
if (!ColIndex){
alert('Geometry column "geometry" not found.')
}
for (var i in rows){
var geometries = rows[i][ColIndex]['geometry'];
if (geometries) {
var options = {
"icon": "http://labs.google.com/ridefinder/images/mm_20_red.png"
};
var overlay = new GeoJSON(geometries,options);
if (overlay.error){
alert("error in overlay")
}else{
overlays.push(overlay);
}
}else {
alert("error in geoJSON response")
}
}