I am new in Ionic and React. I am trying to build component what is tracking GPS coordinates of device and send them via AJAX to the backend API. I was able to develop the tracking including sending data to API but I dont know how to initiate and display / store device UID.
I did follow a guide here https://ionicframework.com/docs/native/unique-device-id but there is no info how to initiate plugin in React, always only in Angular.
Here is the code:
import React from "react";
import { stopwatchOutline, play, stop } from "ionicons/icons";
import {
IonHeader,
IonPage,
IonTitle,
IonToolbar,
IonContent,
IonIcon,
IonGrid,
IonRow,
IonCol,
IonButton,
} from "@ionic/react";
import { UniqueDeviceID } from '@ionic-native/unique-device-id/ngx';
type State = {
intervalID: number;
latitude: number;
longitude: number;
trackingEnabled: boolean;
counter: number;
device_id: string;
};
class GPS extends React.Component<any, State> {
constructor(props: any) {
super(props);
this.state = {
intervalID: 0,
latitude: 0,
longitude: 0,
trackingEnabled: false,
counter: 0,
device_id: '',
};
}
startTracking = () => {
this.setState({ trackingEnabled: true });
const intervalID = window.setInterval(() => {
navigator.geolocation.getCurrentPosition((position) => {
let latitude = position.coords.latitude;
let longitude = position.coords.longitude;
if (latitude > 0 && longitude > 0) {
if (
latitude !== this.state.latitude &&
longitude !== this.state.longitude
) {
fetch("http://xyz", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
device_id: 123456,
lat: latitude,
lng: longitude,
}),
})
.then(async (response) => {
const data = await response.json();
if (!response.ok) {
const error = (data && data.message) || response.status;
return Promise.reject(error);
}
console.log(data);
})
.catch((error) => {
console.error("There was an error!", error);
});
} else {
console.log(
"lat: " + latitude + ", lng: " + longitude + " are without change"
);
}
}
this.setState({ latitude: latitude, longitude: longitude, counter: this.state.counter+1 });
console.log("lat: " + latitude + ", lng: " + longitude, " counter: " + this.state.counter);
if(this.state.counter > 10) {
this.setState({ trackingEnabled: false });
window.clearInterval(this.state.intervalID);
console.log("stop after thousands call");
}
});
}, 2000);
this.setState({ intervalID: intervalID });
console.log("start");
};
stopTracking = () => {
this.setState({ trackingEnabled: false });
window.clearInterval(this.state.intervalID);
console.log("stop");
};
render() {
return (
<IonPage>
<IonHeader>
<IonToolbar color="primary">
<IonTitle>
<IonIcon icon={stopwatchOutline} /> GPS tracker
</IonTitle>
</IonToolbar>
</IonHeader>
<IonContent>
<IonGrid>
<IonRow>
<IonCol>
<h3>lat: {this.state.latitude}</h3>
<h3>lng: {this.state.longitude}</h3>
<h3>Device ID: {this.state.device_id}</h3>
</IonCol>
</IonRow>
<IonRow>
<IonCol>
<IonButton
id="start_tracking"
color="primary"
expand="block"
strong={true}
size="large"
disabled={this.state.trackingEnabled}
onClick={this.startTracking}
>
<IonIcon icon={play} /> Start
</IonButton>
</IonCol>
<IonCol>
<IonButton
id="stop_tracking"
color="light"
expand="block"
size="large"
disabled={!this.state.trackingEnabled}
onClick={this.stopTracking}
>
<IonIcon icon={stop} /> Stop
</IonButton>
</IonCol>
</IonRow>
</IonGrid>
</IonContent>
</IonPage>
);
}
}
export default GPS;
In documentation is written this (for Angular):
import { UniqueDeviceID } from '@ionic-native/unique-device-id/ngx';
constructor(private uniqueDeviceID: UniqueDeviceID) { }
...
this.uniqueDeviceID.get()
.then((uuid: any) => console.log(uuid))
.catch((error: any) => console.log(error));
But I have no idea how to do it in React in my class. I spent 2 days with Googling and trying everything what I found but still no solution.
Thank you.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…