Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8317119
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T21:30:58+00:00 2026-06-08T21:30:58+00:00

I’m making a google maps project and need to create a route clicking on

  • 0

I’m making a google maps project and need to create a route clicking on it.

My project has 2 points that has a predefined lat and lng, and I want to draw by myself the start and the end of the point A and B and don’t lose it functionality.

I made another project that you can click to draw the route but it don’t have markers, and is not draggabble, this is my actual project with the full code
I will post here a short code only with my directions.

I want that my first click to the point A and the second my point B, and them the possibility to drag them, like the project linked

function goma()
{   
    var mapDiv = document.getElementById('mappy');
    var mapOptions = {
    zoom: 12, 
    center: new google.maps.LatLng(-23.563594, -46.654239),
    mapTypeId : google.maps.MapTypeId.ROADMAP
    }
    pode ser ROADMAP, SATELLITE, HYBRID, TERRAIN
    map = new google.maps.Map( mapDiv, mapOptions ); 

    //Render route, etc.
    ren = new google.maps.DirectionsRenderer( {'draggable':true} );
    ren.setMap(map);
    ren.setPanel(document.getElementById("directionsPanel"));
    ser = new google.maps.DirectionsService();

    //Create the route
    ser.route({ 'origin': new google.maps.LatLng(-23.563594, -46.654129), 'destination':  new google.maps.LatLng(
-23.563672, -46.640396), 'travelMode': google.maps.DirectionsTravelMode.DRIVING},function(res,sts) {
        if(sts=='OK')ren.setDirections(res);    
    })      

}   

I’m updating here my code, I done only wayA, that is the first waypoint, the second is a latLng predefined, where you click, it gets the latLng and put inside the ‘origin’.

    google.maps.event.addListener(map, "click", function(event) {
            wayA = new google.maps.Marker({ 
              position: event.latLng,
              map: map                
            });
     });
    ren = new google.maps.DirectionsRenderer( {'draggable':true} );
    ren.setMap(map);
    ren.setPanel(document.getElementById("directionsPanel"));
    ser = new google.maps.DirectionsService();          
    ser.route({ 'origin': wayA, 'destination':  new google.maps.LatLng(
-23.563672, -46.640396), 'travelMode': google.maps.DirectionsTravelMode.DRIVING},function(res,sts) {
        if(sts=='OK')ren.setDirections(res);    
    })      

This is my test with the full code

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T21:31:00+00:00Added an answer on June 8, 2026 at 9:31 pm

    Concept:
    (sounds like this is what you want)

    1. add a click event listener to the map
    2. when the user clicks the map add a draggable marker (I would add a click listener to it so they can delete it by clicking on it)
    3. when the user clicks on the map a second time add a second draggable marker
    4. when the second marker is added, call the directions service with the position of the two markers.
    5. if either marker is dragged call the directions service again.

    (if this what you are trying and are running into problems, code or a live link would be helpful)

    You are missing #3 and #4

    Working example

    code snippet:

    var map, ren, ser;
    var data = {};
    var data2 = {};
    var marker;
    var infowindow;
    var doMark = true;
    var directionsDisplay;
    
    var wayA;
    var wayB;
    
    //Função de Inicio
    
    function goma() {
    
      var mapDiv = document.getElementById('mappy');
    
      var mapOptions = {
          zoom: 12,
    
          center: new google.maps.LatLng(-23.563594, -46.654239),
          mapTypeId: google.maps.MapTypeId.ROADMAP
        }
        //Cria o mapa do google, coloca as definições do mapa, como tipo de visualização, pode ser ROADMAP, SATELLITE, HYBRID, TERRAIN
      map = new google.maps.Map(mapDiv, mapOptions);
    
    
      var control = document.createElement('DIV');
      control.style.padding = '1px';
      control.style.border = '1px solid #000';
      control.style.backgroundColor = 'white';
      control.style.cursor = 'pointer';
      control.innerHTML = '<img src="https://web.archive.org/web/20151226013612if_/http://i47.tinypic.com/2dlp2fc.jpg" border="0" alt="Image and video hosting by TinyPic">';
      control.index = 1;
    
    
      map.controls[google.maps.ControlPosition.TOP_RIGHT].push(control);
    
      google.maps.event.addDomListener(control, 'click', function() {
        doMark = false;
        markNow();
    
    
      });
    
      google.maps.event.addListener(map, "click", function(event) {
        if (!wayA) {
          wayA = new google.maps.Marker({
    
            position: event.latLng,
            map: map
    
          });
        } else {
          wayB = new google.maps.Marker({
    
            position: event.latLng,
            map: map
    
          });
    
          //Renderiza a rota, o draggable é diz se o waypoint é arrastavel ou não
          ren = new google.maps.DirectionsRenderer({
            'draggable': true
          });
          ren.setMap(map);
          ren.setPanel(document.getElementById("directionsPanel"));
          ser = new google.maps.DirectionsService();
    
          //Cria a rota, o DirectionTravelMode pode ser: DRIVING, WALKING, BICYCLING ou TRANSIT
          ser.route({
            'origin': wayA.getPosition(),
            'destination': wayB.getPosition(),
            'travelMode': google.maps.DirectionsTravelMode.DRIVING
          }, function(res, sts) {
            if (sts == 'OK') ren.setDirections(res);
          })
    
        }
      });
    }
    
    var html = "<table>" +
      "<tr><td>Nome:</td> <td><input type='text' id='name'/> </td> </tr>" +
      "<tr><td>Endereco:</td> <td><input type='text' id='address'/></td> </tr>" +
      "<tr><td>Tipo:</td> <td><select id='type'>" +
      "<option value='oficina' SELECTED>oficina</option>" +
      "<option value='restaurante'>restaurante</option>" +
      "</select> </td></tr>" +
      "<tr><td></td><td><input type='button' value='Salvar' onclick='saveData()'/></td></tr>";
    infowindow = new google.maps.InfoWindow({
      content: html
    });
    
    google.maps.event.addDomListener(window, 'load', goma);
    html,
    body {
      height: 100%;
      width: 100%;
    }
    <script src="https://maps.google.com/maps/api/js?key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk"></script>
    <div id="mappy" style="float:left;width:70%; height:100%"></div>
    <div id="directionsPanel" style="float:right;width:30%;height 100%"></div>
    <div>
      <label>Endereco</label>
    </div>
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm making a simple page using Google Maps API 3. My first. One marker
That's pretty much it. I'm using Nokogiri to scrape a web page what has
Basically, what I'm trying to create is a page of div tags, each has
I've got a string that has curly quotes in it. I'd like to replace
In my XML file chapters tag has more chapter tag.i need to display chapters
I need a function that will clean a strings' special characters. I do NOT
I'm trying to create an if statement in PHP that prevents a single post
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.