我是一个新手,以离子框架。目前在ionic3-angular4组合工作。在我的应用程序,我需要发送位置信息的用户(每100米,一旦用户移动)的到请求JSON字符串与它的位置信息服务器。用我在网上找到的例子非常少,我用BackgroundGeolocation编码下面给出的代码。问题是我按预期得到位置细节,但我无法击中服务器。我已经按照服务器的预期创建了JSON字符串并发送它,即使我无法访问服务器。我找不到任何教程在线发送位置详细信息作为JSON字符串。有人能帮我解决这个问题吗?下面是我的位置跟踪代码:发送位置信息作为JSON字符串服务器ionic3-angular4 [编辑]
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();
}
}
服务器端是罚款。当使用'postman'检查时它会被击中! – svs