将位置详细信息作为JSON字符串发送到ionic3-angular4中的服务器[已编辑]
我是爱奥尼亚框架的新手。目前与ionic3-angular4结合使用。在我的应用程序中,我需要将用户的位置详细信息发送到服务器,服务器请求一个包含位置详细信息的JSON字符串。使用我在网上找到的很少几个示例,我使用BackgroundGeolocation对下面给出的代码进行了编码。问题是我获得了预期的位置详细信息,但我无法访问服务器。我已经按照服务器的预期创建了一个JSON字符串并发送了它,尽管我无法访问服务器。我也找不到任何以JSON字符串形式在线发送位置详细信息的教程。有人能帮我吗。以下是我的位置跟踪器代码:将位置详细信息作为JSON字符串发送到ionic3-angular4中的服务器[已编辑],angular,object,location,tracking,ionic3,Angular,Object,Location,Tracking,Ionic3,我是爱奥尼亚框架的新手。目前与ionic3-angular4结合使用。在我的应用程序中,我需要将用户的位置详细信息发送到服务器,服务器请求一个包含位置详细信息的JSON字符串。使用我在网上找到的很少几个示例,我使用BackgroundGeolocation对下面给出的代码进行了编码。问题是我获得了预期的位置详细信息,但我无法访问服务器。我已经按照服务器的预期创建了一个JSON字符串并发送了它,尽管我无法访问服务器。我也找不到任何以JSON字符串形式在线发送位置详细信息的教程。有人能帮我吗。以下是
import { Injectable, NgZone } from '@angular/core';
import { BackgroundGeolocation, BackgroundGeolocationConfig } from'@ionic-native/background-geolocation';
import { Geolocation, Geoposition } from '@ionic-native/geolocation';
import { Toast } from '@ionic-native/toast';
import { ToastController } from 'ionic-angular';
import { Http } from '@angular/http';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/filter';
@Injectable()
export class LocationTracker {
public watch: any;
public lat: number = 0;
public lng: number = 0;
public timing: any;
public locationJson: string;
constructor(public zone: NgZone, private backgroundGeolocation: BackgroundGeolocation, private geolocation: Geolocation, private toastCtrl: ToastController) {}
startTracking() {
// Background Tracking
let config = {
desiredAccuracy: 0,
locationProvider: 1,
//For testing purpose, I've given 1 metre although the target is 100 metres
distanceFilter: 1,
stationaryRadius: 1,
debug: true,
interval: 30000, //30 secs
fastInterval: 15000, //15 secs
activitiesInterval: (15000),
url: 'http://192.168.100.14:8084/CAPWS' + '/ULD/' + this.locationJson,
syncThreshold: 100,
stopOnTerminate: false,
startForeground: true,
startOnBoot: true,
stopOnStillActivity: false,
pauseLocationUpdates: false
};
this.backgroundGeolocation.configure(config).subscribe((location) => {
console.log('BackgroundGeolocation: ' + location.latitude + ',' + location.longitude);
// Run update inside of Angular's zone
this.zone.run(() => {
this.lat = location.latitude;
this.lng = location.longitude;
this.timing = location.timestamp;
console.log('backgroundGeolocation--this.lat: ', this.lat);
console.log('backgroundGeolocation--this.lng: ', this.lng);
console.log('backgroundGeolocation--this.timing: ', this.timing);
this.locationJson = JSON.stringify({
userId: 'f3fceda259df80200159fd48c0ea14ab',
batchId: 'f3fceda25c211882015c67f59e410acc',
timing: new Date(location.timestamp),
latitude: location.latitude,
longitude: location.longitude
})
});
}, (err) => {
console.log(err);
});
// Turn ON the background-geolocation system.
this.backgroundGeolocation.start();
// Foreground Tracking
let options = {
frequency: 3000,
//interval: 20000,
enableHighAccuracy: true,
url: 'http://192.168.100.14:8084/CAPWS' + '/ULD/' + this.locationJson,
stopOnTerminate: false // enable this to clear background location settings when the app terminates
};
this.watch = this.geolocation.watchPosition(options).filter((p: any) => p.code === undefined).subscribe((position: Geoposition) => {
console.log(position);
// Run update inside of Angular's zone
this.zone.run(() => {
this.lat = position.coords.latitude;
this.lng = position.coords.longitude;
this.timing = position.timestamp;
console.log('watch--this.lat: ', this.lat);
console.log('watch--this.lng: ', this.lng);
console.log('watch--this.timing: ', this.timing);
this.locationJson = JSON.stringify({
userId: 'f3fceda259df80200159fd48c0ea14ab',
batchId: 'f3fceda25c211882015c67f59e410acc',
timing: new Date(position.timestamp),
latitude: position.coords.latitude,
longitude: position.coords.longitude
})
console.log('locationJson===: ' + this.locationJson);
});
});
}
stopTracking() {
console.log('stopTracking');
this.backgroundGeolocation.finish();
this.watch.unsubscribe();
}
}
根据文件上的要求,这将是一个职位。
在我看来,您试图将其格式化为GET,因为您正在将this.locationJson
添加到URL查询字符串中
您提供的代码在发送POST请求时运行良好。根据要将其发送到的服务器类型,您可能还需要设置httpHeaders
选项
然后,您应该将一个JsonArray发布到您的服务器上,它看起来类似于:
[
{
“提供者”:“融合”,
“时间”:1504290833000,
“纬度”:0.0000000,
“经度”:0.0000000,
“准确性”:20,
“速度”:0,
“高度”:0,
“locationProvider”:1
}
]
服务器端很好。当使用邮递员检查时,它会被击中!