Dynamic Blinkie Text Generator at TextSpace.net

Feedburner

I heart FeedBurner

Senin, 05 Februari 2018

GOOGLE Maps API direction website

Selamat malam teman teman hari ini saya akan sharing tentang google maps API directions untuk website. Sebagai alternatif untuk menampilkan rute di website :
1). Daftar seperti biasa di google console
2).  Buat project di google console
3). Pilih dan aktifkan Google Maps Api Direction
4). buat api di credential setelah aktif copas apinya ke kode di bawah ini :
<!DOCTYPE html>
<html>
  <head>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
    <meta charset="utf-8">
    <title>Directions service</title>
    <style>
      /* Always set the map height explicitly to define the size of the div
       * element that contains the map. */
      #map {
        height: 100%;
      }
      /* Optional: Makes the sample page fill the window. */
      html, body {
        height: 100%;
        margin: 0;
        padding: 0;
      }
      #floating-panel {
        position: absolute;
        top: 10px;
        left: 25%;
        z-index: 5;
        background-color: #fff;
        padding: 5px;
        border: 1px solid #999;
        text-align: center;
        font-family: 'Roboto','sans-serif';
        line-height: 30px;
        padding-left: 10px;
      }
    </style>
  </head>
  <body>
    <div id="floating-panel">
   
   
    <b>Posisi Anda: </b>
    <select id="end">
      <option value="Stasiun Sudirman">Stasiun Sudirman</option>
      <option value="Bandar Udara Internasional Halim Perdanakusuma (HLP)">Halim Perdana Kusumah</option>
      <option value="Stasiun Manggarai">Stasiun Manggarai</option>
      <option value="Bandara Soekarno-Hatta International Airport">Bandara Soekarno-Hatta International Airport</option>
      <option value="Stasiun Pasar Senen">Stasiun Pasar Senen</option>
      <option value="stasiun jatinegara">stasiun jatinegara</option>
      <option value="Stasiun Tanahabang">Stasiun Tanahabang</option>
      <option value="stasiun tangerang">stasiun tangerang</option>
      <option value="Stasiun Sudirman Baru">Stasiun Sudirman Baru</option>
      <option value="Stasiun Cikini">Stasiun Cikini</option>
      <option value="Stasiun Gambir">Stasiun Gambir</option>
      <option value="Stasiun Lenteng Agung">Stasiun Lenteng Agung</option>
    </select>
    </div>
    <div id="map"></div>
   
    <script>
      var myLatLng={lat: -6.231375, lng: 106.821450};
      function initMap() {
          
        var directionsService = new google.maps.DirectionsService;
        var directionsDisplay = new google.maps.DirectionsRenderer({
          draggable: true,
          myLatLng: myLatLng
         
        });
        var map = new google.maps.Map(document.getElementById('map'), {
          zoom: 19,
          center:myLatLng,
         
        });
        var marker = new google.maps.Marker({
        position: myLatLng,
        map: map,
       
        title: 'Lokasi Kantor'
  });

        directionsDisplay.setMap(map);

        var onChangeHandler = function() {
          calculateAndDisplayRoute(directionsService, directionsDisplay);
        };
        document.getElementById('end').addEventListener('change', onChangeHandler);
        document.getElementById('end').addEventListener('change', onChangeHandler);
      }

      function calculateAndDisplayRoute(directionsService, directionsDisplay) {
         
         
        directionsService.route({
          origin: myLatLng,
          destination: document.getElementById('end').value,
          travelMode: 'DRIVING'
         
        }, function(response, status) {
          if (status === 'OK') {
            directionsDisplay.setDirections(response);
          } else {
            window.alert('Directions request failed due to ' + status);
          }
        });
      }
    </script>
      <script async defer src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCicrveXVBwHB9b8BIBv-uW_TLfsty1y-8&callback=initMap"
  type="text/javascript"></script>
  </body>
 
</html>

Atau contoh lain :
<!DOCTYPE html>
<html>
  <head>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
    <meta charset="utf-8">
    <title>Directions service</title>
    <style>
      /* Always set the map height explicitly to define the size of the div
       * element that contains the map. */
      #map {
        height: 100%;
      }
      /* Optional: Makes the sample page fill the window. */
      html, body {
        height: 100%;
        margin: 0;
        padding: 0;
      }
      #floating-panel {
        position: absolute;
        top: 10px;
        left: 25%;
        z-index: 5;
        background-color: #fff;
        padding: 5px;
        border: 1px solid #999;
        text-align: center;
        font-family: 'Roboto','sans-serif';
        line-height: 30px;
        padding-left: 10px;
      }
    </style>
  </head>
  <body>
    <div id="floating-panel">
    <b>Start: </b>
    <select id="start">
      <option value="chicago, il">Chicago</option>
      <option value="st louis, mo">St Louis</option>
      <option value="joplin, mo">Joplin, MO</option>
      <option value="oklahoma city, ok">Oklahoma City</option>
      <option value="amarillo, tx">Amarillo</option>
      <option value="gallup, nm">Gallup, NM</option>
      <option value="flagstaff, az">Flagstaff, AZ</option>
      <option value="winona, az">Winona</option>
      <option value="kingman, az">Kingman</option>
      <option value="barstow, ca">Barstow</option>
      <option value="san bernardino, ca">San Bernardino</option>
      <option value="los angeles, ca">Los Angeles</option>
    </select>
    <b>End: </b>
    <select id="end">
      <option value="Stasiun Sudirman">Stasiun Sudirman</option>
      <option value="Monas, Gambir">Monas</option>
      <option value="Stasiun Manggarai">Stasiun Manggarai</option>
      <option value="oklahoma city, ok">Oklahoma City</option>
      <option value="amarillo, tx">Amarillo</option>
      <option value="gallup, nm">Gallup, NM</option>
      <option value="flagstaff, az">Flagstaff, AZ</option>
      <option value="winona, az">Winona</option>
      <option value="kingman, az">Kingman</option>
      <option value="barstow, ca">Barstow</option>
      <option value="san bernardino, ca">San Bernardino</option>
      <option value="los angeles, ca">Los Angeles</option>
    </select>
    </div>
    <div id="map"></div>
   
    <script>
      var myLatLng={lat: -6.231375, lng: 106.821450};
      function initMap() {
          
        var directionsService = new google.maps.DirectionsService;
        var directionsDisplay = new google.maps.DirectionsRenderer({
          draggable: true,
          myLatLng: myLatLng
         
        });
        var map = new google.maps.Map(document.getElementById('map'), {
          zoom: 19,
          center:myLatLng,
         
        });
        var marker = new google.maps.Marker({
        position: myLatLng,
        map: map,
       
        title: 'Lokasi Kantor'
  });

        directionsDisplay.setMap(map);

        var onChangeHandler = function() {
          calculateAndDisplayRoute(directionsService, directionsDisplay);
        };
        document.getElementById('start').addEventListener('change', onChangeHandler);
        document.getElementById('end').addEventListener('change', onChangeHandler);
      }

      function calculateAndDisplayRoute(directionsService, directionsDisplay) {
         
         
        directionsService.route({
          origin: myLatLng,
          destination: document.getElementById('end').value,
          travelMode: 'DRIVING'
         
        }, function(response, status) {
          if (status === 'OK') {
            directionsDisplay.setDirections(response);
          } else {
            window.alert('Directions request failed due to ' + status);
          }
        });
      }
    </script>
      <script async defer src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCicrveXVBwHB9b8BIBv-uW_TLfsty1y-8&callback=initMap"
  type="text/javascript"></script>
  </body>
 
