简体   繁体   中英

Geo Location code returning undefined

I am trying to run the following Geolocation code but the following code is returning undefined. can anyone guide me please

When I call the function for example

alert(verifylocation());

it returns undefined. I want to use this in form validation

 <script src="//maps.googleapis.com/maps/api/js?key=<?=$google_api_key?>&sensor=false" type="text/javascript"></script> <script language="javascript"> var geocoder, location1, location2, radius_verified; geocoder = new google.maps.Geocoder(); radius_verified = - 1; var Zone1, Zone1_enabled; var Zone2, Zone2_enabled; var Zone3, Zone3_enabled; window.verifylocation = function() { var restaurant_location = '<?= $objRestaurant->rest_address . ", " . $objRestaurant->rest_city . ", " . $objRestaurant->rest_state; ?>'; var customer_location = $("#customer_address").val() + " , " + $("#customer_city").val() + " , " + $("#customer_state").val(); var radius = '<?= $objRestaurant->delivery_radius ?>'; geocoder.geocode({'address': restaurant_location}, function(results, status) { // Here is my Code but before my code run it returns undefined. }); }; 

You can use something like this :

            <!DOCTYPE html>
            <html>
            <body>

            <p>Click the button to get your coordinates.</p>

            <button onclick="getLocation()">Try It</button>

            <p id="demo"></p>

            <script>
            var x = document.getElementById("demo");

            function getLocation() {
                if (navigator.geolocation) {
                    navigator.geolocation.getCurrentPosition(showPosition);
                } else { 
                    x.innerHTML = "Geolocation is not supported by this browser.";
                }
            }

            function showPosition(position) {
                x.innerHTML = "Latitude: " + position.coords.latitude + 
                "<br>Longitude: " + position.coords.longitude;  
            }
            </script>

            </body>
            </html>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM