I am doing a Google map project and you need to create a route by clicking on it.
In my project, there are 2 points that have predefined lat and lng, and I want to draw the beginning and end of points A and B myself and not lose its functionality.
I made another project that you can click to draw a route, but it does not have markers, and it does not drag, this is my actual project with full code. I will post a short code here only with my instructions.
I want my first click to point A and my second point B, and they have the ability to drag them, for example, related to the project
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 am updating my code here, I only made wayA, this is the first waypoint, the second is the predefined latLng, where you click, it gets latLng and is placed inside "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 full code test.