本文介紹了使用 google maps API,我們?nèi)绾问褂?map.setCenter 函數(shù)將當(dāng)前位置設(shè)置為默認(rèn)設(shè)置位置?的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!
問(wèn)題描述
我正在使用 Google Maps API 編寫 JavaScript 代碼.
I am writing JavaScript code using Google Maps API.
map = new google.maps.Map2(document.getElementById("map_canvas"));
map.setCenter(new google.maps.LatLng(37.4419, -122.1419), 13);
以上代碼將地圖畫布的默認(rèn)位置設(shè)置為帕洛阿爾托.
The above code sets the default location of the map canvas to Palo Alto.
如何編寫腳本,讓setCenter函數(shù)自動(dòng)指向客戶端的當(dāng)前位置?
How can we write the script in such a way that the setCenter function automatically points to the current location of the client?
推薦答案
您可以在支持它的瀏覽器中使用 HTML5 GeoLocation API.
You can use the HTML5 GeoLocation API in browsers that support it.
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(success, error);
} else {
alert('geolocation not supported');
}
function success(position) {
alert(position.coords.latitude + ', ' + position.coords.longitude);
}
function error(msg) {
alert('error: ' + msg);
}
這篇關(guān)于使用 google maps API,我們?nèi)绾问褂?map.setCenter 函數(shù)將當(dāng)前位置設(shè)置為默認(rèn)設(shè)置位置?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!
【網(wǎng)站聲明】本站部分內(nèi)容來(lái)源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問(wèn)題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!