2015-11-02 85 views
0

我想运行以下地理位置代码,但下面的代码返回undefined。任何人都可以指导我请地理位置代码返回undefined

当我打电话例如

警报(verifylocation())的功能;

它返回未定义。我想用这个表单验证

<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() { 
 
\t var restaurant_location = '<?= $objRestaurant->rest_address . ", " . $objRestaurant->rest_city . ", " . $objRestaurant->rest_state; ?>'; 
 
\t var customer_location = $("#customer_address").val() + " , " + $("#customer_city").val() + " , " + $("#customer_state").val(); 
 
\t 
 
    var radius = '<?= $objRestaurant->delivery_radius ?>'; 
 
\t 
 
    geocoder.geocode({'address': restaurant_location}, function(results, status) { 
 
\t \t // Here is my Code but before my code run it returns undefined. 
 
    }); 
 
};

+0

什么变量未定义? – Griffith

+0

当我调用函数例如alert(verifylocation()) –

+0

你没有返回函数中的任何值。你想要返回什么? – Griffith

回答

0

您可以使用这样的事情:

  <!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>