我是 Ruby on Rails 的实习生,昨天我不得不用 Javascript 做一些事情(我的 javascript 技能太棒了,我什至没有这方面的技能)。我在一个项目中实现了当前位置功能,但我想用另一种方式来做...... thig 已经完成了,看看:function geolocationSuccess(position) { var latitude = position.coords.latitude; var longitude = position.coords.longitude; var geocoder = new google.maps.Geocoder(); var latlng = {lat: latitude, lng: longitude}; geocoder.geocode({'location': latlng}, function(results, status) { if (status === google.maps.GeocoderStatus.OK) { if (results[0]){ var user_address = results[0].formatted_address; document.getElementById("current_location").innerHTML = user_address; }else { console.log('No results found for these coords.'); } }else { console.log('Geocoder failed due to: ' + status); } });}function geolocationError() { console.log("please enable location for this feature to work!");}$(document).on("ready page:ready", function() { $("#current-location").on("click", function(event) { event.preventDefault(); if ("geolocation" in navigator) { navigator.geolocation.getCurrentPosition(geolocationSuccess, geolocationError); } else { alert("Geolocation not supported!"); } });});好吧,我知道当我单击带有 Id="current-location" 的按钮时会发生这一切,但是我希望它在页面加载时自动发生,我该怎么做?
添加回答
举报
0/150
提交
取消