简体   繁体   English

地理位置根本不起作用,找不到解决方案,不要认为我的代码有什么问题

[英]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.

相关问题 当我似乎找不到任何错误时,此代码为什么不起作用? - How come this code isn't working when I can't seem to find anything wrong? 我找不到关于我的代码的任何错误,但脚本仍然不起作用 - I can't find anything wrong regarding my code, but the script still doesn't work jQuery function 不动……找不到什么问题? - jQuery function not moving… can't find anything wrong with it? 函数作为React子代无效。 我在代码中找不到导致此错误的任何内容 - Functions are not valid as a React child. I can't find anything in my code that cause this error 找不到这段代码有什么问题 - Can't find what's wrong in this code discord.js 我的代码不起作用,在终端显示 typeError 但我不知道出了什么问题 - discord.js my code isn't working, in the terminal it says typeError but I don't know what went wrong 我没有弄明白我的JavaScript代码有什么问题 - I don't get what's wrong with my Javascript code 不了解我的代码FreeCodeCamp(NodeJS)有什么问题 - Don't Understand what's wrong with my Code, FreeCodeCamp (NodeJS) 我必须制作一个可以运行一些简单JavaScript的网页,但是我认为我的脚本没有运行 - I have to make a webpage that can run some simple JavaScript, but I don't think my script is running 不明白为什么我的 for 循环代码不起作用 - Don't understand why my for loop code isn't working
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM