Avatar of Lanmarkian
Lanmarkian asked on

Enter Address and have Google Maps Directions appear on submit

Here are the steps that I need.

- Go to website, only see form field that says "Enter your address"
- On submit, below the form field, the user now sees a map and the directions listed out. (the destination address has to already be pre-populated)

How can I accomplish this? Are there any samples out there? I can't seem to find anything online.

Thank you.
JavaScriptjQueryCSS

Avatar of undefined
Last Comment
Tom Beck

8/22/2022 - Mon
Big Monty

here's a tutorial that allows you to enter in an address and it'll display directions to the address on your site:

http://charliesaidthat.com/digital/web-design/all-small-businesses-need-this-how-to-add-google-map-directions-for-customers-to-your-website/
ASKER
Lanmarkian

No, that takes you to google maps site. I want everything embedded on my website. When the user enters their address, the map and directions appear directly below the form field.

Please re-read my original post.
Gary

Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
Big Monty

my apologies, i did misread the question...this method should still work thought, just embed the url into an iFrame on your site and it should work.

if that doesnt do the trick, try looking at their API:

https://developers.google.com/maps/documentation/javascript/basics
ASKER
Lanmarkian

OK - How can I hide/show this div when the user hits submit?
ASKER
Lanmarkian

The div would contain the iframe.
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Gary

Everything is contained in the iframe, you cannot be hiding/showing it.
ASKER
Lanmarkian

That's the point of the question. Then the iframe method will not work. Any other thoughts?
Gary

Are we talking about the link I posted, there is no iframe.  Did you even try it?
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
ASKER
Lanmarkian

Cathill - I did look at that, but I don't want the map viewable until after the user enters directions.
ASKER
Lanmarkian

I also need full control over styling for the form etc. and I have used this widget in the past - There isn't a way to get rid of the drop down etc.
Gary

Then I suggest you start reading their API documentation
https://developers.google.com/maps/documentation/directions/
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
ASKER
Lanmarkian

Still having trouble even with API - maybe I just don't know enough. Are there any samples of this being done?
Gary

Tom Beck

That's not a good example. It uses Google Maps v2 instead of v3 and makes getting directions far more complicated. Google API v3 provides a Directions Service that makes it much easier.

Complete working example based on these instructions:
https://developers.google.com/maps/documentation/javascript/examples/directions-simple
<!DOCTYPE html>
<html>
  <head>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
    <meta charset="utf-8">
    <title>Directions service</title>
    <style>
      html, body, #map-canvas {
        height: 100%;
		width: 100%;
        margin: 0px;
        padding: 0px
      }
	  #map-canvas {
		position: absolute;
		left: -9999px
	  }
	  #start, #end {
		  width: 300px
	  }
	  
    </style>
    <script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script>
    <script>
var directionsDisplay;
var directionsService = new google.maps.DirectionsService();
var map;

function initialize() {
  directionsDisplay = new google.maps.DirectionsRenderer();
  var chicago = new google.maps.LatLng(41.850033, -87.6500523);
  var mapOptions = {
    zoom:7,
    center: chicago
  };
  map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
  directionsDisplay.setMap(map);
}

function calcRoute() {
  var start = document.getElementById('start').value;
  var end = document.getElementById('end').value;
  var request = {
      origin:start,
      destination:end,
      travelMode: google.maps.TravelMode.DRIVING
  };
  directionsService.route(request, function(response, status) {
    if (status == google.maps.DirectionsStatus.OK) {
      directionsDisplay.setDirections(response);
    }
  });
  document.getElementById("map-canvas").style.left = 0;
}

google.maps.event.addDomListener(window, 'load', initialize);

    </script>
</head>

<body>
<label for="pointA">Point A</label>
<input type="text" id="start" /><br />
<label for="pointB">Point B</label>
<input type="text" id="end" />
<input type="button" value="Submit" onclick="calcRoute()" />
<div id="map-canvas"></div>
</body>
</html>

Open in new window

Here's another good example that adds auto-complete to the entering of addresses.
https://developers.google.com/maps/documentation/javascript/examples/places-autocomplete?csw=1
Your help has saved me hundreds of hours of internet surfing.
fblack61
Gary

Where in that linked page does it say v2
Where in your example are the directions?
Tom Beck

line 12, "v=2"
http://maps.google.com/?file=api&v=2&key=ABQIAAAAjU0EJWnWPMv7oQ-jjS7dYxSPW5CJgpdgO_s4yyMovOaVh_KvvhSfpvagV18eOyDWu7VytS6Bi1CWxw

You mean printed out directions? I missed that part of the question.
Gary

Simple fix - change it to 3, but it seems to be using v3 regardless
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
ASKER CERTIFIED SOLUTION
Tom Beck

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question