如何从 json 创建服务 return 的经度和纬度值?

How can I create service return values of longitude and latitude from json?

如何从 JSON?

创建服务 return 经度和纬度值

我有以下 URL:

http://192.168.7.xxx:9200/location/_doc/27737

下面这个URLJSON:

    {"_index":"location","_type":"_doc","_id":"27737","_version":1,"_seq_no":5577,"_primary_term":1,"found":true,"_source":{"Locid":27737,"GPS1":"25.0173, 121.462","GPS2":"25°01'02.2\"N 121°27'44.8\"E","CompanyID":1005070,"ads":"142 Sec. 1, HsIn Nan Rd cung Ko Dšst, New Taipei City, Taiwan","Crid":75,"con":"Taiwan","Ctid":5894,"Cn":"Zhonghe District","pushdate":"2019-12-26T03:38:20.883"}}  

我需要创建服务 return 来自 GPS1 的两个值:

longitude: 25.0173,
latitude: 121.462

基于位置 Id 参数 27737

因此我需要创建服务获取位置 id 参数和 return GPS1 25.0173 和 25.0173 的两个值

getDataService(locationid): Observable<any> {    
     const url = "http://192.168.7.xxx:9200/location/_doc/27737";    
       return this.httpClient.get(`${url} + locationid`);    
   }  

ngOinInit 事件

ngOnInit  
{  
call service here  
}  

假设您在名为 response 的变量中返回了 json。你可以简单地做

getDataService(locationid): Observable<any> {    
     const url = "http://192.168.7.xxx:9200/location/_doc/27737";    
       return this.httpClient.get(`${url} + locationid`)
          .then(response => {
             const [latitude, longitude] = response._source.GPS1.trim().split(",");
             return {latitude, longitude};
           });
   }  

以下代码将 return 包含给定 locationId 的纬度和经度的可观察对象。 locationId 需要在 url 中传递,如下所示。

import { map } from 'rxjs/operators'; 
getDataService(locationId): Observable<any> {    
     const url = 'http://192.168.7.xxx:9200/location/_doc/' + locationId;    
     return this.httpClient.get(url).pipe(
       map(response => {
         const [longitude, latitude] = response._source.GPS1.split(",");
         return {latitude: latitude.trim(), longitude: longitude.trim()};
       })
     );
}  

然后调用ngOnInit中的服务,获取位置的经纬度。

latitude: number;
longitude: number;
constructor(private service: DataService) {}

ngOnInit() {
  const locationId = '27737';
  this.service.getDataService(locationId).subscribe((location) => {
    this.latitude = Number(location.latitude);
    this.longitude = Number(location.longitude);
  })
}