Geolocation 用于刷新用户的地理位置脚本';s位置
我有下面的地理位置脚本来查找用户的位置。如果用户移动了位置,我将如何在单击刷新用户位置时合并一个按钮?换言之,我正在寻找一个html按钮,它在单击时刷新用户的位置Geolocation 用于刷新用户的地理位置脚本';s位置,geolocation,Geolocation,我有下面的地理位置脚本来查找用户的位置。如果用户移动了位置,我将如何在单击刷新用户位置时合并一个按钮?换言之,我正在寻找一个html按钮,它在单击时刷新用户的位置 // Find user's location start jQuery(window).ready(function () { geo_latitude = jQuery.cookie('geo_latitude');
// Find user's location start
jQuery(window).ready(function () {
geo_latitude = jQuery.cookie('geo_latitude');
geo_longitude = jQuery.cookie('geo_longitude');
jQuery.cookie('the_cookie', 'the_value', { expires: 7 });
if(null!=geo_latitude && null!=geo_longitude){
var position = {
coords : { latitude : geo_latitude, longitude : geo_longitude }
};
handle_geolocation_query(position);
}else{
initiate_geolocation();
}
})
function initiate_geolocation() {
console.log('initiate_geolocation');
if(navigator.geolocation) {
navigator.geolocation.getCurrentPosition(handle_geolocation_query, handle_errors);
} else {
yqlgeo.get('visitor', normalize_yql_response);
}
}
function handle_errors(error) {
switch(error.code) {
case error.PERMISSION_DENIED:
alert("User did not share geolocation data");
break;
case error.POSITION_UNAVAILABLE:
alert("Could not detect current position");
break;
case error.TIMEOUT:
alert("Retrieving position timed out");
break;
default:
alert("Unknown Error");
break;
}
}
function normalize_yql_response(response) {
if(response.error) {
var error = {
code : 0
};
handle_error(error);
return;
}
var position = {
coords : {
latitude : response.place.centroid.latitude,
longitude : response.place.centroid.longitude
},
address : {
city : response.place.locality2.content,
region : response.place.admin1.content,
country : response.place.country.content
}
};
handle_geolocation_query(position);
}
function handle_geolocation_query(position) {
jQuery.cookie('geo_latitude', position.coords.latitude);
jQuery.cookie('geo_longitude', position.coords.longitude);
var image_url = "http://maps.google.com/maps/api/staticmap?sensor=false¢er=" + position.coords.latitude + "," + position.coords.longitude + "&zoom=12&size=400x116&style=feature:water|element:geometry|hue:0x336699|saturation:30|lightness:25&style=feature:road.highway|element:labels|visibility:off&style=feature:transit|element:all|visibility:off&markers=icon:http%3A%2F%2Fwww.jabbermap.com%2Fwp-content%2Fuploads%2Fimages%2Fmarker.png|" + position.coords.latitude + ',' + position.coords.longitude;
jQuery("#map").remove();
jQuery('#welcome_map').append(jQuery(document.createElement("img")).attr("src", image_url).attr('id', 'map'));
}
// Find user's location end
// Slider start: http://www.webchief.co.uk/blog/simple-jquery-slideshow/index.php
jQuery(document).ready(function () {
var currentPosition = 0;
var slideWidth = 630;
var slides = jQuery('.slide');
var numberOfSlides = slides.length;
var slideShowInterval;
var speed = 8000;
slideShowInterval = setInterval(changePosition, speed);
slides.wrapAll('<div id="slidesHolder"></div>')
slides.css({ 'float' : 'left' });
jQuery('#slidesHolder').css('width', slideWidth * numberOfSlides);
function changePosition() {
if(currentPosition == numberOfSlides - 1) {
currentPosition = 0;
} else {
currentPosition++;
}
moveSlide();
}
function moveSlide() {
jQuery('#slidesHolder').animate({'marginLeft' : slideWidth*(-currentPosition)});
}
});
// Slider end
//开始查找用户的位置
jQuery(窗口).ready(函数(){
geo_latitude=jQuery.cookie('geo_latitude');
geo_-longitude=jQuery.cookie('geo_-longitude');
cookie('the_cookie','the_value',{expires:7});
如果(空!=地理纬度和空!=地理经度){
变量位置={
坐标:{纬度:地理纬度,经度:地理经度}
};
处理地理位置查询(位置);
}否则{
启动地理定位();
}
})
函数initiate_geolocation(){
console.log('initiate_geolocation');
if(导航器.地理位置){
navigator.geolocation.getCurrentPosition(句柄\地理位置\查询,句柄\错误);
}否则{
get('visitor',规范化响应);
}
}
函数句柄错误(错误){
开关(错误代码){
案例错误。权限被拒绝:
警报(“用户未共享地理位置数据”);
打破
案例错误。位置不可用:
警报(“无法检测当前位置”);
打破
大小写错误。超时:
警报(“检索位置超时”);
打破
违约:
警报(“未知错误”);
打破
}
}
函数规格化_yql_响应(响应){
if(response.error){
变量错误={
代码:0
};
处理错误(错误);
回来
}
变量位置={
协调:{
纬度:response.place.centroid.latitude,
经度:response.place.centroid.longitude
},
地址:{
城市:response.place.locality2.content,
地区:response.place.admin1.content,
国家:response.place.country.content
}
};
处理地理位置查询(位置);
}
函数句柄\地理位置\查询(位置){
jQuery.cookie('geo_latitude',position.coords.latitude);
cookie('geo_longitude',position.coords.longitude);
var image_url=”http://maps.google.com/maps/api/staticmap?sensor=false¢er=“+position.coords.latitude+”,“+position.coords.longitude+“&zoom=12&size=400x116&style=feature:water | element:geometry |色调:0x336699 |饱和度:30 |亮度:25&style=feature:road.highway | element:labels | visibility:off&style=feature:transit | element:all | visibility:off&markers=图标:http%3A%2Fwww.jabbermap.com%2Fwp内容%2fWwUploads%2Fimages%2F|“+position.coords.latitude+”,“+position.coords.longitude;
jQuery(“#map”).remove();
jQuery('#welcome_map').append(jQuery(document.createElement(“img”)).attr(“src”,image_url).attr('id',map'));
}
//查找用户的位置结束
//滑块开始:http://www.webchief.co.uk/blog/simple-jquery-slideshow/index.php
jQuery(文档).ready(函数(){
var currentPosition=0;
var slideWidth=630;
var slides=jQuery('.slide');
var numberOfSlides=slides.length;
var幻灯片放映间隔;
var速度=8000;
slideShowInterval=设置间隔(改变位置、速度);
幻灯片。wrapAll(“”)
css({'float':'left'});
jQuery(“#slideshalder”).css('width',slideWidth*numberOfSlides);
函数changePosition(){
如果(currentPosition==numberOfSlides-1){
currentPosition=0;
}否则{
currentPosition++;
}
moveSlide();
}
函数moveSlide(){
jQuery(“#slideshalder”).animate({'marginLeft':slideWidth*(-currentPosition)});
}
});
//滑块端
这是代码
// Request a position. We only accept cached positions whose age is not
// greater than 10 minutes. If the user agent does not have a fresh
// enough cached position object, it will immediately invoke the error
// callback.
navigator.geolocation.getCurrentPosition(successCallback,
errorCallback,
{maximumAge:600000, timeout:0});
function successCallback(position) {
// By using the 'maximumAge' option above, the position
// object is guaranteed to be at most 10 minutes old.
// By using a 'timeout' of 0 milliseconds, if there is
// no suitable cached position available, the user agent
// will aynchronously invoke the error callback with code
// TIMEOUT and will not initiate a new position
// acquisition process.
}
function errorCallback(error) {
switch(error.code) {
case error.TIMEOUT:
// Quick fallback when no suitable cached position exists.
doFallback();
// Acquire a new position object.
navigator.geolocation.getCurrentPosition(successCallback, errorCallback);
break;
case ... // treat the other error cases.
};
}
function doFallback() {
// No fresh enough cached position available.
// Fallback to a default position.
}
更好的是。。。下面是关于这段代码和更多地理位置信息的文档链接
希望有帮助 var-watchID;
var watchID;
// Obtain the initial location one-off
navigator.geolocation.getCurrentPosition(getPosition);
// Obtain the location at regularly interval<br>
watchID = navigator.geolocation.watchPosition(getPosition);
// Discontinue watch
navigator.geolocation.clearWatch(watchID);
//一次性获得初始位置
navigator.geolocation.getCurrentPosition(getPosition);
//定期获取位置
watchID=navigator.geolocation.watchPosition(getPosition);
//停止手表
navigator.geolocation.clearWatch(watchID);
资料来源: