-
Notifications
You must be signed in to change notification settings - Fork 0
/
direction.php
135 lines (101 loc) · 3.49 KB
/
direction.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
<?php require_once("header.php"); ?>
<style>
/* Always set the map height explicitly to define the size of the div
* element that contains the map. */
.head{
padding: 5px;
}
#map{
z-index: 0.5;
margin-top: 100px;
width:100%;
height: 600px;;
}
</style>
<body>
<div id="layoutSidenav_content">
<main>
<div class="head">
<div style="display:flex; ">
<button id="getdir" class="btn btn-primary">Get Direction</button> <span><?php echo $_GET['address']?></span> <br>
<br> <p><b>Distance:</b><span id="dis"></span></p><br>
<p><b>Time:</b><span id="time"></span></p>
</div>
<div id="map"></div>
</div>
</main>
</div>
</body>
<script>
let lat = <?php echo $_GET["lat"]; ?>;
let lon = <?php echo $_GET["long"]; ?>;
let mylat=0;
let mylon=0;
var lattlong;
let directionsService;
let directionsDisplay;
$(document).ready(function() {
getmyloc();
lattlong= new google.maps.LatLng(lat, lon);
})
function getmyloc(){
var optn = {
enableHighAccuracy: true,
timeout: Infinity,
maximumAge: 0
};
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition((position)=>{
mylat=position.coords.latitude;
mylon=position.coords.longitude;
console.log(mylat);
});
} else {
x.innerHTML = "Geolocation is not supported by this browser.";
}
console.log("sd");
}
function initMap() {
directionsService = new google.maps.DirectionsService;
directionsDisplay = new google.maps.DirectionsRenderer;
directionsDisplay.setDirections()
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 20,
center: {
lat: lat,
lng: lon
},
mapTypeId: google.maps.MapTypeId.ROADMAP,
navigationControlOptions: {style:google.maps.NavigationControlStyle.SMALL}
});
directionsDisplay.setMap(map);
function onChangeHandler() {
calculateAndDisplayRoute(directionsService, directionsDisplay);
};
document.getElementById('getdir').addEventListener('click', onChangeHandler);
}
function showPosition(position) {
}
function calculateAndDisplayRoute(directionsService, directionsDisplay) {
var srclattlong = new google.maps.LatLng(mylat, mylon);
directionsService.route({
origin: srclattlong,
destination: lattlong,
travelMode: 'DRIVING'
}, function(response, status) {
if (status === 'OK') {
$('#dis').html(response.routes[0].legs[0].distance['text']);
$('#time').html(response.routes[0].legs[0].duration['text']);
console.log(response.routes[0].legs[0].distance['text']);
directionsDisplay.setDirections(response);
} else {
window.alert('Please Wait While Fetching Your Location' + status);
}
});
}
// setInterval(getmyloc,1000);
// setInterval(calculateAndDisplayRoute,2000);
</script>
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCdk0GkRdoCCpgU-T_rBFoU_CFPWB5KnBM&callback=initMap">
</script>
</html>