从功能中获取经度和纬度值 [英] Grabbing longitude and Latitude value from a function

查看:105
本文介绍了从功能中获取经度和纬度值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请问如何在其功能之外获取lat和lon的值,以便在页面上使用else。

  navigator.geolocation.getCurrentPosition(handle_geolocation_query,handle_errors); 

函数handle_errors(错误)
{
switch(error.code)
{
case error.PERMISSION_DENIED:alert(user does not share geolocation数据);
break;
case error.POSITION_UNAVAILABLE:alert(can not detect current position);
break;
case error.TIMEOUT:alert(retrieve position timed out);
break;
default:alert(unknown error);
break;
}
}
函数handle_geolocation_query(position){
var lat =(position.coords.latitude);
var lon =(position.coords.longitude);

}

我想要做的是获得经度和设备的纬度和通过它作为查询和数据库的参数,也为其他的东西使用相同的值。我不知道如何去做。
任何帮助将不胜感激。

解决方案

第一件事情来了(在这个晚上D:是这样的:




  • 在你的处理程序之外声明一个变量,让我们说:



    var coords = {};


  • 然后在你的'handle_geolocation_query'处理程序中,触发一个事件,给你一个ideea,你的位置准备就绪假设你使用jQuery:



    $(coords).trigger('positionReady',position);


  • <之后,你需要这些坐标,听你自定义的'positionReady'事件,然后做你的工作:



    $(coords).on( 'positionReady',function(e,position){
    console.log(position);
    });




这将帮助您避免意大利面条代码,但在同一时间,您需要异步思考,也可能使用(正确)所需的回调。


Please how do i get the value of "lat" and "lon" outside their function to use else where on the page.

   navigator.geolocation.getCurrentPosition(handle_geolocation_query,handle_errors);  

    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 handle_geolocation_query(position){  
        var lat = (position.coords.latitude);
       var lon = (position.coords.longitude); 

    }

What i am trying to do is to get the longitude and latitude of a device and pass it is as a parameter to query and database, also use that same value for other things. I cant figure out how to go about it. Any help will be much appreciated.

解决方案

The first thing came my mind (at this late hour :D) is something like this:

  • declare a variable outside your handlers, let's say:

    var coords = {};

  • then in your 'handle_geolocation_query' handler, trigger an event that gives you an ideea that your position is ready, assuming you're using jQuery:

    $(coords).trigger('positionReady', position);

  • after that, everywhere you need those coordinates, listen for your custom 'positionReady' event, and do your job:

    $(coords).on('positionReady', function(e, position) { console.log(position); });

This will help you to avoid 'spaghetti code', but in the same time, you need to 'think' asynchronously, and maybe using callbacks the (right) way you need.

这篇关于从功能中获取经度和纬度值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