Send coordinates in javascript and ASP.NET MVC

0

I have a question, I have a function in javascript, but apparently it does not receive the coordinates correctly, when you put the mouse over the link that the function calls, you can see that if the coordinates go. I enclose the code of the function and the error generated

function VerRuta(origen, destino){

           var ori = { lat: 0.0, lng: 0.0 };
           var dest = { lat: 0.0, lng: 0.0 };
           ///Para crear una ruta
           ////alert("origen: " + origen.lat + " " + origen.lng + " Destino: " + destino.lat + " " + destino.lng);
           var map = new google.maps.Map(document.getElementById('map-geolocation1'), {
               center: { lat: -0.248207, lng: -78.480393 },
               zoom: 8
           });
           var directionsDisplay = new google.maps.DirectionsRenderer({
               map: map
           });

           //// Asignar el destinatario y el origen, ademas del modo de viaje.
           alert(ori.lat);
           var request = {
               destination: destino,
               origin: origen,
               travelMode: 'DRIVING'
           };
           //// Pasamos las direcciones al servicio de direcciones.
           var directionsService = new google.maps.DirectionsService();
           directionsService.route(request, function (response, status) {

               if (status == 'OK') {
                   // Mostramos la ruta en el mapa.
                   directionsDisplay.setDirections(response);

               } else {
                   alert(status);
               }
           });
       }

and this is the error it throws:

  

"InvalidValueError: in property origin: not to string; and not to LatLng or LatLngLiteral: not to Object; and not to Object"

Apparently it does not detect a valid latitude and longitude.

    
asked by Javier Cordova 26.03.2017 в 20:07
source

1 answer

1

In the code I notice that you use

alert(ori.lat);

but at no time assign that variable to DirectionsService , instead you assign the parameter origen , which does not evaluate its content, therefore recommend uses

alert(origen.lat);

to evaluate if you define the coordinates that google maps will use

Also if you analyze the documentation

Directions Service

You will see that the origin and destination require a text, I do not see that they use latitude / longitude coordinates, you could try to define in a fixed way the json that you assign to directionsService.route() to validate that it works correctly

{
  origin: "Chicago, IL",
  destination: "Los Angeles, CA",
  travelMode: google.maps.TravelMode.DRIVING,
}

If you review the example

Place Autocomplete and Directions

You can analyze how the place resolves to define the origin and destination

    
answered by 28.03.2017 в 19:03