Я новичок в Ionic. Я пытаюсь сохранить свое текущее местоположение (т.е. широту и долготу) в базе данных php. И я не могу этого сделать. Это проект в реальном времени.
Это мой php скрипт
locationsave.php
<?php
if (isset($_SERVER['HTTP_ORIGIN'])) {
header("Access-Control-Allow-Origin: {$_SERVER['HTTP_ORIGIN']}");
header('Access-Control-Allow-Credentials: true');
header('Access-Control-Max-Age: 86400'); // cache for 1 day
}
if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_METHOD']))
header("Access-Control-Allow-Methods: GET, POST, OPTIONS");
if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']))
header("Access-Control-Allow-Headers: {$_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']}");
exit(0);
}
$mysql_host = "localhost";
$mysql_database = "locationtracker";
$mysql_user = "root";
$mysql_password = "";
// Create connection
$conn = new mysqli($mysql_host, $mysql_user, $mysql_password,$mysql_database);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$postdata = file_get_contents("php://input");
if ($postdata != null) {
$request = json_decode($postdata);
$lat = $request->latitude;
$lng = $request->longitude;
$sql = "INSERT INTO user_info (uLatitude,uLongitude) VALUES ('$lat','$lng')";
$result = $conn->query($sql);
$conn->close();
}else{
echo ("Data received is null!");
}
?>
А это мой файл home.ts!
import { Component, ViewChild, ElementRef } from '@angular/core';
import { NavController } from 'ionic-angular';
import { Http, Headers,RequestOptions } from '@angular/http';
import { Geolocation } from '@ionic-native/geolocation';
import 'rxjs/add/operator/map';
declare var google;
@Component({
selector: 'home-page',
templateUrl: 'home.html'
})
export class HomePage {
@ViewChild('map') mapElement: ElementRef;
map: any;
@ViewChild("latitude") latitude;
@ViewChild("longitude") longitude;
constructor(public navCtrl: NavController, public geolocation: Geolocation, public http:Http) {
this.loadMap();
}
loadMap(){
this.geolocation.getCurrentPosition().then((position) => {
let latLng = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
//passsing values to db
let latitude = position.coords.latitude;
let longitude = position.coords.longitude;
//to pass in db method
this.saveLocationCoords(latitude,longitude);
let mapOptions = {
center: latLng,
zoom: 15,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
this.map = new google.maps.Map(this.mapElement.nativeElement, mapOptions);
let marker = new google.maps.Marker({
map: this.map,
animation: google.maps.Animation.DROP,
position: this.map.getCenter()
});
let content = "<h4>Information!</h4>";
this.addInfoWindow(marker, content);
}, (err) => {
console.log(err);
});
}
addInfoWindow(marker, content){
let infoWindow = new google.maps.InfoWindow({
content: content
});
google.maps.event.addListener(marker, 'click', () => {
infoWindow.open(this.map, marker);
});
}
//db method
saveLocationCoords(latitude,longitude){
let headers = new Headers();
headers.append('Content-Type', 'application/json');
console.log("In the territory")
let coordsData = JSON.stringify({ latitude: latitude, longitude: longitude });
this.http.post('http://localhost:82/authapp/locationsave.php',
coordsData, {headers: headers})
.map(res => res.json())
.subscribe(data => {
console.log(data);
});
}
}
А как я могу обновлять текущее местоположение через каждую 1 минуту?