Overview
Most of the time, we have the use of the scene: foreign workers outside the job, we need to know the current real-time location and personnel information, how to achieve it. This article will give you a simple explanation of the scenario we should implement. effect
Ideas
To solve the real-time location of the display, the core of the two points: 1, data transmission and storage, 2, the device side and the monitoring end of the data interaction.
First, let's start with the first question: the transmission and storage of data. Most of the time, the number of location information transmitted over the device needs to be stored, except in individual cases. Therefore, we can consider establishing a table in the database to record the real-time location data of the device.
--Create TABLE
location_track
(
ID serial not NULL,
lon double precision NOT NULL,
lat double pre Cision NOT NULL,
create_time time (Ten) with time zone not NULL,
geom geometry not NULL,
PRIMARY KEY (ID)
)
Second, data interaction issues. There are two ways to solve this problem: 1, the timing of the front-end refresh, 2, the back-end of the real-time push. If the timing of the front-end refresh, we can use setinterval (function, time) to deal with, if it is the back-end of the live push, which involves two aspects of thinking: A, the location of information transmitted over the storage of the trigger; B, websocket data real-time push. implementation In this demo, in order to achieve the effect, after the table is created, the first insert a batch of data in the table for testing.
INSERT into Location_track (lon, LAT, create_time, Geom) VALUES (116.379883, 39.865601, now (), St_point (116.379883, 39.865601));
INSERT into Location_track (lon, LAT, create_time, Geom) VALUES (116.379997, 39.865498, now (), St_point (116.379997, 39.865498));
INSERT into Location_track (lon, LAT, create_time, Geom) VALUES (116.380104, 39.865364, now (), St_point (116.380104, 39.865364));
Take out all the data at once and do a timed processing to simulate the real-time updating of the position (in practice, just get the latest record in the trigger).
Package com.lzugis.web.websocket;
Import Com.lzugis.services.TestService;
Import org.springframework.beans.factory.annotation.Autowired;
Import org.springframework.web.bind.annotation.RequestMapping;
Import Org.springframework.web.socket.CloseStatus;
Import Org.springframework.web.socket.TextMessage;
Import org.springframework.web.socket.WebSocketSession;
Import Org.springframework.web.socket.handler.TextWebSocketHandler;
Import java.util.List;
Import Java.util.Map;
Import Java.util.Timer;
Import Java.util.TimerTask; @RequestMapping ("/websocket") public class Locationtrackendpoint extends Textwebsockethandler {@Autowired privat
e Testservice testservice;
private timer timer;
private static int rownum = 1;
Private List Gpsdata; @Override protected void Handletextmessage (websocketsession session, TextMessage
Message) throws Exception {if (!session.isopen ()) {timer.cancel ();
Return } super.handletextmessage (session, message);
Session.sendmessage (message); } @Override public void afterconnectionestablished (Websocketsession session) throws Exception {timer = NE
W Timer (True);
Long delay = 0;
rownum = 0;
Ordertimetask ordertimetask = new Ordertimetask (session);
Gpsdata = Testservice.getgpsdata ();
Timer.schedule (Ordertimetask, delay, 1000);//Set the time specified, here is the 5s} class Ordertimetask extends timertask{
Private Websocketsession session;
Public Ordertimetask (websocketsession session) {this.session = session; } @Override public void Run () {try {String Lonlat = (Map) gpsdata.get (rownu
m). Get ("Lonlat"). ToString ();
TextMessage textmessage = new TextMessage (Lonlat);
Handlemessage (Session,textmessage);
rownum++; if (Rownum>gpsdata.size ()-1) {rownum = 0;
}} catch (Exception e) {e.printstacktrace (); }}} @Override public void afterconnectionclosed (websocketsession session, Closestatus status) throws Exception {System.out.println ("Connection Closed.
"); }
}
3, the front-end gets the location, and all the location information is saved to render the echo path.
<! DOCTYPE html>
Technical Blog
Csdn:http://blog.csdn.net/gisshixisheng
Blog Park: http://www.cnblogs.com/lzugis/
Online Tutorials
https://edu.csdn.net/course/detail/7471
Github
https://github.com/lzugis/
Contact Information
type |
content |
Qq |
1004740957 |
Public number |
Lzugis15 |
E-Mail |
Niujp08@qq.com |
Webgis Group |
452117357 |
Android Group |
337469080 |
Visualization Group of GIS data |
458292378 |