手机js页面实现一键定位,并且把获取的地理位置显示到文本框中代码?

JavaScript06

手机js页面实现一键定位,并且把获取的地理位置显示到文本框中代码?,第1张

手机js页面实现一键定位,并且把获取的地理位置显示到文本框中代码如下:

var getLocation = function (successFunc, errorFunc) { //successFunc获取定位成功回调函数,errorFunc获取定位失败回调

//首先设置默认城市

var defCity = {

id: '000001',

name: '北京市',

date: curDateTime()//获取当前时间方法

}

//默认城市

$.cookie('VPIAO_MOBILE_DEFAULTCITY', JSON.stringify(defCity), { expires: 1, path: '/' })

if (navigator.geolocation) {

navigator.geolocation.getCurrentPosition(function (position) {

var lat = position.coords.latitude

var lon = position.coords.longitude

//var map = new BMap.Map("container")  // 创建Map实例

var point = new BMap.Point(lon, lat)// 创建点坐标

var gc = new BMap.Geocoder()

gc.getLocation(point, function (rs) {

var addComp = rs.addressComponents

var curCity = {

id: '',

name: addComp.province,

date: curDateTime()

}

//当前定位城市

$.cookie('VPIAO_MOBILE_CURRENTCITY', JSON.stringify(curCity), { expires: 7, path: '/' })

//alert(addComp.province + ", " + addComp.city + ", " + addComp.district + ", " + addComp.street)

if (successFunc != undefined)

successFunc(addComp)

})

},

function (error) {

switch (error.code) {

case 1:

alert("位置服务被拒绝。")

break

case 2:

alert("暂时获取不到位置信息。")

break

case 3:

alert("获取位置信息超时。")

break

default:

alert("未知错误。")

break

}

var curCity = {

id: '000001',

name: '北京市',

date: curDateTime()

}

//默认城市

$.cookie('VPIAO_MOBILE_DEFAULTCITY', JSON.stringify(curCity), { expires: 1, path: '/' })

if (errorFunc != undefined)

errorFunc(error)

}, { timeout: 5000, enableHighAccuracy: true })

} else {

alert("你的浏览器不支持获取地理位置信息。")

if (errorFunc != undefined)

errorFunc("你的浏览器不支持获取地理位置信息。")

}

}

var showPosition = function (position) {

var lat = position.coords.latitude

var lon = position.coords.longitude

//var map = new BMap.Map("container")  // 创建Map实例

var point = new BMap.Point(lon, lat)// 创建点坐标

var gc = new BMap.Geocoder()

gc.getLocation(point, function (rs) {

var addComp = rs.addressComponents

var curCity = {

id: '',

name: addComp.province,

date: curDateTime()

}

//当前定位城市

$.cookie('VPIAO_MOBILE_CURRENTCITY', JSON.stringify(curCity), { expires: 7, path: '/' })

//alert(addComp.province + ", " + addComp.city + ", " + addComp.district + ", " + addComp.street)

})

}

var showPositionError = function (error) {

switch (error.code) {

case 1:

alert("位置服务被拒绝。")

break

case 2:

alert("暂时获取不到位置信息。")

break

case 3:

alert("获取位置信息超时。")

break

default:

alert("未知错误。")

break

}

var curCity = {

id: '000001',

name: '北京市',

date: curDateTime()

}

//默认城市

$.cookie('VPIAO_MOBILE_DEFAULTCITY', JSON.stringify(curCity), { expires: 1, path: '/' })

}

一、PC端通过IP实现定位代码

<script type="text/javascript" src="http://fw.qq.com/ipaddress"></script>

<script type="text/javascript">

document.write(IPData[0])   //显示IP地址

document.write(IPData[2])   //显示省

document.write(IPData[3])   //显示市

</script>

二、获取客户端地址代码

<script>

var url = 'http://chaxun.1616.net/s.php?type=ip&output=json&callback=?&_=' + Math.random()

$.getJSON(url, function(data) {

alert(data.Ip)

})

</script>

js网页绝对定位/相对定位,参考如下:

<html>

<head>

<meta http-equiv="content-type" content="text/htmlcharset=GB2312" />

<title>CSS和Js演示网页绝对定位与相对定位</title>

<!-- 通用样式表 -->

<style>

* { font-size:12pxfont-family:宋体, Arial} /*规定了所有的字体样式*/

body { overflow:autoborder-style:nonemargin:0pxpadding:0px}

.tools { position:absolutetop:180pxleft:10px}

#abs { position:absoluteleft:10pxtop:10pxwidth:150pxheight:150pxbackground-color:buttonfacepadding:10px}

#rel { position:relativeleft:170pxtop:40pxwidth:150pxheight:120pxbackground-color:buttonfacepadding:10px}

</style>

<script>

function logEvent(){

$("ipt_x").value = event.x

$("ipt_y").value = event.y

$("ipt_x2").value = event.clientX

$("ipt_y2").value = event.clientY

}

function $(str){ return(document.getElementById(str))}

window.onload = function(){

document.onmousemove = logEvent

}

</script>

</head>

<body>

<div id="abs">绝对定位</div>

<div id="rel">相对定位</div>

<div class="tools">

event.x <input id="ipt_x" size="15" >

event.y <input id="ipt_y" size="15" >

<br/>

event.clientX <input id="ipt_x2" size="9" >

event.clientY <input id="ipt_y2" size="9" >

</div>

</body>

</html>

  直接定位实时位置。

  

GPS系统的前身是美军研制的一种子午仪卫星定位系统(Transit),1958年研制,1964年正式投入使用。该系统用5到6颗卫星组成的星网工作,每天最多绕过地球13次,并且无法给出高度信息,在定位精度方面也不尽如人意。

然而,子午仪系统使得研发部门对卫星定位取得了初步的经验,并验证了由卫星系统进行定位的可行性,为GPS系统的研制埋下了铺垫。由于卫星定位显示出在导航方面的巨大优越性及子午仪系统存在对潜艇和舰船导航方面的巨大缺陷。

美国海陆空三军及民用部门都感到迫切需要一种新的卫星导航系统。

为此,美国海军研究实验室(NRL)提出了名为Tinmation的用12到18颗卫星组成10000km高度的全球定位网计划,并于67年、69年和74年各发射了一颗试验卫星,在这些卫星上初步试验了原子钟计时系统,这是GPS系统精确定位的基础。

而美国空军则提出了621-B的以每星群4到5颗卫星组成3至4个星群的计划,这些卫星中除1颗采用同步轨道外其余的都使用周期为24h的倾斜轨道,该计划以伪随机码(PRN)为基础传播卫星测距信号,其强大的功能,当信号密度低于环境噪声的1%时也能将其检测出来。

以上内容参考:百度百科-GPS定位