婷婷综合国产,91蜜桃婷婷狠狠久久综合9色 ,九九九九九精品,国产综合av

主頁 > 知識庫 > html5指南-7.geolocation結合google maps開發一個小的應用

html5指南-7.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="http://maps.google.com/maps/api/js?sensor=false"></script>,其中sensor參數的具體含義:
要使用 Google Maps API,您需要指明自己的應用程序在任何 Maps API 庫或服務請求中是否是使用傳感器(如 GPS 定位器)來確定用戶所處位置的。這對移動設備尤為重要。如果您的 Google Maps API 應用程序使用任何形式的傳感器確定訪問您的應用程序的設備的位置,那么您必須通過將 sensor 參數值設置為 true 以聲明這一點。
html部分比較簡單,只需要準備一個div就可

復制代碼
代碼如下:

<body>
<div id="map">
</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

標簽:廣西 蚌埠 松原 阿克蘇 果洛 廣東 常德 鄂爾多斯

巨人網絡通訊聲明:本文標題《html5指南-7.geolocation結合google maps開發一個小的應用》,本文關鍵詞  html5,指南,-7.geolocation,結合,;如發現本文內容存在版權問題,煩請提供相關信息告之我們,我們將及時溝通與處理。本站內容系統采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《html5指南-7.geolocation結合google maps開發一個小的應用》相關的同類信息!
  • 本頁收集關于html5指南-7.geolocation結合google maps開發一個小的應用的相關信息資訊供網民參考!
  • 推薦文章
    主站蜘蛛池模板: 曲阜市| 兰西县| 喜德县| 韶山市| 抚宁县| 夏邑县| 汝阳县| 青阳县| 封丘县| 宁津县| 如东县| 涡阳县| 化隆| 新昌县| 大冶市| 蚌埠市| 三台县| 兴仁县| 六枝特区| 方正县| 那坡县| 上犹县| 大余县| 衡阳市| 隆尧县| 鹤岗市| 喀喇沁旗| 德江县| 嘉黎县| 黎平县| 利川市| 双桥区| 荆州市| 金山区| 横山县| 枣阳市| 三门县| 延安市| 晋中市| 金秀| 南江县|