本文介紹了使用 google maps API,我們如何使用 map.setCenter 函數將當前位置設置為默認設置位置?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
我正在使用 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);
以上代碼將地圖畫布的默認位置設置為帕洛阿爾托.
The above code sets the default location of the map canvas to Palo Alto.
如何編寫腳本,讓setCenter函數自動指向客戶端的當前位置?
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);
}
這篇關于使用 google maps API,我們如何使用 map.setCenter 函數將當前位置設置為默認設置位置?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!