Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function initMap() {
- var map = new google.maps.Map(document.getElementById('harta'), {
- zoom: 7,
- center: {lat: 46.095968, lng: 24.915397}
- });
- var geocoder = new google.maps.Geocoder;
- var infowindow = new google.maps.InfoWindow;
- document.getElementById('gasireLocatie1').addEventListener('click', function() {
- geocodareAdresa(geocoder, map, infowindow, 'adresa1');
- });
- document.getElementById('gasireLocatie2').addEventListener('click', function() {
- geocodareAdresa(geocoder, map, infowindow, 'adresa2');
- });
- document.getElementById('calculeazaDistanta').addEventListener('click', function() {
- calculeazaDistanta(geocoder, map, infowindow);
- });
- }
- function geocodareAdresa(geocoder, map, infowindow, adresa) {
- var textAdresa = document.getElementById(adresa).value;
- geocoder.geocode({'address': textAdresa}, function(results, status) {
- if (status === 'OK') {
- if (results[0]) {
- map.setZoom(11);
- map.setCenter(results[0].geometry.location);
- var marker = new google.maps.Marker({
- map: map,
- position: results[0].geometry.location
- });
- infowindow.setContent(results[0].formatted_address);
- infowindow.open(map, marker);
- } else {
- window.alert('Niciun rezultat pentru locaţia introdusă!');
- }
- } else {
- window.alert('Geocodarea a eşuat: ' + status);
- }
- });
- }
- function calculeazaDistanta(geocoder, map, infowindow)
- {
- var adresa1 = document.getElementById('adresa1').value;
- var adresa2 = document.getElementById('adresa2').value;
- var locatie1, locatie2;
- var marker1, marker2;
- geocoder.geocode({'address': adresa1}, function(results, status) {
- if (status === 'OK') {
- if (results[0]) {
- marker1 = new google.maps.Marker({
- map: map,
- position: results[0].geometry.location
- });
- locatie1 = results[0].geometry.location;
- infowindow.setContent(results[0].formatted_address);
- infowindow.open(map, marker1);
- geocoder.geocode({'address': adresa2}, function(results, status) {
- if (status === 'OK') {
- if (results[0]) {
- marker2 = new google.maps.Marker({
- map: map,
- position: results[0].geometry.location
- });
- locatie2 = results[0].geometry.location;
- infowindow.setContent(results[0].formatted_address);
- infowindow.open(map, marker2);
- var service = new google.maps.DistanceMatrixService();
- service.getDistanceMatrix({
- origins: [locatie1],
- destinations: [locatie2],
- travelMode: google.maps.TravelMode.DRIVING,
- unitSystem: google.maps.UnitSystem.METRIC,
- avoidHighways: false,
- avoidTolls: false
- }, function (response, status) {
- if (status == google.maps.DistanceMatrixStatus.OK && response.rows[0].elements[0].status != "ZERO_RESULTS") {
- var distance = response.rows[0].elements[0].distance.text;
- var duration = response.rows[0].elements[0].duration.text;
- var dvDistance = document.getElementById("distanta");
- dvDistance.innerHTML = "";
- dvDistance.innerHTML += "Distanţă în linie dreaptă: " +
- Math.round(google.maps.geometry.spherical.computeDistanceBetween(locatie1, locatie2) / 1000)
- + " km<br />";
- dvDistance.innerHTML += "Distanţă pe şosea: " + distance + "<br />";
- // dvDistance.innerHTML += "Durată drum: " + duration;
- dvDistance.style.display = "block";
- } else {
- alert("Imposibil de găsit distanţa de parcurs.");
- }
- });
- } else {
- window.alert('Niciun rezultat pentru locaţia introdusă!');
- }
- } else {
- window.alert('Geocodarea a eşuat: ' + status);
- }
- });
- } else {
- window.alert('Niciun rezultat pentru locaţia introdusă!');
- }
- } else {
- window.alert('Geocodarea a eşuat: ' + status);
- }
- });
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement