-
-
Save lokeshjain2008/70ec7546f6c7bd90deb906850d635e9b to your computer and use it in GitHub Desktop.
Calculate if locations is on the routes using google mapz// source https://jsbin.com/hivuyu
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta name="description" content="isLocationOnEdge"> | |
<meta charset="utf-8"> | |
<meta name="viewport" content="width=device-width"> | |
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyDfKLDQPQgpVU7wokCKnUgJVEB8vcwmd3g&libraries=geometry"> | |
</script> | |
<title>JS Bin</title> | |
<style id="jsbin-css"> | |
#map{ | |
width: 80%; | |
height: 300px; | |
} | |
</style> | |
</head> | |
<body> | |
<h2> Hello man this is good</h2> | |
<div id="map"></div> | |
<script id="jsbin-javascript"> | |
console.clear(); | |
var directionsService = new google.maps.DirectionsService(); | |
var polyline = null; | |
var Locations = { | |
"driver": [ | |
{ | |
"location": { | |
"start": { | |
"formatted_address": "Oberoi Mall General AK Vidya Marg, Yashodham, Goregaon, Mumbai, Maharashtra 400063, India", | |
"lat": 19.173808, | |
"lng": 72.8605943 | |
}, | |
"end": { | |
"formatted_address": "Pune, Maharashtra, India", | |
"lat": 18.5204303, | |
"lng": 73.85674369999992 | |
} | |
} | |
} | |
], | |
"passenger": [ | |
{ | |
"location": { | |
"start": { | |
"formatted_address": "Chhatrapati Shivaji International Airport Area, Vile Parle, Mumbai, Maharashtra 400099, India", | |
"lat": 19.0895595, | |
"lng": 72.86561440000003 | |
}, | |
"end": { | |
"formatted_address": "Pune, Maharashtra, India", | |
"lat": 18.5204303, | |
"lng": 73.85674369999992 | |
} | |
} | |
} | |
] | |
}; | |
function initialize() { | |
var directionsDisplay = new google.maps.DirectionsRenderer(); | |
var kandivali = new google.maps.LatLng(19.2048, 72.8368); | |
var lonavala = new google.maps.LatLng(18.7481, 73.4072); | |
var khandala = new google.maps.LatLng(18.7548, 73.3778); | |
var lavasa = new google.maps.LatLng(18.4077, 73.5075); | |
var blore = new google.maps.LatLng(12.9716, 77.5946); | |
var mapOptions = { | |
zoom: 10, | |
mapTypeId: 'roadmap' | |
}; | |
var map = new google.maps.Map(document.getElementById('map'), | |
mapOptions); | |
directionsDisplay.setMap(map); | |
var request = { | |
origin: new google.maps.LatLng(Locations.driver[0].location.start), | |
destination: new google.maps.LatLng( Locations.driver[0].location.end), | |
// Note that Javascript allows us to access the constant | |
// using square brackets and a string value as its | |
// "property." | |
travelMode: google.maps.TravelMode['DRIVING'] | |
}; | |
directionsService.route(request, function(response, status) { | |
if (status == 'OK') { | |
directionsDisplay.setDirections(response); | |
polyline = google.maps.geometry.encoding.decodePath(response.routes[0].overview_polyline); | |
polyline = new google.maps.Polyline({path: polyline}); | |
console.log(google.maps.geometry.poly.isLocationOnEdge(lonavala, polyline, 0.01)); | |
if (google.maps.geometry.poly.isLocationOnEdge(lonavala, polyline, 0.05)) { | |
alert("lonavala is on way!"); | |
} | |
if (google.maps.geometry.poly.isLocationOnEdge(kandivali, polyline, 0.05)) { | |
alert("kandivali is on way "); | |
} | |
if (google.maps.geometry.poly.isLocationOnEdge(khandala, polyline, 0.05)) { | |
alert("khandala is on way "); | |
} | |
if (google.maps.geometry.poly.isLocationOnEdge(lavasa, polyline, 0.05)) { | |
alert("lavasa is on way "); | |
} else { | |
alert("lavasa is not on way "); | |
} | |
if (google.maps.geometry.poly.isLocationOnEdge(blore, polyline, 0.1)) { | |
alert("blore is on way!"); | |
} else { | |
alert("blore is not on way "); | |
} | |
} | |
}); | |
} | |
google.maps.event.addDomListener(window, 'load', initialize); | |
</script> | |
<script id="jsbin-source-css" type="text/css">#map{ | |
width: 80%; | |
height: 300px; | |
}</script> | |
<script id="jsbin-source-javascript" type="text/javascript"> | |
console.clear(); | |
var directionsService = new google.maps.DirectionsService(); | |
var polyline = null; | |
var Locations = { | |
"driver": [ | |
{ | |
"location": { | |
"start": { | |
"formatted_address": "Oberoi Mall General AK Vidya Marg, Yashodham, Goregaon, Mumbai, Maharashtra 400063, India", | |
"lat": 19.173808, | |
"lng": 72.8605943 | |
}, | |
"end": { | |
"formatted_address": "Pune, Maharashtra, India", | |
"lat": 18.5204303, | |
"lng": 73.85674369999992 | |
} | |
} | |
} | |
], | |
"passenger": [ | |
{ | |
"location": { | |
"start": { | |
"formatted_address": "Chhatrapati Shivaji International Airport Area, Vile Parle, Mumbai, Maharashtra 400099, India", | |
"lat": 19.0895595, | |
"lng": 72.86561440000003 | |
}, | |
"end": { | |
"formatted_address": "Pune, Maharashtra, India", | |
"lat": 18.5204303, | |
"lng": 73.85674369999992 | |
} | |
} | |
} | |
] | |
}; | |
function initialize() { | |
var directionsDisplay = new google.maps.DirectionsRenderer(); | |
var kandivali = new google.maps.LatLng(19.2048, 72.8368); | |
var lonavala = new google.maps.LatLng(18.7481, 73.4072); | |
var khandala = new google.maps.LatLng(18.7548, 73.3778); | |
var lavasa = new google.maps.LatLng(18.4077, 73.5075); | |
var blore = new google.maps.LatLng(12.9716, 77.5946); | |
var mapOptions = { | |
zoom: 10, | |
mapTypeId: 'roadmap' | |
}; | |
var map = new google.maps.Map(document.getElementById('map'), | |
mapOptions); | |
directionsDisplay.setMap(map); | |
var request = { | |
origin: new google.maps.LatLng(Locations.driver[0].location.start), | |
destination: new google.maps.LatLng( Locations.driver[0].location.end), | |
// Note that Javascript allows us to access the constant | |
// using square brackets and a string value as its | |
// "property." | |
travelMode: google.maps.TravelMode['DRIVING'] | |
}; | |
directionsService.route(request, function(response, status) { | |
if (status == 'OK') { | |
directionsDisplay.setDirections(response); | |
polyline = google.maps.geometry.encoding.decodePath(response.routes[0].overview_polyline); | |
polyline = new google.maps.Polyline({path: polyline}); | |
console.log(google.maps.geometry.poly.isLocationOnEdge(lonavala, polyline, 0.01)); | |
if (google.maps.geometry.poly.isLocationOnEdge(lonavala, polyline, 0.05)) { | |
alert("lonavala is on way!"); | |
} | |
if (google.maps.geometry.poly.isLocationOnEdge(kandivali, polyline, 0.05)) { | |
alert("kandivali is on way "); | |
} | |
if (google.maps.geometry.poly.isLocationOnEdge(khandala, polyline, 0.05)) { | |
alert("khandala is on way "); | |
} | |
if (google.maps.geometry.poly.isLocationOnEdge(lavasa, polyline, 0.05)) { | |
alert("lavasa is on way "); | |
} else { | |
alert("lavasa is not on way "); | |
} | |
if (google.maps.geometry.poly.isLocationOnEdge(blore, polyline, 0.1)) { | |
alert("blore is on way!"); | |
} else { | |
alert("blore is not on way "); | |
} | |
} | |
}); | |
} | |
google.maps.event.addDomListener(window, 'load', initialize); | |
</script></body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#map{ | |
width: 80%; | |
height: 300px; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
console.clear(); | |
var directionsService = new google.maps.DirectionsService(); | |
var polyline = null; | |
var Locations = { | |
"driver": [ | |
{ | |
"location": { | |
"start": { | |
"formatted_address": "Oberoi Mall General AK Vidya Marg, Yashodham, Goregaon, Mumbai, Maharashtra 400063, India", | |
"lat": 19.173808, | |
"lng": 72.8605943 | |
}, | |
"end": { | |
"formatted_address": "Pune, Maharashtra, India", | |
"lat": 18.5204303, | |
"lng": 73.85674369999992 | |
} | |
} | |
} | |
], | |
"passenger": [ | |
{ | |
"location": { | |
"start": { | |
"formatted_address": "Chhatrapati Shivaji International Airport Area, Vile Parle, Mumbai, Maharashtra 400099, India", | |
"lat": 19.0895595, | |
"lng": 72.86561440000003 | |
}, | |
"end": { | |
"formatted_address": "Pune, Maharashtra, India", | |
"lat": 18.5204303, | |
"lng": 73.85674369999992 | |
} | |
} | |
} | |
] | |
}; | |
function initialize() { | |
var directionsDisplay = new google.maps.DirectionsRenderer(); | |
var kandivali = new google.maps.LatLng(19.2048, 72.8368); | |
var lonavala = new google.maps.LatLng(18.7481, 73.4072); | |
var khandala = new google.maps.LatLng(18.7548, 73.3778); | |
var lavasa = new google.maps.LatLng(18.4077, 73.5075); | |
var blore = new google.maps.LatLng(12.9716, 77.5946); | |
var mapOptions = { | |
zoom: 10, | |
mapTypeId: 'roadmap' | |
}; | |
var map = new google.maps.Map(document.getElementById('map'), | |
mapOptions); | |
directionsDisplay.setMap(map); | |
var request = { | |
origin: new google.maps.LatLng(Locations.driver[0].location.start), | |
destination: new google.maps.LatLng( Locations.driver[0].location.end), | |
// Note that Javascript allows us to access the constant | |
// using square brackets and a string value as its | |
// "property." | |
travelMode: google.maps.TravelMode['DRIVING'] | |
}; | |
directionsService.route(request, function(response, status) { | |
if (status == 'OK') { | |
directionsDisplay.setDirections(response); | |
polyline = google.maps.geometry.encoding.decodePath(response.routes[0].overview_polyline); | |
polyline = new google.maps.Polyline({path: polyline}); | |
console.log(google.maps.geometry.poly.isLocationOnEdge(lonavala, polyline, 0.01)); | |
if (google.maps.geometry.poly.isLocationOnEdge(lonavala, polyline, 0.05)) { | |
alert("lonavala is on way!"); | |
} | |
if (google.maps.geometry.poly.isLocationOnEdge(kandivali, polyline, 0.05)) { | |
alert("kandivali is on way "); | |
} | |
if (google.maps.geometry.poly.isLocationOnEdge(khandala, polyline, 0.05)) { | |
alert("khandala is on way "); | |
} | |
if (google.maps.geometry.poly.isLocationOnEdge(lavasa, polyline, 0.05)) { | |
alert("lavasa is on way "); | |
} else { | |
alert("lavasa is not on way "); | |
} | |
if (google.maps.geometry.poly.isLocationOnEdge(blore, polyline, 0.1)) { | |
alert("blore is on way!"); | |
} else { | |
alert("blore is not on way "); | |
} | |
} | |
}); | |
} | |
google.maps.event.addDomListener(window, 'load', initialize); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment