[英]Geolocation not working at all, can't find a solution don't think there is anything wrong with my code
I'm trying to use Geolocation on Google Maps, but when the page loads, nothing happens I have googled many different examples and many different things but I cannot seem to find an answer, I really don't see anything wrong with my code. 我正在尝试在Google Maps上使用Geolocation,但是当页面加载时,什么也没发生,我已经在Google上搜索了许多不同的示例和许多不同的东西,但是我似乎找不到答案,我的代码确实看不到任何错误。
<script>
var initialLocation;
var map;
var markersArray = [];
function initialize() {
var mapOptions = {
zoom: 15,
center: new google.maps.LatLng(-34.397, 150.644),
mapTypeId: google.maps.MapTypeId.ROADMAP,
disableDefaultUI: false,
zoomControl: true,
zoomControlOptions: {
style: google.maps.ZoomControlStyle.LARGE
}
};
map = new google.maps.Map(document.getElementById('gmaps'),
mapOptions);
google.maps.event.addListener(map, 'click', function(event) {
var latitude = event.latLng.lat();
var longitude = event.latLng.lng();
deleteOverlays();
addMarker(event.latLng);
updateTextFields(latitude, longitude);
});
google.maps.event.addListener(marker, 'click', function(event) {
showInfo(window);
});
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(success);
alert('Browser Supports but Im not doing anything yet');
} else {
error('Geo Location is not supported');
}
}
function updateTextFields(lati, longi) {
$('#inputLatitude').val(lati);
$('#inputLongitude').val(longi);
}
function addMarker(location) {
marker = new google.maps.Marker({
position: location,
map: map
});
markersArray.push(marker);
}
function deleteOverlays() {
if (markersArray) {
for (i in markersArray) {
markersArray[i].setMap(null);
}
markersArray.length = 0;
}
}
function loadScript() {
var script = document.createElement('script');
script.src = 'https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=true&' +
'callback=initialize';
document.body.appendChild(script);
}
$(document).ready(function() {
$(window).resize(function () {
var h = $(window).height(),
offsetTop = 190; // Calculate the top offset
$('#gmaps').css('height', (h - offsetTop));
}).resize();
window.onload = loadScript;
});
</script>
You're assigning a function to window.onload
after the window has loaded; 您要在窗口加载后为
window.onload
分配一个函数。 thus it won't be called. 因此它不会被调用。
The following would be better: 以下将更好:
$(document).ready(function() {
$(window).resize(function () {
var h = $(window).height(),
offsetTop = 190; // Calculate the top offset
$('#gmaps').css('height', (h - offsetTop));
}).resize();
loadScript();
});
In addition: 此外:
navigator.geolocation.getCurrentPosition(success);
requires a function called success
that takes the geolocation position as an input parameter. 需要一个名为
success
的函数,该函数将地理位置位置作为输入参数。 I don't see such a function in your code. 我在您的代码中看不到这样的功能。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.