国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > HTML > 正文

html5指南-7.geolocation結合google maps開發一個小的應用

2024-08-26 00:16:10
字體:
來源:轉載
供稿:網友

點評:今天我們將把html5的geolocation結合google maps開發一個小的應用,感興趣的朋友可以了解下,如有不足,愿大俠給予指教

今天我們將把html5的geolocation結合google maps開發一個小的應用。google maps的api地址:https://developers.google.com/maps/documentation/javascript/?hl=zh-CN。
調用google maps,實現需要添加js引用<script type="text/javascript" src="?sensor=false"></script>,其中sensor參數的具體含義:
要使用 Google Maps API,您需要指明自己的應用程序在任何 Maps API 庫或服務請求中是否是使用傳感器(如 GPS 定位器)來確定用戶所處位置的。這對移動設備尤為重要。如果您的 Google Maps API 應用程序使用任何形式的傳感器確定訪問您的應用程序的設備的位置,那么您必須通過將 sensor 參數值設置為 true 以聲明這一點。
html部分比較簡單,只需要準備一個div就可:

復制代碼

代碼如下:


<body>
<div>
</div>
</body>


js代碼的框架如下:

復制代碼

代碼如下:


<script type="text/javascript">
var map;
var browserSupport = false;
var attempts = 0;
$(document).ready(function () {
//初始化地圖
    InitMap();
//定位
getLocation();
    //定位跟蹤
watchLocation();
});
function InitMap() {
/* Set all of the options for the map */
var options = {
};
/* Create a new Map for the application */
map = new google.maps.Map($('#map')[0], options);
}
/*
* If the W3C Geolocation object is available then get the current
* location, otherwise report the problem
*/
function getLocation() {
}
function watchLocation() {
}
/* Plot the location on the map and zoom to it */
function plotLocation(position) {
}
/* Report any errors using this function */
function reportProblem(e) {
}
</script>


InitMap方法就是調用google maps api初始化地圖,他需要設置options對象,在調用地圖初始化的時候使用。

復制代碼

代碼如下:


function InitMap() {
/* Set all of the options for the map */
var options = {
zoom: 4,
center: new google.maps.LatLng(38.6201, -90.2003),
mapTypeId: google.maps.MapTypeId.ROADMAP,
mapTypeControl: true,
mapTypeControlOptions: {
style: google.maps.MapTypeControlStyle.HORIZONTAL_BAR,
position: google.maps.ControlPosition.BOTTOM_CENTER
},
panControl: true,
panControlOptions: {
position: google.maps.ControlPosition.TOP_RIGHT
},
zoomControl: true,
zoomControlOptions: {
style: google.maps.ZoomControlStyle.LARGE,
position: google.maps.ControlPosition.LEFT_CENTER
},
scaleControl: true,
scaleControlOptions: {
position: google.maps.ControlPosition.BOTTOM_LEFT
},
streetViewControl: true,
streetViewControlOptions: {
position: google.maps.ControlPosition.LEFT_TOP
}
};
/* Create a new Map for the application */
map = new google.maps.Map($('#map')[0], options);
}


getLocation和watchLocation方法獲取定位信息。

復制代碼

代碼如下:


function getLocation() {
/* Check if the browser supports the W3C Geolocation API */
if (navigator.geolocation) {
browserSupport = true;
navigator.geolocation.getCurrentPosition(plotLocation, reportProblem, { timeout: 45000 });
} else {
reportProblem();
}
}
function watchLocation() {
/* Check if the browser supports the W3C Geolocation API */
if (navigator.geolocation) {
browserSupport = true;
navigator.geolocation.watchPosition(plotLocation, reportProblem, { timeout: 45000 });
} else {
reportProblem();
}
}


成功獲取位置信息后,調用plotLocation方法把位置顯示在google maps上。

復制代碼

代碼如下:


function plotLocation(position) {
attempts = 0;
var point = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
var marker = new google.maps.Marker({
position: point
});
marker.setMap(map);
map.setCenter(point);
map.setZoom(15);
}


demo下載地址:googleMapGeolocation.rar

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 新民市| 嫩江县| 文化| 凤翔县| 宜良县| 资源县| 中阳县| 和田市| 南澳县| 嵩明县| 临颍县| 临邑县| 大庆市| 大名县| 石城县| 普宁市| 郑州市| 石渠县| 龙南县| 岱山县| 通州区| 蓬溪县| 遂昌县| 阿坝| 大兴区| 澳门| 宜昌市| 鸡泽县| 禄丰县| 若羌县| 清流县| 锦屏县| 德昌县| 肥东县| 泸溪县| 剑阁县| 比如县| 水城县| 盐源县| 平乡县| 三台县|