Route in Google Maps using coordinates

Asked

Viewed 1,342 times

0

I have this code for creating routes using Google Maps

var map;
var directionsDisplay; // Instanciaremos ele mais tarde, que será o nosso     google.maps.DirectionsRenderer
var directionsService = new google.maps.DirectionsService();

function initialize() {
  directionsDisplay = new google.maps.DirectionsRenderer(); // Instanciando...
  var latlng = new google.maps.LatLng(-18.8800397, -47.05878999999999);

  var options = {
    zoom: 5,
    center: latlng,
    mapTypeId: google.maps.MapTypeId.ROADMAP
  };

  map = new google.maps.Map(document.getElementById("mapa"), options);
  directionsDisplay.setMap(map); // Relacionamos o directionsDisplay com o mapa desejado
}

initialize();

$("form").submit(function(event) {
  event.preventDefault();

  var enderecoPartida = $("#txtEnderecoPartida").val();
  var enderecoChegada = $("#txtEnderecoChegada").val();

  var request = { // Novo objeto google.maps.DirectionsRequest, contendo:
    origin: enderecoPartida, // origem
    destination: enderecoChegada, // destino
    travelMode: google.maps.TravelMode.DRIVING // meio de transporte, nesse caso, de carro
  };

  directionsService.route(request, function(result, status) {
    if (status == google.maps.DirectionsStatus.OK) { // Se deu tudo certo
      directionsDisplay.setDirections(result); // Renderizamos no mapa o resultado
    }
  });
});

I found this code on the link Blog Princiweb

I need to use the route using coordinates directly, without using Geoprocessing to convert the coordinates to address.

  • You can pass the lat/lng to the properties origin and destination, need not necessarily be an address (String). See more in the documentation of Google Maps API.

  • Thank you Douglas, I was looking for this documentation.

  • Okay, I’ll leave that as an answer then. Just mark it as the correct one by doing.

1 answer

1


As I had commented, it is not necessary to move to the properties origin and destination an address (String), you can pass latitude and longitude.

For further clarification, please check the Google Maps API.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.