/**
* 以下为html5代码,获取地理位置
*/
function getLocation() {
//检查浏览器是否支持地理位置获取
if (navigator.geolocation) {
//若支持地理位置获取,成功调用showPosition(),失败调用showError
// alert("正在努力获取位置...")
var config = { enableHighAccuracy: true, timeout: 5000, maximumAge: 30000 }
navigator.geolocation.getCurrentPosition(showPosition, showError, config)
} else {
//alert("Geolocation is not supported by this browser.")
alert("定位失败,用户已禁用位置获取权限")
}
}
/**
* 获取地址位置成功
*/
function showPosition(position) {
//获得经度纬度
var x = position.coords.latitude
var y = position.coords.longitude
//配置Baidu Geocoding API
var url = "http://api.map.baidu.com/geocoder/v2/?ak=C93b5178d7a8ebdb830b9b557abce78b" +
"&callback=renderReverse" +
"&location=" + x + "," + y +
"&output=json" +
"&pois=0"
$.ajax({
type: "GET",
dataType: "jsonp",
url: url,
success: function (json) {
if (json == null || typeof (json) == "undefined") {
return
}
if (json.status != "0") {
return
}
setAddress(json.result.addressComponent)
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert("[x:" + x + ",y:" + y + "]地址位置获取失败,请手动选择地址")
}
})
}
/**
* 获取地址位置失败[暂不处理]
*/
function showError(error) {
switch (error.code) {
case error.PERMISSION_DENIED:
alert("定位失败,用户拒绝请求地理定位")
//x.innerHTML = "User denied the request for Geolocation.[用户拒绝请求地理定位]"
break
case error.POSITION_UNAVAILABLE:
alert("定位失败,位置信息是不可用")
//x.innerHTML = "Location information is unavailable.[位置信息是不可用]"
break
case error.TIMEOUT:
alert("定位失败,请求获取用户位置超时")
//x.innerHTML = "The request to get user location timed out.[请求获取用户位置超时]"
break
case error.UNKNOWN_ERROR:
alert("定位失败,定位系统失效")
//x.innerHTML = "An unknown error occurred.[未知错误]"
break
}
}
/**
* 设置地址
*/
function setAddress(json) {
var position = document.getElementById("txtPosition")
//省
var province = json.province
//市
var city = json.city
//区
var district = json.district
province = province.replace('市', '')
position.value = province + "," + city + "," + district
position.style.color = 'black'
}
Geolocation接口不可以获取手机的gps定位,这个api具有自己特有的定位方式。定位用户的位置
HTML5 Geolocation API 用于获得用户的地理位置。
鉴于该特性可能侵犯用户的隐私,除非用户同意,否则用户位置信息是不可用的。
用法如下:
<!DOCTYPE html>
<html>
<body>
<p id="demo">点击这个按钮,获得您的位置:</p>
<button onclick="getLocation()">试一下</button>
<div id="mapholder"></div>
<script src="http://maps.google.com/maps/api/js?sensor=false"></script>
<script>
var x=document.getElementById("demo")
function getLocation()
{
if (navigator.geolocation)
{
navigator.geolocation.getCurrentPosition(showPosition,showError)
}
else{x.innerHTML="Geolocation is not supported by this browser."}
}
function showPosition(position)
{
lat=position.coords.latitude
lon=position.coords.longitude
latlon=new google.maps.LatLng(lat, lon)
mapholder=document.getElementById('mapholder')
mapholder.style.height='250px'
mapholder.style.width='500px'
var myOptions={
center:latlon,zoom:14,
mapTypeId:google.maps.MapTypeId.ROADMAP,
mapTypeControl:false,
navigationControlOptions:{style:google.maps.NavigationControlStyle.SMALL}
}
var map=new google.maps.Map(document.getElementById("mapholder"),myOptions)
var marker=new google.maps.Marker({position:latlon,map:map,title:"You are here!"})
}
function showError(error)
{
switch(error.code)
{
case error.PERMISSION_DENIED:
x.innerHTML="User denied the request for Geolocation."
break
case error.POSITION_UNAVAILABLE:
x.innerHTML="Location information is unavailable."
break
case error.TIMEOUT:
x.innerHTML="The request to get user location timed out."
break
case error.UNKNOWN_ERROR:
x.innerHTML="An unknown error occurred."
break
}
}
</script>
</body>
</html>