//地理位置获取 function getLocation(){ if (navigator.geolocation){ navigator.geolocation.getCurrentPosition(showPosition,showError,{ enableHighAccuracy:false, timeout:5000, maximumAge:3000 }); }else{ itbeing.alert("您的浏览器不支持地理定位"); } } function showPosition(position){ //var latlon = position.coords.latitude+','+position.coords.longitude; doShowBaiduPositon(position.coords); } function doShowBaiduPositon(coords){ var latlon = coords.latitude+','+coords.longitude; var url = "http://api.map.baidu.com/geocoder/v2/?ak=WxY4sOpglGhaLZtMncuuut5sZnMWPoQE&callback=renderReverse&location="+latlon+"&output=json&pois=0"; $.ajax({ type: "GET", dataType: "jsonp", url: url, beforeSend: function(){ $("#area").html('正在定位...'); }, success: function (json) { if(json.status==0){ //$("#area").html(json.result.addressComponent.city+' '+json.result.addressComponent.street); $("#area").html(json.result.addressComponent.city+' '+json.result.addressComponent.street); $("#lng").val(json.result.location.lng); $("#lat").val(json.result.location.lat); $("#location").val(json.result.addressComponent.city + ' ' + json.result.addressComponent.street); } }, error: function (XMLHttpRequest, textStatus, errorThrown) { $("#area").html('地址位置获取失败'); } }); } function showError(error){ switch(error.code) { case error.PERMISSION_DENIED: itbeing.alert("GPS定位失败,可能因为安全隐私缘由,用户拒绝请求地理定位,系统将使用移动IP地址进行定位!"); $.getJSON('https://ipinfo.io/geo', function(response) { var loc = response.loc.split(','); var coords = { latitude: loc[0], longitude: loc[1] }; doShowBaiduPositon(coords); }); break; case error.POSITION_UNAVAILABLE: itbeing.alert("定位失败,位置信息是不可用!"); break; case error.TIMEOUT: itbeing.alert("定位失败,请求获取用户位置超时!"); break; case error.UNKNOWN_ERROR: itbeing.alert("定位失败,定位系统失效!"); break; } }