</html>

Atau contoh lain :
<!DOCTYPE html>
<html>
  <head>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
    <meta charset="utf-8">
    <title>Directions service</title>
    <style>
      /* Always set the map height explicitly to define the size of the div
       * element that contains the map. */
      #map {
        height: 100%;
      }
      /* Optional: Makes the sample page fill the window. */
      html, body {
        height: 100%;
        margin: 0;
        padding: 0;
      }
      #floating-panel {
        position: absolute;
        top: 10px;
        left: 25%;
        z-index: 5;
        background-color: #fff;
        padding: 5px;
        border: 1px solid #999;
        text-align: center;
        font-family: 'Roboto','sans-serif';
        line-height: 30px;
        padding-left: 10px;
      }
    </style>
  </head>
  <body>
    <div id="floating-panel">
    <b>Start: </b>
    <select id="start">
      <option value="chicago, il">Chicago</option>
      <option value="st louis, mo">St Louis</option>
      <option value="joplin, mo">Joplin, MO</option>
      <option value="oklahoma city, ok">Oklahoma City</option>
      <option value="amarillo, tx">Amarillo</option>
      <option value="gallup, nm">Gallup, NM</option>
      <option value="flagstaff, az">Flagstaff, AZ</option>
      <option value="winona, az">Winona</option>
      <option value="kingman, az">Kingman</option>
      <option value="barstow, ca">Barstow</option>
      <option value="san bernardino, ca">San Bernardino</option>
      <option value="los angeles, ca">Los Angeles</option>
    </select>
    <b>End: </b>
    <select id="end">
      <option value="chicago, il">Chicago</option>
      <option value="st louis, mo">St Louis</option>
      <option value="joplin, mo">Joplin, MO</option>
      <option value="oklahoma city, ok">Oklahoma City</option>
      <option value="amarillo, tx">Amarillo</option>
      <option value="gallup, nm">Gallup, NM</option>
      <option value="flagstaff, az">Flagstaff, AZ</option>
      <option value="winona, az">Winona</option>
      <option value="kingman, az">Kingman</option>
      <option value="barstow, ca">Barstow</option>
      <option value="san bernardino, ca">San Bernardino</option>
      <option value="los angeles, ca">Los Angeles</option>
    </select>
    </div>
    <div id="map"></div>
   
    <script>
      function initMap() {
        var map = new google.maps.Map(document.getElementById('map'), {
          zoom: 4,
          center: {lat: -24.345, lng: 134.46}  // Australia.
        });

        var directionsService = new google.maps.DirectionsService;
        var directionsDisplay = new google.maps.DirectionsRenderer({
          draggable: true,
          map: map,
          panel: document.getElementById('right-panel')
        });

        directionsDisplay.addListener('directions_changed', function() {
          computeTotalDistance(directionsDisplay.getDirections());
        });

        displayRoute('Perth, WA', 'Sydney, NSW', directionsService,
            directionsDisplay);
      }

      function displayRoute(origin, destination, service, display) {
        service.route({
          origin: origin,
          destination: destination,
          waypoints: [{location: 'Adelaide, SA'}, {location: 'Broken Hill, NSW'}],
          travelMode: 'DRIVING',
          avoidTolls: true
        }, function(response, status) {
          if (status === 'OK') {
            display.setDirections(response);
          } else {
            alert('Could not display directions due to: ' + status);
          }
        });
      }

      function computeTotalDistance(result) {
        var total = 0;
        var myroute = result.routes[0];
        for (var i = 0; i < myroute.legs.length; i++) {
          total += myroute.legs[i].distance.value;
        }
        total = total / 1000;
        document.getElementById('total').innerHTML = total + ' km';
      }
    </script>

      <script async defer src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCicrveXVBwHB9b8BIBv-uW_TLfsty1y-8&callback=initMap"
  type="text/javascript"></script>
  </body>
 
</html>

Contoh Project saya :
http://ap2hni.com/

Tidak ada komentar:

Posting Komentar