摘要:使用Google maps api3 定位
重要參考 :
首先練習定位,請看Code,下次再補充標記與取消標記
一開始記得先引用api
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
<meta name="viewport" content="width=device-width, initial-scale=1">
function ShowGoogleMap() {
//判斷瀏覽器可否使用google api的geoloaction 抓取使用者當前位址
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition);
}
else { x.innerHTML = "Geolocation is not supported by this browser."; }
function showPosition(position) {
var latlng = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
//設定地圖選項
var mapOptions = {
center: latlng,
//設定顯示範圍大小,數字越小,範圍越大
zoom: 13,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
//Map(HTML位置,將設定好位置加入)
map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);
var marker = new google.maps.Marker({
position: latlng,
map: map,
title: 'here you are !'
});
}
}
//加入監聽,在loadong時,執行showgooglemap()
google.maps.event.addDomListener(window, 'load', ShowGoogleMap);