欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2023-11-27 10:23:22
今天我们将把html5的geolocation结合google maps开发一个小的应用,感兴趣的朋友可以了解下,如有不足,愿大侠给予指教... 13-01-07...
今天我们将把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