欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

html5指南-7.geolocation結(jié)合google maps開(kāi)發(fā)一個(gè)小的應(yīng)用

  發(fā)布時(shí)間:2013-01-07 14:55:06   作者:佚名   我要評(píng)論
今天我們將把html5的geolocation結(jié)合google maps開(kāi)發(fā)一個(gè)小的應(yīng)用,感興趣的朋友可以了解下,如有不足,愿大俠給予指教

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

復(fù)制代碼
代碼如下:

<body>
<div id="map">
</div>
</body>

js代碼的框架如下

復(fù)制代碼
代碼如下:

<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方法就是調(diào)用google maps api初始化地圖,他需要設(shè)置options對(duì)象,在調(diào)用地圖初始化的時(shí)候使用。

復(fù)制代碼
代碼如下:

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方法獲取定位信息。

復(fù)制代碼
代碼如下:

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();
}
}

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

復(fù)制代碼
代碼如下:

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

相關(guān)文章

  • 使用HTML5 Geolocation實(shí)現(xiàn)一個(gè)距離追蹤器

    HTML5 Geolocation(地理定位)用于定位用戶(hù)的位置,那么怎么實(shí)現(xiàn)一個(gè)距離追蹤器呢?下面小編給大家?guī)?lái)了基于HTML5 Geolocation實(shí)現(xiàn)一個(gè)距離追蹤器的實(shí)例代碼,感興趣的朋
    2018-04-09
  • Html5 Geolocation獲取地理位置信息實(shí)例

    這篇文章主要介紹了Html5 Geolocation獲取地理位置信息實(shí)例,具有一定的參考價(jià)值,有興趣的同學(xué)可以了解一下。
    2016-12-09
  • HTML5的Geolocation地理位置定位API使用教程

    地理位置(Geolocation)是 HTML5 的重要特性之一,提供了確定用戶(hù)位置的功能,借助這個(gè)特性能夠開(kāi)發(fā)基于位置信息的應(yīng)用,今天這篇文章就向大家介紹一下HTML5的Geolocation地理
    2016-05-12
  • html5指南-4.使用Geolocation實(shí)現(xiàn)定位功能

    今天我們要學(xué)習(xí)的是使用Geolocation實(shí)現(xiàn)定位功能。我們可以通過(guò)navigator.geolocation獲取Geolocation對(duì)象,感興趣的朋友可以了解下
    2013-01-07
  • HTML5 Geolocation API的正確使用方法

    Geolocation是HTML5標(biāo)準(zhǔn)下的一個(gè)Web API,利用它可以獲取設(shè)備的當(dāng)前位置信息(坐標(biāo)),本篇文章主要介紹了三個(gè)方法,非常具有實(shí)用價(jià)值,需要的朋友可以參考下
    2018-12-04

最新評(píng)論