Google Maps Drag Markers For Directions

Result

Drag directions and click the map to geocode.
<!DOCTYPE html>
  <html>
  <head>
  <meta charset="utf-8">
  <title>Google Maps Drag Markers For Directions</title>
  <script type="text/javascript" src="https://www.google.com/jsapi"></script>
 <style type="text/css">
  html, body {
  color: #585858;
  font-family: sans-serif;
  height: 96%;
  margin: 2%;
  padding: 2%;
  }
 #map_div {
  height: 100%;
  }
  #map_canvas {
  height: 80%;
  width: 70%;
  margin: 10px;
  box-shadow: 1px 1px 4px 2px rgba(0,0,0 ,0.7);
  float: left;
  }
  #textbox{
  float: right;
  width: 25%;
  height: 80%;
  overflow: auto;
  }
  .hidden{
  visibility: hidden;
  display: none
  }
  .visible{
  visibility: visible,
  display: block
  }
  </style>
 <script type="text/javascript">
  var map;
  var infowindow;
  var directionsService;
  var directionsDisplay;
  var geocoder;
  var infoWindow;
  var marker;
 function mapsLoaded() {
  
  directionsService = new google.maps.DirectionsService();
  geocoder = new google.maps.Geocoder();
  infowindow = new google.maps.InfoWindow();
  var torun = new google.maps.LatLng(53.01357, 18.597665);
  map = new google.maps.Map(document.getElementById('map_canvas'), {
  mapTypeId: google.maps.MapTypeId.ROADMAP,
  center: torun,
  zoom: 5
  });
  google.maps.event.addListener(map, 'click', function(e) {
  geocoder.geocode(
  {'latLng': e.latLng},
  function(results, status) {
  if (status == google.maps.GeocoderStatus.OK) {
  if (results[0]) {
  if (marker) {
  marker.setPosition(e.latLng);
  } else {
  marker = new google.maps.Marker({
  position: e.latLng,
  map: map});
  }
  infowindow.setContent(results[0].formatted_address);
  infowindow.open(map, marker);
  } else {
  document.getElementById('geocoding').innerHTML =
  'No results found';
  }
  } else {
  document.getElementById('geocoding').innerHTML =
  'Geocoder failed due to: ' + status;
  }
  });
  });
  showDirections();
  }
 function showDirections() {
  directionsDisplay = new google.maps.DirectionsRenderer({
  map: map,
  preserveViewport: true,
  draggable: true
  });
  directionsDisplay.setPanel(document.getElementById('textbox'));
  var sampleRequest = {
  origin: 'Stoke on Trent, UK',
  destination: 'Budapest, Hungary',
  travelMode: google.maps.TravelMode.DRIVING,
  unitSystem: google.maps.UnitSystem.METRIC
  };
  directionsService.route(sampleRequest, function(response, status) {
  if (status == google.maps.DirectionsStatus.OK) {
  directionsDisplay.setDirections(response);
  }
  });
  }
 function loadMap(langCode) {
  google.load('maps', '3.7', {
  'other_params' :
  'sensor=false&libraries=places&language=en-GB',
  'callback' : mapsLoaded
  });
 }
 var langCode = 'en-GB';
  
  loadMap(langCode);
  </script>
  </head>
  <body>
  <div id="map_canvas"></div>
  <div id="textbox">
  Drag directions and click the map to geocode.<br>
  <div id="geocoding"> </div>
  <div id="directions"> </div>
  </div>
  </div>
  </body>
  </html>

Available For Freelance:

I Am Available To Work For You Freelance, See My Skills To See If I Can Help Your Project, If I Can Help You Then Please Contact Me.

My Rates Start From £180 A Day, Discount Prices For Regular Clients And Work Over 3 Days.

I Started To Learn Web Development In 2008, I Did A Few Open University Courses While Working Full Time In The Building Trade, I Managed To Get A Diploma With Distinction

Request A Tutorial

If You Are Having Difficulty With The Development Of Your Website Or If You Are Just Starting Out Leaning Web Development And You Think I Can Help You Then Request A Tutorial, I Will Do My Best To Help You

I Dont Charge For My Services If I Can Help You With Something That Can Be Be Fixed In A Few Minutes.

Advertise

If You Would Like To Advertise On This Site Then Please Contact Me, You Will Be Given Login Details To See What Day And Where Clicks Have Come From.

Prices Start From £10 A Month With Discounts Available For 3 Months Or More.

You Can Either Send Me The Banner Advert That You Have Designed Or From £10 I Can Have Designer Design A Banner For You.

Please Leave A Comment And A Rating





image

Chris Gave This Code Rating

Google Maps Drag the 2 markers to get directions, hope it helps

image

Ayanna Defilippo Gave This Code Rating

This is good site to watch.Thanks for shareing the posts.

www.codecaven.co.uk image 5.00 out of 5 based on 2 ratings. 2 user reviews.

My Skills:

  • PHP 5
  • JavaScript
  • Jquery
  • XHTML Validation
  • KineticJS
  • HTML5
  • CSS - CSS3
  • SEO

My Social Networks:

  • facebook youtube