不带任何地图的Javascript地理定位
我正在尝试使用javascript进行地理定位。我一直在搜索,但我只能找到使用地图的示例。我只想在标签、文本框或任何纯文本小部件中显示用户的地址。签出: 退房:不带任何地图的Javascript地理定位,javascript,geolocation,Javascript,Geolocation,我正在尝试使用javascript进行地理定位。我一直在搜索,但我只能找到使用地图的示例。我只想在标签、文本框或任何纯文本小部件中显示用户的地址。签出: 退房: window.onload=getGeo; 函数getGeo(){ if(navigator&&navigator.geolocation){ navigator.geolocation.getCurrentPosition(geoOK,geoKO); }否则{ geoMaxmind(); } } 功能geoOK(位置){ showL
window.onload=getGeo;
函数getGeo(){
if(navigator&&navigator.geolocation){
navigator.geolocation.getCurrentPosition(geoOK,geoKO);
}否则{
geoMaxmind();
}
}
功能geoOK(位置){
showLatLong(位置.坐标.纬度,位置.坐标.经度);
}
函数geoMaxmind(){
showLatLong(地理纬度(),地理经度();
}
功能geoKO(err){
如果(err.code==1){
错误('El usuario ha denegado El permiso para obtener informacion de ubicacion');
}else if(err.code==2){
错误('Tu ubicacion no se puede determinator');
}else if(err.code==3){
错误('超时')
}否则{
错误(‘没有错误’);
}
}
功能显示LATLONG(横向、纵向){
var geocoder=new google.maps.geocoder();
var yourLocation=new google.maps.LatLng(lat,longi);
geocoder.geocode({'latLng':yourLocation},processGeocoder);
}
功能处理地理编码器(结果、状态){
if(status==google.maps.GeocoderStatus.OK){
如果(结果[0]){
document.forms[0]。dir.value=results[0]。格式化的\u地址;
document.getElementById(“”)
}否则{
错误('Google no Returno resultado alguno.');
}
}否则{
错误(“对fallo debido a进行地理编码:+状态);
}
}
函数错误(msg){
警报(msg);
}
window.onload=getGeo;
函数getGeo(){
if(navigator&&navigator.geolocation){
navigator.geolocation.getCurrentPosition(geoOK,geoKO);
}否则{
geoMaxmind();
}
}
功能geoOK(位置){
showLatLong(位置.坐标.纬度,位置.坐标.经度);
}
函数geoMaxmind(){
showLatLong(地理纬度(),地理经度();
}
功能geoKO(err){
如果(err.code==1){
错误('El usuario ha denegado El permiso para obtener informacion de ubicacion');
}else if(err.code==2){
错误('Tu ubicacion no se puede determinator');
}else if(err.code==3){
错误('超时')
}否则{
错误(‘没有错误’);
}
}
功能显示LATLONG(横向、纵向){
var geocoder=new google.maps.geocoder();
var yourLocation=new google.maps.LatLng(lat,longi);
geocoder.geocode({'latLng':yourLocation},processGeocoder);
}
功能处理地理编码器(结果、状态){
if(status==google.maps.GeocoderStatus.OK){
如果(结果[0]){
document.forms[0]。dir.value=results[0]。格式化的\u地址;
document.getElementById(“”)
}否则{
错误('Google no Returno resultado alguno.');
}
}否则{
错误(“对fallo debido a进行地理编码:+状态);
}
}
函数错误(msg){
警报(msg);
}
您可以使用geoplugin
将以下内容添加到您的
标记中
<script src="http://www.geoplugin.net/javascript.gp" type="text/javascript"></script>
您可以使用
geoplugin
将以下内容添加到您的
标记中
<script src="http://www.geoplugin.net/javascript.gp" type="text/javascript"></script>
查看HTML5地理定位API: 简单到:
navigator.geolocation.getCurrentPosition(function(location) {
// This function is called if the location is found. Use location.coords.latitude & location.coords.longitude
}, function() {
// This function is called if the location is not found.
});
但并非所有浏览器都支持它。如果您要将此公开,我还建议您使用Modernizer来检测支持:查看HTML5地理定位API: 简单到:
navigator.geolocation.getCurrentPosition(function(location) {
// This function is called if the location is found. Use location.coords.latitude & location.coords.longitude
}, function() {
// This function is called if the location is not found.
});
但并非所有浏览器都支持它。如果您要将此公开,我还建议您使用Modernizer来检测支持:尝试以下方法:
<script src="http://www.merkwelt.com/people/stan/geo_js/js/geo.js?id=1" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript">
if (geo_position_js.init()) {
geo_position_js.getCurrentPosition(success_callback, error_callback, { enableHighAccuracy: true });
}
else {
alert("Functionality not available");
}
function success_callback(p) {
//alert('lat=' + p.coords.latitude.toFixed(2) + ';lon=' + p.coords.longitude.toFixed(2));
longitude = p.coords.longitude.toFixed(2);
latitude = p.coords.latitude.toFixed(2);
}
function error_callback(p) {
alert('error=' + p.code);
}
</script>
if(geo_position_js.init()){
geo_position_js.getCurrentPosition(成功回调,错误回调,{enableHighAccurance:true});
}
否则{
警报(“功能不可用”);
}
函数成功\u回调(p){
//警报('lat='+p.coords.latitude.toFixed(2)+';lon='+p.coords.longitude.toFixed(2));
经度=p.coords.longitude.toFixed(2);
纬度=p.coords.latitude.toFixed(2);
}
函数错误\u回调(p){
警报(“错误=”+p.code);
}
然后从经度和纬度,您可以使用google api获取位置:
试试这个:
<script src="http://www.merkwelt.com/people/stan/geo_js/js/geo.js?id=1" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript">
if (geo_position_js.init()) {
geo_position_js.getCurrentPosition(success_callback, error_callback, { enableHighAccuracy: true });
}
else {
alert("Functionality not available");
}
function success_callback(p) {
//alert('lat=' + p.coords.latitude.toFixed(2) + ';lon=' + p.coords.longitude.toFixed(2));
longitude = p.coords.longitude.toFixed(2);
latitude = p.coords.latitude.toFixed(2);
}
function error_callback(p) {
alert('error=' + p.code);
}
</script>
if(geo_position_js.init()){
geo_position_js.getCurrentPosition(成功回调,错误回调,{enableHighAccurance:true});
}
否则{
警报(“功能不可用”);
}
函数成功\u回调(p){
//警报('lat='+p.coords.latitude.toFixed(2)+';lon='+p.coords.longitude.toFixed(2));
经度=p.coords.longitude.toFixed(2);
纬度=p.coords.latitude.toFixed(2);
}
函数错误\u回调(p){
警报(“错误=”+p.code);
}
然后从经度和纬度,您可以使用google api获取位置:
为什么不使用HTML5新的地理定位功能呢?我开始认为显示地址比显示地址更有帮助,据我所知,您必须使用外部服务。例如,您可以使用地理名称从地理位置获取国家。我曾经问过一个问题,如何实现这一点:然而,它使用的是php而不是Javascript。为什么不使用HTML5的新地理定位功能呢?我开始认为,显示地址比显示地址更有帮助,据我所知,您将不得不使用外部服务。例如,您可以使用地理名称从地理位置获取国家。我曾经问过一个问题:如何实现这一点:然而,它使用的是php而不是javascript