【发布时间】:2018-08-25 01:54:06
【问题描述】:
我正在尝试在我的列表服务中使用地理定位服务中的纬度和经度。不幸的是,这一直返回为未定义。不太确定可能是什么问题。
list.service.ts
import { Injectable } from '@angular/core';
import { Http, Headers } from '@angular/http';
import { Observable, Subject, asapScheduler, pipe, of, from, interval, merge, fromEvent, SubscriptionLike, PartialObserver } from 'rxjs';
import { List } from '../models/list.model';
import { map } from 'rxjs/operators';
import { GeolocationService } from '../services/geolocation.service';
@Injectable()
export class ListService {
constructor(private http: Http, private geoServ: GeolocationService) { }
getLongitude() {
this.geoServ.getLongitude().subscribe((longitude) => {
console.log(longitude)
});
}
private serverApi = 'http://localhost:3000';
public getAllLists(): Observable<List[]> {
const URI = `${this.serverApi}/yelp/${longitude}/${latitude}`;
return this.http.get(URI)
.pipe(map(res => res.json()))
.pipe(map(res => <List[]>res.businesses));
}
}
地理位置.service.ts
import { Injectable } from '@angular/core';
@Injectable({
providedIn: 'root'
})
export class GeolocationService {
constructor() { }
getGeoLocation() {
console.log('Geolocation working!');
const options = {
enableHighAccuracy: true,
timeout: 5000,
maximumAge: 0
};
const success = (pos) => {
const crd = pos.coords;
console.log(`Latitude : ${crd.latitude}`);
console.log(`Longitude : ${crd.longitude}`);
};
const error = (err) => {
console.warn(`ERROR(${err.code}): ${err.message}`);
};
navigator.geolocation.getCurrentPosition(success, error, options);
}
getLongitude() {
console.log('Geolocation working!');
const options = {
enableHighAccuracy: true,
timeout: 5000,
maximumAge: 0
};
const success = (pos) => {
const crd = pos.coords;
console.log(`Longitude : ${crd.longitude}`);
const longitude = crd.longitude;
return longitude;
};
const error = (err) => {
console.warn(`ERROR(${err.code}): ${err.message}`);
};
navigator.geolocation.getCurrentPosition(success, error, options);
}
}
感谢您抽出宝贵时间查看此内容。我感谢对下面我的附加问题的进一步帮助。 text text text text 直到我添加更多详细信息 text text text text 才会让我发布。
【问题讨论】:
-
我认为您在 GeolocationService 中的语法是错误的。您需要订阅 getCurrentPositions' Observable ,不是吗? “pos”还应该从哪里来?
-
我不确定是不是这个问题?我能够执行:console.log(
Latitude : ${crd.latitude});控制台.log(Longitude : ${crd.longitude});并看到登录控制台
标签: javascript angular typescript service