帝王谷资源网 Design By www.wdxyy.com
今天我们将把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
调用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
标签:
geolocation,maps
帝王谷资源网 Design By www.wdxyy.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
帝王谷资源网 Design By www.wdxyy.com
暂无评论...
更新日志
2024年07月03日
2024年07月03日
- 华语群星《宝丽金超白金精选特辑Ⅰ 2CD》[WAV+CUE][980MB]
- 周传雄.1994-发觉【歌林】【WAV+CUE】
- 群星.2015-Celebration.45th.Anniversary环球志101系列6CD【环球】【WAV+CUE】
- 谭咏麟.1997-永恒的珍(新歌+精选)【宝丽金】【WAV+CUE】
- 原画壁纸及美图第262期,无水印可自取
- 原画壁纸及美图第263期,无水印可自取
- 原画壁纸及美图第264期,无水印可自取
- 绝区零感电鸣徽有哪些 绝区零感电鸣徽效果介绍
- 绝区零冻结鸣徽有哪些 绝区零冻结鸣徽效果介绍
- 绝区零丽娜角色精美壁纸分享 丽娜高清图片大全
- 《命运2:终焉之形》评测:在线人数几回峰值的史诗新篇
- 《艾尔登法环:黄金树幽影》评测:你所拥有的力量,正是你为王的理由
- 【游新视界】《幻兽帕鲁》帕桑!岛上滴樱花开了,不回来看看吗?
- 群星1996-《96滚石年度最卖座主打歌刷新全纪录·伤心咖啡馆之歌》台湾首版[WAV+CUE]
- 群星.1997-滚石年度最卖座专辑主打歌全纪录·乱世佳人2【滚石】【WAV+CUE】