您现在的位置是:网站首页> 编程资料编程资料
html5指南-7.geolocation结合google maps开发一个小的应用使用HTML5 Geolocation实现一个距离追踪器Html5 Geolocation获取地理位置信息实例HTML5的Geolocation地理位置定位API使用教程html5指南-4.使用Geolocation实现定位功能HTML5 Geolocation API的正确使用方法
2023-10-15
362人已围观
简介 今天我们将把html5的geolocation结合google maps开发一个小的应用,感兴趣的朋友可以了解下,如有不足,愿大侠给予指教
今天我们将把html5的geolocation结合google maps开发一个小的应用。google maps的api地址:https://developers.google.com/maps/documentation/javascript/?hl=zh-CN。
调用google maps,实现需要添加js引用,其中sensor参数的具体含义:
要使用 Google Maps API,您需要指明自己的应用程序在任何 Maps API 库或服务请求中是否是使用传感器(如 GPS 定位器)来确定用户所处位置的。这对移动设备尤为重要。如果您的 Google Maps API 应用程序使用任何形式的传感器确定访问您的应用程序的设备的位置,那么您必须通过将 sensor 参数值设置为 true 以声明这一点。
html部分比较简单,只需要准备一个div就可:
js代码的框架如下:
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
调用google maps,实现需要添加js引用,其中sensor参数的具体含义:
要使用 Google Maps API,您需要指明自己的应用程序在任何 Maps API 库或服务请求中是否是使用传感器(如 GPS 定位器)来确定用户所处位置的。这对移动设备尤为重要。如果您的 Google Maps API 应用程序使用任何形式的传感器确定访问您的应用程序的设备的位置,那么您必须通过将 sensor 参数值设置为 true 以声明这一点。
html部分比较简单,只需要准备一个div就可:
复制代码
代码如下:js代码的框架如下:
复制代码
代码如下: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的Canvas上绘制椭圆的几种方法总结html5-Canvas可以在web中绘制各种图形HTML5 Canvas绘制超级漂亮的发光Loading动画HTML5在canvas中绘制复杂形状附效果截图HTML5 canvas绘制的玫瑰花效果html5使用canvas绘制一张图片html5使用canvas绘制太阳系效果
- html5构建触屏网站之touch事件介绍html5 touch事件实现触屏页面上下滑动(二)html5 touch事件实现触屏页面上下滑动(一)html5 touch事件实现页面上下滑动效果【附代码】
- 我叫mt 卓越游戏官方微信兑换功能上线公告通知_手机游戏_游戏攻略_
- 我叫MT3.2神庙外围上层2号boss伤害属性测试分析_手机游戏_游戏攻略_
- 我叫mt online3.2 五元党打造百级伯爵之路_手机游戏_游戏攻略_
- 我叫mt online喜迎圣诞 六大给力活动火热开启_手机游戏_游戏攻略_
- 割绳子2 城市公园 第1关 三星图文攻略_手机游戏_游戏攻略_
- 天天飞车12月23日烧饼修改器刷分教程攻略 最新刷分不封号心得_手机游戏_游戏攻略_
- 天天飞车郭采洁专属A车免费获得方法 郭采洁专属A车怎么获得_手机游戏_游戏攻略_
- 天天飞车赛车改装顺序技巧 天天飞车赛车改装攻略心得_手机游戏_游戏攻略_
