Я создаю приложение для механика / автомобилиста, использующее реагирование native и sockect.io. Я хочу, чтобы мой routeResponse возвращал json текущего местоположения. Как мне это сделать?
Вот мой код Backend в index.js с использованием express и socket.io
const app = express();
const server = require("http").createServer(app);
const io = require("socket.io").listen(server);
const port = 3000;
io.on("connection", socket => {
console.log("a user connected :D");
socket.on("repairRequest", routeResponse => {
console.log(routeResponse);
});
});
server.listen(port, () => console.log("server running on port:" + port));
Вот выдержка из файла Motorist.js. Я хочу получить JSON текущего местоположения
import React, { Component } from "react";
import {
TextInput,
StyleSheet,
Text,
View,
Keyboard,
TouchableHighlight,
TouchableOpacity
} from "react-native";
import MapView, { Polyline, Marker } from "react-native-maps";
import apiKey from "../google_api_key";
import _ from "lodash";
import PolyLine from "@mapbox/polyline";
import socketIO from 'socket.io-client';
export default class Motorist extends Component {
constructor(props) {
super(props);
this.state = {
error: "",
latitude: 0,
longitude: 0,
destination: "",
predictions: [],
pointCoords: []
};
// this.onChangeDestinationDebounced = _.debounce(
// this.onChangeDestination,
// 1000
// );
}
componentDidMount() {
//Get current location and set initial region to this
navigator.geolocation.getCurrentPosition(
position => {
this.setState({
latitude: position.coords.latitude,
longitude: position.coords.longitude
});
},
error => console.error(error),
{ enableHighAccuracy: true, maximumAge: 2000, timeout: 20000 }
);
}
async getRouteDirections(destinationPlaceId, destinationName) {
try {
const response = await fetch(
`https://maps.googleapis.com/maps/api/directions/json?origin=${
this.state.latitude
},${
this.state.longitude
}&destination=place_id:${destinationPlaceId}&key=${apiKey}`
);
const json = await response.json();
console.log(json);
const points = PolyLine.decode(json.routes[0].overview_polyline.points);
const pointCoords = points.map(point => {
return { latitude: point[0], longitude: point[1] };
});
this.setState({
pointCoords,
predictions: [],
destination: destinationName,
routeResponse: json
});