javascript - 如何在我的所有课程中使用 API 结果?
问题描述
我用一种方法创建了一个JS
类MapController
,该方法调用API
JC Decaux
. 在成功的情况下,此方法initJcDecaux()
在谷歌地图上创建不同的标记。但我会将结果分配$ajax()
给我的公共财产this.data
。我尝试了这段代码,但它并不好......如果有人可以帮助我?谢谢你。
class MapController {
constructor(mapIdPosition) {
this.mapIdPosition = mapIdPosition;
this.position = {lat: 43.6009272, lng: 1.4452841};
this.zoom = 14;
this.markers = [];
this.data = [];
}
initMap() {
this.map = new google.maps.Map(document.getElementById(this.mapIdPosition), {
center: this.position,
zoom: 5,
})
this.map.setZoom(this.zoom);
}
initJcDecaux() {
$.ajax({
url: 'https://api.jcdecaux.com/vls/v1/stations?contract=Toulouse&apiKey=6d8e030000000000000000000000000000',
type: 'GET',
datatype: 'json',
success: function(data) {
this.data = data;
data.forEach(this.addMarkerOnMap.bind(this));
var markerCluster = new MarkerClusterer(this.map, this.markers,
{imagePath: 'https://developers.google.com/maps/documentation/javascript/examples/markerclusterer/m'});
}.bind(this)
})
}
addMarkerOnMap(data) {
this.marker = new google.maps.Marker({
position: data.position,
map: this.map,
title: '',
})
this.markers.push(this.marker);
}
};
const map1 = new MapController('map');
map1.initMap();
map1.initJcDecaux();
console.log(map1.data);
解决方案
推荐阅读
- authentication - 如何在 Click-once 应用程序部署中添加身份验证
- javascript - 单击时使用布尔值,然后使用 if 语句检查布尔值以运行其他代码
- php - 将页面的xml文件导入wordpress并设置父页面或永久链接
- xslt - 如何使用 xslt 添加根元素?
- python - Flask 错误 werkzeug 路由故障排除
- postgresql - 在 PostgreSQL 中,2019-04-23T22:34:40.253-05:00 的日期时间格式是什么?
- python - 如何分组列并返回一个数据框而不是分组对象
- python - 是否有替代方法:as_integer_ratio(),以获得“更清洁”的分数?
- java - 我们是否有任何用于使用 apache poi 在 excel 中取消保护工作表的功能?
- python - 如何仅在实例级别覆盖 __eq__?