Angular 2谷歌地图Javascript API,路由服务功能不';不存在
在我的Angular 2项目中,我正在尝试使用谷歌地图上的Javascript API 我遇到的问题基于以下代码:Angular 2谷歌地图Javascript API,路由服务功能不';不存在,javascript,google-maps,angular,Javascript,Google Maps,Angular,在我的Angular 2项目中,我正在尝试使用谷歌地图上的Javascript API 我遇到的问题基于以下代码: @Injectable() export class CalculateAndDisplayRouteService { public durationTrafficSource = new ReplaySubject<string>(); public durationTraffic$ = this.durationTrafficSource.asObservab
@Injectable()
export class CalculateAndDisplayRouteService {
public durationTrafficSource = new ReplaySubject<string>();
public durationTraffic$ = this.durationTrafficSource.asObservable();
public changeDurationTraffic(string) {
this.durationTrafficSource.next(string);
}
public routeResponse(response, status) {
console.log(response);
let map = new google.maps.Map(document.getElementById('map'), {
center: {lat: lat, lng: lng},
zoom: 8
});
if (status === 'OK') {
let directionsDisplay = new google.maps.DirectionsRenderer({
suppressMarkers: false,
suppressInfoWindows: true
});
directionsDisplay.setMap(map);
directionsDisplay.setDirections(response);
this.changeDurationTraffic(response.routes[0].legs[0].duration.text); //Error is here
} else {
window.alert('Directions request failed due to ' + status);
}
}
private currentDate = new Date();
public route(directionsService, transportMode, origin, destination) {
if(transportMode === 'DRIVING') {
directionsService.route({
origin: origin,
destination: destination,
travelMode: transportMode,
drivingOptions: {
departureTime: new Date(this.currentDate.getFullYear(), this.currentDate.getMonth(), this.currentDate.getDate() + 1, 7, 45),
trafficModel: 'pessimistic'
}
}, this.routeResponse);
@Injectable()
导出类CalculateAndDisplayRouteService{
public durationTrafficSource=新的ReplaySubject();
public durationTraffic$=this.durationTrafficSource.asObservable();
public changeDurationTraffic(字符串){
this.durationTrafficSource.next(字符串);
}
公共路由响应(响应、状态){
控制台日志(响应);
让map=new google.maps.map(document.getElementById('map'){
中心:{lat:lat,lng:lng},
缩放:8
});
如果(状态=='OK'){
让directionsDisplay=新建google.maps.DirectionsRenderer({
对:错,,
suppressInfoWindows:true
});
方向显示.setMap(地图);
方向显示。设置方向(响应);
this.changeDurationTraffic(response.routes[0]。legs[0]。duration.text);//此处有错误
}否则{
window.alert('由于'+状态,指示请求失败);
}
}
private currentDate=新日期();
公共路线(方向服务、运输方式、起点、终点){
如果(传输模式===‘驾驶’){
方向服务.路线({
来源:来源,,
目的地:目的地,
travelMode:transportMode,
驾驶选项:{
出发时间:新日期(this.currentDate.getFullYear()、this.currentDate.getMonth()、this.currentDate.getDate()+1,7,45),
流量模型:“悲观”
}
},这是常规反应);
我遇到的问题是routeResponse函数。我在调用changeDurationTraffic函数时收到一个错误
“未捕获类型错误:this.changeDurationTraffic不是一个函数”
是否我做错了什么?谢谢。由于此方法,该函数中的
此
未引用您的组件:
public route(directionsService, transportMode, origin, destination) {
if(transportMode === 'DRIVING') {
directionsService.route({
origin: origin,
destination: destination,
travelMode: transportMode,
drivingOptions: {
departureTime: new Date(this.currentDate.getFullYear(), this.currentDate.getMonth(), this.currentDate.getDate() + 1, 7, 45),
trafficModel: 'pessimistic'
}
}, this.routeResponse); //<-- ****Error here.****
建议阅读:有人有什么想法吗?哈哈。我试图通过在回调`drivingOptions:{departureTime:new Date(this.currentDate.getFullYear(),this.currentDate.getMonth(),this.currentDate.getDate()+1,7,45)中添加第二个函数来取得进展,trafficModel:'悲观'},this.routerResponse | | | this.otherFunction);`但是这个其他函数仍然无法访问我的changeDurationTraffic函数。答案解决了你的问题吗?@echonax是的,非常感谢。
this.routeResponse.bind(this);