Google maps api 3 如何访问使用loadGeoJson()加载的Google Map API v3功能
我正在使用Google Maps API,并尝试访问使用loadGeoJson()加载的功能。文档似乎建议map.data.forEach(function(feature))应该能够遍历所有特性。但当我使用loadGeoJson加载数据时,它会在地图上创建pin,而不会在数据中创建任何特征 例如: 我的代码尝试将功能数据输出到console.log(见下文),因此,如果您访问该页面并打开开发人员工具,您将能够看到尝试访问数据失败的各种方式 这是我的地图代码:Google maps api 3 如何访问使用loadGeoJson()加载的Google Map API v3功能,google-maps-api-3,geojson,Google Maps Api 3,Geojson,我正在使用Google Maps API,并尝试访问使用loadGeoJson()加载的功能。文档似乎建议map.data.forEach(function(feature))应该能够遍历所有特性。但当我使用loadGeoJson加载数据时,它会在地图上创建pin,而不会在数据中创建任何特征 例如: 我的代码尝试将功能数据输出到console.log(见下文),因此,如果您访问该页面并打开开发人员工具,您将能够看到尝试访问数据失败的各种方式 这是我的地图代码: map = new googl
map = new google.maps.Map(document.getElementById('map'), {
center: {lat: 40.397, lng: 0.644},
zoom: 6
});
var json = 'overflow.geojson';
map.data.loadGeoJson(json);
console.log("Logging the data features:");
console.log(map.data.features);
console.log("Using map.data.forEach:");
map.data.forEach(function(feature) {
});
console.log("Here's the whole map object:");
console.log(map);
}
以及正在加载的GeoJson:
我的数据到哪里去了 是异步的。在数据可用之前,您需要等待回调运行
从
loadGeoJson(url[,选项,回调])参数:
url:string
选项(可选):Data.GeoJsonOptions
回调(可选):函数(数组)
返回值:无
从URL加载GeoJSON,并将功能添加到集合中。
注意:GeoJSON是使用XHR获取的,可能无法跨域工作。如果您有问题,我们建议您使用您选择的AJAX库获取GeoJSON,然后调用addGeoJson() 或者您可以使用回调函数中传递的
功能
数组:
map.data.loadGeoJson('https://storage.googleapis.com/mapsdevsite/json/google.json', {}, function(features) {
console.log("Logging the data features:");
console.log(features);
console.log("Using map.data.forEach:");
features.forEach(function(feature) {
console.log(feature);
});
})
代码片段:
函数初始化(){
map=new google.maps.map(document.getElementById('map'){
中心:{
拉丁语:-28,
液化天然气:137
},
缩放:4
});
map.data.loadGeoJson('https://storage.googleapis.com/mapsdevsite/json/google.json“,{},函数(特征){
log(“记录数据特性:”);
//请注意,没有map.data.features属性,但回调将返回添加的特性数组
console.log(特性);
log(“使用map.data.forEach:”);
map.data.forEach(函数(特性){
console.log(特性);
});
});
}
google.maps.event.addDomListener(窗口“加载”,初始化)代码>
html,
身体,
#地图{
身高:100%;
宽度:100%;
边际:0px;
填充:0px
}
map.data.loadGeoJson('https://storage.googleapis.com/mapsdevsite/json/google.json', {}, function(features) {
console.log("Logging the data features:");
// note that there is no map.data.features property, but the callback returns the array of added features
console.log(map.data.features); // == undefined
console.log(features); // array of added features
console.log("Using map.data.forEach:");
map.data.forEach(function(feature) {
console.log(feature);
});
});
map.data.loadGeoJson('https://storage.googleapis.com/mapsdevsite/json/google.json', {}, function(features) {
console.log("Logging the data features:");
console.log(features);
console.log("Using map.data.forEach:");
features.forEach(function(feature) {
console.log(feature);
});
})