Posted on 3 Comments

Writing A Real Time Location Service – Views and Front End Logic

[callaction url=”https://www.youtube.com/user/JPlaya01″ background_color=”#333333″ text_color=”#ffffff” button_text=”Go Now” button_background_color=”#e64429″]Subscribe To My Youtube Page[/callaction]

Views

Thanks to the scaffolding done in the initial project setup, our login and registration views are already created, we just need to modify the home page and add a new page for device registration. Le’t create the activation view first, copy the home.blade.php file and name it activation.blade.php. This view will only have one form for taking in an activation code from an Android device replace the current contents with the following:


@extends('layouts.app')
@section('content')
<div class="container">
<div class="row">
<div class="col-md-8 col-md-offset-2">
<div class="panel panel-default">
<div class="panel-heading">Activate Device</div>
<div class="panel-body">
<form class="form-horizontal" role="form" method="POST" action="{{ url('/activate') }}">
{{ csrf_field() }}
<div class="form-group{{ $errors->has('name') ? ' has-error' : '' }}">
<label for="name" class="col-md-4 control-label">Code</label>
<div class="col-md-6">
<input id="name" type="text" class="form-control" name="code" value="{{ old('code') }}" required autofocus>
@if ($errors->has('name'))
<span class="help-block">
<strong>{{ $errors->first('activate') }}</strong>
</span>
@endif
</div>
</div>
<div class="form-group">
<div class="col-md-6 col-md-offset-4">
<button type="submit" class="btn btn-primary">
Activate
</button>
</div>
</div>
</form>.
</div>
</div>
</div>
</div>
</div>
@endsection

Don’t worry that the route hasn’t been created yet, we will get to that in a bit. In the mean time open up the home.blade.php file, this file is where the majority of the seen action is going to happen.  In this view the user will see a list of their devices with options to either start tracking GPS, stop tracking GPS, or delete the device. There is a Google map that will be used to show the position of the device and it will be updated in real time.


@extends('layouts.app')
@section('content')
<link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
<style>
#map {
height: 400px;
width: 100%;
}
</style>
<script src="https://cdn.socket.io/socket.io-1.4.5.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.js"></script>
<div class="container">
<div class="row">
<div class="col-md-8 col-md-offset-2">
<div class="panel panel-default">
<div class="panel-heading">Dashboard</div>
<div class="panel-body">
<table class="table">
<tbody>
@foreach(auth()->user()->devices as $device)
<tr>
<td>{{$device->uuid}}</td>
<td>
<button data-id="{{$device->id}}" class="btn btn-sm btn-default action-start-gps" id="start-gps"><i class="fa fa-map-marker" aria-hidden="true"></i></button>
<button data-id="{{$device->id}}" class="btn btn-sm btn-warning action-stop-gps" id="stop-gps"><i class="fa fa-map-marker" aria-hidden="true"></i></button>
<form method="post" action="/device/{{$device->id}}">
<input type="hidden" name="_method" value="DELETE">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<button data-id="{{$device->id}}" type="submit" class="btn btn-sm btn-danger" id="delete"><i class="fa fa-close" aria-hidden="true"></i></button>
</form>
</td>
</tr>
@endforeach
</tbody>
</table>
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-md-8 col-md-offset-2">
<div class="panel panel-default">
<div class="panel-heading">Map</div>
<div class="panel-body">
<div id="map"></div>
</div>
</div>
</div>
</div>
</div>
<script>
var startGps = document.getElementsByClassName("action-start-gps");
var stopGps = document.getElementsByClassName("action-stop-gps");
for (var i = 0; i < startGps.length; i++) {
startGps[i].addEventListener('click', function(){
$.get("http://gps.app/command/start-gps/" + $(this).data('id'), function(data, status){
}, false);
});
}
for (var i = 0; i < stopGps.length; i++) {
stopGps[i].addEventListener('click', function(){
$.get("http://gps.app/command/stop-gps/" + $(this).data('id'), function(data, status){
}, false);
});
}
function initMap(lat,long) {
var uluru = {lat: -25.363, lng: 131.044};
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 20,
center: uluru,
});
var marker = new google.maps.Marker({
position: uluru,
map: map
});
var socket = io.connect('http://gps.app:6001');
socket.on('gps', function (data) {
var center = {lat:Number(data.data.gps.lat),lng:Number(data.data.gps.long)};
@foreach(auth()->user()->devices as $device)
socket.on({{$device->id}}, function(data){
console.log(data);
});
@endforeach
marker.setPosition(center);
// using global variable:
map.panTo(center);
})
}
</script>
<script async defer
src="https://maps.googleapis.com/maps/api/js?key=AIzaSyBCDNt1biVyfA8h-eCZyZ69CKS6NNBCeEQ&callback=initMap">
</script>
@endsection

Home screen
Your screen should look like this

This is where the majority of the magic happens in the home view there are two divs. In the first div all of the associate devices will be shown in a tabular format with options to start GPS tracking, stop GPS tracking, and deleting the associated device. In the second div there is a Google map that will be used to visualize our devices (in order to use Google map services you must register for a key). The javascript code calls the routes needed to start and stop GPS tracking, as well as the Socket.IO code needed to update the Google map in real time ( for those who don’t know Socket.IO please refer here). Let’s take a closer look at the Socket.IO code and understand what is going on. When new data comes through the websocket channel, the marker’s latitude and longitude are updated and the map is recentered all without a page reload. With this bagged up the next step is to set up the routes and controller logic!

3 thoughts on “Writing A Real Time Location Service – Views and Front End Logic

  1. Hello mastashake08 , Thanks for your work...
    where is the next steps ? 😀

    1. I am on vacation will be up next week promise!

  2. […] you are following along from the previous tutorial then you should have the following page implemented on the front […]

Leave a Reply