我正在学习角度课程,课程中使用的角度版本是角度2,所以我的代码存在一些问题..
我应该根据纬度和经度获取我的位置,并使用谷歌API,它将使用反向地理编码从here返回位置名称
然而,我出于某些原因无法使用我的代码获取任何值..它应该返回一个位置数组,但控制台中的结果是: {results:Array(0),status:“ZERO_RESULTS”}
文件.service.ts中的代码
foreach($attendace as $k => $v) {
$id = $v['project_id'];
$result[$id]['project_name'] = $v['project_name'];
$result[$id]['labour'] += $v['no_of_labours'];
$result[$id]['total_time'] += ($v['stop_time']-$v['start_time'])*$v['no_of_labours'];
}
完整的服务文件:
foreach($attendace as $k => $v) {
$id = $v['project_id'];
$result[$id]['labour'] =0;
$result[$id]['total_time']=0;
$result[$id]['project_name'] = $v['project_name'];
$result[$id]['labour'] += $v['no_of_labours'];
$result[$id]['total_time'] += ($v['stop_time']-$v['start_time'])*$v['no_of_labours'];
}
}
这是文件.component.ts中的代码
getLocationName(lat:number, long:number):Observable<any>{
const url = GOOGLE_ROOT;
const queryParams = "?latlng=" + lat + "," + long + "&key="+ GOOGLE_KEY;
return this.http.get(url+queryParams)
.pipe(map(loc =>loc.json()))
}
组件的完整文件:
import {Injectable} from '@angular/core';
import {Jsonp, Http} from '@angular/http';
import { Observable} from 'rxjs';
import { map, catchError } from 'rxjs/operators';
import { HttpClient, HttpResponse } from '@angular/common/http';
import {FORCAST_KEY, FORCAST_ROOT, GOOGLE_KEY, GOOGLE_ROOT} from '../constants/constants';
@Injectable()
export class WeatherService{
constructor(private jsonp:Jsonp, private http: Http){}
getCurrentLocation() : Observable<any>{
if(navigator.geolocation){
return Observable.create(observer => {
navigator.geolocation.getCurrentPosition(pos=>{
observer.next(pos);
}
),
err => {
return Observable.throw(err);}
});
}else{
return Observable.throw("Geolaction is not available");
}
}
getCurrentWeather(lat:number, long:number): Observable<any>{
const url= FORCAST_ROOT+FORCAST_KEY+"/"+lat+","+long;
const queryParams = "?callback=JSONP_CALLBACK";
return this.jsonp.get(url+queryParams)
.pipe(map(data=>data.json()))
}
getLocationName(lat:number, long:number):Observable<any>{
const url = GOOGLE_ROOT;
const queryParams = "?latlng=" + lat + "," + long + "&key="+ GOOGLE_KEY;
return this.http.get(url+queryParams)
.pipe(map(loc =>loc.json()))
}
}
我仍然很有棱角。我希望有一个人可以帮助我。 该项目已上传到github here以查看完整代码