1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
   | <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
<script type="text/javascript">
 
	var map;
	var geocoder;
	var marker;
 
	function initialize() {
	  var nantes = new google.maps.LatLng(47.216842,-1.556744);
	  var myOptions = {
	    zoom:11,
	    mapTypeId: google.maps.MapTypeId.ROADMAP,
	    center: nantes
	  }
	  map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);e
	}
 
 
	/*
	 * C'est ici que ça se complique
	 */
 
	function codeLatLng(lat, lng) {
	  geocoder = new google.maps.Geocoder(); //module pour récupérer un nom en fonction des coordonnées GPS
	  var latlng = new google.maps.LatLng(lat, lng);
	  geocoder.geocode({'latLng': latlng}, function(results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
          var str = "";
          str += results[0].formatted_address;
          return str;
 
          //ici ce return ne marche pas, la fonction me renvoi en faite "undefined"
          // alors qu'un alert(str); fonctionne très bien
 
                } else {
                    alert("Geocoder failed due to: " + status);
                }
            });
        }
 
</script>
 
/*
 * Et un peu plus loin dans mon HTML, j'ai ce bout de code :
 */
 
<script type="text/javascript">
  var start = "";
  start += codeLatLng(47.206927,-1.555082);
  document.write(start); //Ici je dois normalement récupérer une adresse de depart
</script>
//mais ça me renvoi "undefined"
 
//[...]
 
<script type="text/javascript">
  var end = "";
  end += codeLatLng(46.206927,-1.450082);
  document.write(end); //Ici je dois normalement récupérer une adresse d'arrivée
</script>
//mais ça me renvoi "undefined" | 
Partager