我試圖讓我的中心位置根據點擊按鈕在我的谷歌地圖上進行更改。我稍後添加了更多功能,但我無法獲得此基本更新的工作。我有一個在頁面加載時加載的默認值。該按鈕應該設置一個新的位置,但我發現它會更新,但會很快被默認值覆蓋,因爲頁面會再次加載。爲什麼我不能讓我的谷歌地圖更新按鈕點擊緯度/經度?
我是新手谷歌地圖,我只是想弄清楚如何加載和正確地與地圖交互。
這裏是我的代碼:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script>
var deanLat = 43.0843816;
var deanLong = -89.5302394;
var deanAddress = '1277 deming way, madison wi';
var stmaryLat = 43.0592730;
var stmaryLong = -89.4015130;
var stmaryAddress = '700 S Park St, Madison, WI 53715';
$("#FindDean").click(function() {
refreshMap(deanLat, deanLong);
});
function alertme(message) {
alert (message);
}
function refreshMap(ilat,ilong) {
ilat = typeof ilat !== 'undefined' ? ilat : stmaryLat;
ilong = typeof ilong !== 'undefined' ? ilong : stmaryLong;
alertme(ilat);
var mapOptions = {
zoom: 16,
center: new google.maps.LatLng(ilat, ilong)
};
gmap = new google.maps.Map(document.getElementById('mapholder'),
mapOptions);
}
function loadScript() {
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = 'https://maps.googleapis.com/maps/api/js?v=3.exp' +
'&signed_in=true&callback=refreshMap';
document.body.appendChild(script);
}
window.onload = loadScript;
怎麼樣:http://stackoverflow.com/questions/7905733/google-maps-api -3-get-coordinates-from-right-click –