我正在网站上工作,现在遇到了错误。我希望在加载页面时获取当前的纬度和经度。但是当加载页面时,该代码的函数没有运行,并且显示错误" Uncaught ReferenceError:未定义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%"/> <b>Loading Map</b></div>
</body>
</html>
这是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>
感谢您的帮助..!
答案 0 :(得分:0)
在您定义后调用getLocation
或将其分配给要提升的变量var getLocation = function(){...}
答案 1 :(得分:0)
这很好用。这意味着您可以在其他地方再次调用该函数,并且文件未加载,在这种情况下,您需要等到文件加载
patient_reaction_reactionmeddrapt = data['patient']['reaction'].get('reactionmeddrapt')
AttributeError: 'list' object has no attribute 'get'
请参阅https://codepen.io/anon/pen/GdNLVL
此外,在这里的window.location.href。
window.onload = function(){getLocation()}
jQuery(window).on('load', getLocation);
Window是一个可免疫的对象。你可能想要。
function showPosition(position) {
window.location.href="search-product.php?Lat=" + position.coords.latitude + "&Lng=" + position.coords.longitude;
}
编辑2: 更正您需要创建实例onload
function showPosition(position) {
return window.location.href+"search-product.php?Lat=" + position.coords.latitude + "&Lng=" + position.coords.longitude;
}