E
E
Evgeny Trofimov2016-04-12 00:58:58
Google
Evgeny Trofimov, 2016-04-12 00:58:58

Why can't I get directions on the map?

I want to build a route on the map from the user specified in the "start" field to the place that I specify.
There is a map:

<section id="cd-google-map">
        <div id="google-container"></div>
        <div id="cd-zoom-in"></div>
        <div id="cd-zoom-out"></div>
    </section>

and form:
<form action="" onsubmit="calcRoute();" id="start">
        <input type="text" id="start" value="">
        <input type="submit" value="Проложить маршрут">
    </form>

In code:
jQuery(document).ready(function($){
  //set your google maps parameters
  var latitude = 59.938615,
    longitude = 30.36936,
    map_zoom = 17;

  var style= [//какие-то стили]

  //set google map options
  var map_options = {
      	center: new google.maps.LatLng(latitude, longitude),
      	zoom: map_zoom,
      	panControl: false,
      	zoomControl: false,
      	mapTypeControl: false,
      	scrollwheel: false,
      	styles: style,
    }
    //init map
    var directionsService = new google.maps.DirectionsService;
    var directionsDisplay = new google.maps.DirectionsRenderer;
  var map = new google.maps.Map(document.getElementById('google-container'), map_options);
  directionsDisplay.setMap(map);

function calculateAndDisplayRoute(directionsService, directionsDisplay) {
  directionsService.route({
    origin: document.getElementById('start').value,
    destination: "59.938615,30.36936",
    travelMode: google.maps.TravelMode.DRIVING
  }, function(response, status) {
    if (status === google.maps.DirectionsStatus.OK) {
      directionsDisplay.setDirections(response);
    } else {
      window.alert('Directions request failed due to ' + status);
    }
  });
}

});


Please tell me - what's wrong? Clicking on "Get directions" simply refreshes the page.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
E
Evgeny Trofimov, 2016-04-14
@deadrime

In general, I didn’t understand why it didn’t work - it seemed like I did everything according to the example from Google .. But I found a ready-made and working example - tyts
Mb will come in handy for someone.

O
oleg_drozdov, 2016-04-12
@oleg_drozdov

onsubmit="calcRoute();"
And where is the function itself with this name?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question