Javascript 未捕获引用错误:未定义getLocation

Javascript 未捕获引用错误:未定义getLocation,javascript,google-maps-api-3,Javascript,Google Maps Api 3,我正在一个网站上工作,现在遇到了一个错误。我想在加载页面时获取当前的纬度和经度。但加载页面时,包含该代码的函数未运行,并且显示错误“UncaughtReferenceError:getLocation未定义” 这是我的HTML部分 <!DOCTYPE html> <html> <head> <meta name="viewport" content="initial-scale=1.0, user-scalable=no"> <meta c

我正在一个网站上工作,现在遇到了一个错误。我想在加载页面时获取当前的纬度和经度。但加载页面时,包含该代码的函数未运行,并且显示错误“UncaughtReferenceError:getLocation未定义”

这是我的HTML部分

<!DOCTYPE html>
<html>
 <head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no">
<meta charset="utf-8">   
<script async defer
src="https://maps.googleapis.com/maps/api/js?key=(mykey)&callback=initialize">
</script>
<style>
  #map {
    height: 100%;
    width: 100%;    
</style>
</head>
 <body>
 <div id="map"><img src="adminpanel/images/loader.gif" width="3%" height="5%"/>&nbsp;<b>Loading Map</b></div>  
 </body>
</html>  

#地图{
身高:100%;
宽度:100%;
装载图
这是java脚本

<script type="text/javascript">
  getLocation();
  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) {
     window.location.href="search-product.php?Lat=" + position.coords.latitude + "&Lng=" + position.coords.longitude;
   }

var map;
var Markers = {};
var infowindow;
var locations = [
<?php for($i=0;$i<sizeof($locations);$i++) { $j=$i +1; ?>
[
  'hi ',
  '<a href="map_location_check.php?shop_id=<?php echo $locations[$i]['shop_id']; ?>" title="Click Here"><?php echo $locations[$i]['shop_name'].'<br>'.$locations[$i]['shop_address']; ?></a>',
  <?php echo $locations[$i]['map_latitude']; ?>,
  <?php echo $locations[$i]['map_longitude']; ?>,
  0
] <?php if($j!=sizeof($locations)) echo ",";  } ?>
];

var origin = new google.maps.LatLng(locations[0][2],locations[0][3]);
function initialize () {
 var mapOptions = {
  zoom:12,
  center:origin
 };
 map = new google.maps.Map(document.getElementById('map'),mapOptions);
 infowindow = new google.maps.InfoWindow();
 for(i=0;i<locations.length;i++) {
  var position = new google.maps.LatLng(locations[i][2], locations[i][3]);
  var marker = new google.maps.Marker({
    position:position,
    map:map,
  });
  google.maps.event.addListener(marker, 'click', (function(marker, i){
  return function() {
    infowindow.setContent(locations[i][1]);
    infowindow.setOptions({maxWidth: 200});
    infowindow.open(map, marker);
  }
}) (marker, i));
Markers[locations[i][4]] = marker;
}
locate(0);
}

function locate(marker_id){
 var myMarker = Markers[marker_id];
 var markerPosition = myMarker.getPosition();
 map.setCenter(markerPosition);
 google.maps.event.trigger(myMarker, 'click');
} 
</script>

getLocation();
var x=document.getElementById(“演示”);
函数getLocation(){
if(导航器.地理位置){
navigator.geolocation.getCurrentPosition(showPosition);
}否则{
x、 innerHTML=“此浏览器不支持地理位置。”;
}
}
功能显示位置(位置){
window.location.href=“search product.php?Lat=“+position.coords.lation+”&Lng=“+position.coords.longitude;
}
var映射;
var标记={};
var信息窗口;
变量位置=[
[
“嗨”,
'',
,
,
0
] 
];
var origin=new google.maps.LatLng(位置[0][2],位置[0][3]);
函数初始化(){
变量映射选项={
缩放:12,
中心:原点
};
map=new google.maps.map(document.getElementById('map'),mapOptions);
infowindow=new google.maps.infowindow();

对于(i=0;i在定义它或将它分配给要提升的变量后调用
getLocation
var getLocation=function(){…}
这可以正常工作。这意味着您在其他地方对函数进行了另一次调用,并且文件没有加载,在这种情况下,您需要等待文件加载

window.onload = function(){getLocation()}
jQuery(window).on('load', getLocation);

另外,在这里的window.location.href上

  function showPosition(position) {
     window.location.href="search-product.php?Lat=" + position.coords.latitude + "&Lng=" + position.coords.longitude;
   }
你可能会想要

  function showPosition(position) {
    return window.location.href+"search-product.php?Lat=" + position.coords.latitude + "&Lng=" + position.coords.longitude;
   }
编辑2:更正您需要在加载时创建实例

window.onload = function(){var origin = new google.maps.LatLng(locations[0][2],locations[0][3]);};

谢谢..但现在它显示另一个错误为“未捕获引用错误:google未定义”