On Jul 29, 10:13 am, stephane vtk <[email protected]> wrote: > Good evening, > I try to recover the distance between two points via API v3 > google. > But I do not get anything back for my variable "resultat". > I just want to show the value of matches without using > of <div> or show a card. > Here is my code: > /////////////////////////////////////////////////////////////////////////////////////////////////////////////////// > <script type="text/javascript" src="http://maps.google.com/maps/ > api/ > js?sensor=false"></script> > > </head> > > <body > > <script type="text/javascript"> > var directionsService = new > google.maps.DirectionsService(); > > var requeteItineraire = { > origin: "50.589780,4.354858", > destination: > "50.589730,4.355024", > travelMode: > google.maps.DirectionsTravelMode.DRIVING > }; > > directionsService.route(requeteItineraire, function(response, > status) { > if (status == > google.maps.DirectionsStatus.OK) > { > > directionsService.setDirections(response); > > depart = > response.routes[0].legs[0].start_address; > > arrivee = > response.routes[0].legs[0].end_address; > > distance = > response.routes[0].legs[0].distance.text; > > resultat = "La distance > entre<br />"+depart+"<br />et<br / > > >"+arrivee+"<br />est de "+distance; > > document.write(resultat); > } > </script> > /////////////////////////////////////////////////////////////////////////////////////// > Thank you > Stéphane
The directions service in the Google Maps API is asynchronous. You need to wait for the data to come back, at which point the callback function will run. At that point, document.write will overwrite you html document (you can not use that function once the DOM is complete). Perhaps you should investigate using the directions web service: http://code.google.com/apis/maps/documentation/directions/ However, you should carefully review the terms of use per this statement on that page (you have to display the directions on a Google map): + Note: the Directions API may only be used in conjunction with displaying results on a Google map; using + Directions data without displaying a map for which directions data was requested is prohibited. Additionally, + calculation of directions generates copyrights and warnings which must be displayed to the user in some fashion. + For complete details on allowed usage, consult the Maps API Terms of Service License Restrictions. -- Larry -- You received this message because you are subscribed to the Google Groups "Google Maps JavaScript API v3" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/google-maps-js-api-v3?hl=en.
