发送位置信息至服务器,发送位置信息作为JSON字符串服务器ionic3-angular4 [编辑]...

我是一个新手,以离子框架。目前在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();

}

}

2017-08-31

svs

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值