0

I have a problem in the User location variable I can't use it after on my website! I received an error message that this variable is not defined!! I can't use the values of latitude and longitude to put it in a variable. please any solution?[

        // On initialise la latitude et la longitude de Paris (centre de la carte)  
        var lat = 48.3903;
        var lon = -4.4863;
        var map = null;
        // Fonction d'initialisation de la carte
        function initMap() {
            // Créer l'objet "map" et l'insèrer dans l'élément HTML qui a l'ID "map"
            map = new google.maps.Map(document.getElementById("map"), {
                // Nous plaçons le centre de la carte avec les coordonnées ci-dessus
                center: new google.maps.LatLng(lat, lon), 
                // Nous définissons le zoom par défaut
                zoom: 14, 
                // Nous définissons le type de carte (ici carte routière)
                mapTypeId: google.maps.MapTypeId.ROADMAP, 
                // Nous activons les options de contrôle de la carte (plan, satellite...)
                mapTypeControl: true,
                // Nous désactivons la roulette de souris
                scrollwheel: false, 
                mapTypeControlOptions: {
                    // Cette option sert à définir comment les options se placent
                    style: google.maps.MapTypeControlStyle.HORIZONTAL_BAR 
                },
                // Activation des options de navigation dans la carte (zoom...)
                navigationControl: true, 
                navigationControlOptions: {
                    // Comment ces options doivent-elles s'afficher
                    style: google.maps.NavigationControlStyle.ZOOM_PAN 
                }
            });


            var x = document.getElementById("demo");
            if(navigator.geolocation) {
                    navigator.geolocation.getCurrentPosition(function(position) {
                    var pos = new google.maps.LatLng(position.coords.latitude,
                                                   position.coords.longitude);
                    x.innerHTML = "Latitude: " + position.coords.latitude + 
        "<br>Longitude: " + position.coords.longitude;

        //*************************** ON THIS IS MY QUESTION **********************************    
                    var UserLocation = [position.coords.latitude,position.coords.longitude];

                    var marker = new google.maps.Marker({
                    position: pos,
                    map: map,
                    title: 'Here you are',
                    draggable: true
                    });
                });
            }
            ][1]
Vikrant
  • 4,920
  • 17
  • 48
  • 72
  • Possible duplicate of [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – Patrick Hund Dec 17 '18 at 10:59
  • Try to console the source and destination values in your case `userLocation` and `position.coords.latitude,position.coords.longitude` – Prashant Pimpale Dec 17 '18 at 10:59
  • Looks like you are trying to use a variable that is set asynchronously, see the duplicate link above on how to handle this – Patrick Hund Dec 17 '18 at 11:00
  • Maybe take a look at this : [javascript variables scope](https://stackoverflow.com/a/500459/5847906) – Ayak973 Dec 17 '18 at 11:01
  • Hello patrick i have console the values of position.coords.latitude and position.coords.longitude. it works perfectly .and the value appears :-4.4698382 ,48.4195213 . – Georges Tanios Dec 17 '18 at 12:32
  • but the problem is that i can't put these values in a variable ti use it after in my code ? – Georges Tanios Dec 17 '18 at 12:33

0 Answers0