html5指南-7.geolocation结合google maps开发一个小的应用

所属分类: 网页制作 / html5 阅读数: 1971
收藏 0 赞 0 分享
今天我们将把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的标签的代码的简单介绍 HTML5标签的简介

本文主要介绍了HTML5的标签.不同于以前的标签.更简单.更方便
收藏 0 赞 0 分享

关于HTML5你必须知道的28个新特性,新技巧以及新技术

HTML5有很多的新功能.新代码.非常不错.现在总结一下.仅供参考
收藏 0 赞 0 分享

只要五步 就可以用HTML5/CSS3快速制作便签贴特效(图)

用CSS,HTML5打造一个漂亮的标签效果
收藏 0 赞 0 分享

网易微博Web App用HTML5开发的过程介绍

本文介绍了网易微博用HTML5开发的全过程
收藏 0 赞 0 分享

HTML5 对各个标签的定义与规定:body的介绍

本文主要介绍body标签
收藏 0 赞 0 分享

关于HTML5的安全问题开发人员需要牢记的

HTML5中的安全问题也要注意的
收藏 0 赞 0 分享

关于HTML5的22个初级技巧(图文教程)

HTML5来了.让我们看一下有什么技巧
收藏 0 赞 0 分享

开发人员所需要知道的HTML5性能分析面面观

以下这篇文章是由一位名为张黎明的IT技术人员所写,其发表于InfoQ的网页上。这次他在全文里面从9个不同的方面分析HTML5的性能,还是很值得相应的开发人员阅读的。
收藏 0 赞 0 分享

HTML5安全介绍之内容安全策略(CSP)简介

前言:HTML5出现后,网络安全更加受到广泛的关注。Web对于网络安全有哪些改进?我们如何来面对越来越危险的网络欺诈和攻击?下面的文章谈到了W3C对于这个问题的最新解决方案。未来有机会的话,我会针对XSS、P3P、同源策略、CORS(跨域资源共享)和CSP进行关于HTML5内容
收藏 0 赞 0 分享

input元素的url类型和email类型简介

在过去我们制作网页输入框,会用到不少JS验证,如今有了HTML5写这种效果已经没有那么麻烦了,下面我来给大家介绍两种HTML5的input的新增加的类型应用。
收藏 0 赞 0 分享
查看更多