首页 \ 问答 \ 如何及时找到设备位置(How to find device location in a timely manner)

如何及时找到设备位置(How to find device location in a timely manner)

我正在创建一个应用程序,需要找到本地设备的位置,并将其与另一个设备的位置进行比较。 我正在尝试使用Google Play服务位置apis来查找设备的位置,但似乎连接时间太长。 我还没有看到它连接。 它要么非常慢,要么设置不正确。 连接时这些apis通常很快吗?

这是我有的:

public class LocationHandler implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener
{
    private Activity currActivity = null;
    private GoogleApiClient mGoogleApiClient = null;

    public LocationHandler(Activity act)
    {
        currActivity = act;
    }

    public void Setup()
    {
        if (mGoogleApiClient == null)
        {
            mGoogleApiClient = new GoogleApiClient.Builder(currActivity)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .addApi(LocationServices.API)
                .build();
            mGoogleApiClient.connect();
        }
    }

    @Override
    public void onConnected(@Nullable Bundle bundle)
    {
        Toast.makeText(currActivity, "Connected to service.", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onConnectionSuspended(int i)
    {

    }

    @Override
    public void onConnectionFailed(@NonNull ConnectionResult connectionResult)
    {

    }
}

我有以下权限:

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_GPS" />
<uses-permission android:name="android.permission.INTERNET" />

我还将它添加到build.gradle:

compile 'com.google.android.gms:play-services:9.2.0'

I am creating an app that needs to find the location of the local device and compare it to the location of another device. I am attempting to use the Google Play Services location apis to find the locations of the devices but it seems like it just takes way too long to connect. I haven't seen it connect yet. It is either really slow or something isn't setup correctly. Are these apis normally pretty quick when connecting?

Here is what I have:

public class LocationHandler implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener
{
    private Activity currActivity = null;
    private GoogleApiClient mGoogleApiClient = null;

    public LocationHandler(Activity act)
    {
        currActivity = act;
    }

    public void Setup()
    {
        if (mGoogleApiClient == null)
        {
            mGoogleApiClient = new GoogleApiClient.Builder(currActivity)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .addApi(LocationServices.API)
                .build();
            mGoogleApiClient.connect();
        }
    }

    @Override
    public void onConnected(@Nullable Bundle bundle)
    {
        Toast.makeText(currActivity, "Connected to service.", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onConnectionSuspended(int i)
    {

    }

    @Override
    public void onConnectionFailed(@NonNull ConnectionResult connectionResult)
    {

    }
}

I have the following permissions:

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_GPS" />
<uses-permission android:name="android.permission.INTERNET" />

I have also added this to the build.gradle:

compile 'com.google.android.gms:play-services:9.2.0'

原文:https://stackoverflow.com/questions/38274944
更新时间:2024-03-31 15:03

最满意答案

这是动态PIVOT查询:

DECLARE @sql NVARCHAR(MAX),
        @cols NVARCHAR(MAX);

SELECT @cols = 
    STUFF((
        SELECT DISTINCT ',' + QUOTENAME(PropertyName) 
        FROM #tbl
        FOR XML PATH(''), TYPE
        ).value('.', 'NVARCHAR(MAX)') 
    , 1, 1, '')

SELECT @sql = 
'SELECT GUID, ' + @cols + '
FROM (
    SELECT
        GUID, PropertyName, PropertyValue
    FROM #tbl
) t
PIVOT
(
    MAX(PropertyValue)
    FOR PropertyName IN(' + @cols + ')
) p ';

PRINT (@sql);
EXEC (@sql);

在线演示


实现所需结果的另一种方法是使用动态交叉表

DECLARE @sql NVARCHAR(MAX);

SELECT @sql =
'SELECT
    GUID' + CHAR(10) +
(SELECT DISTINCT
'   , MAX(CASE WHEN PropertyName = ''' + PropertyName + ''' THEN PropertyValue END) AS ' + QUOTENAME(PropertyName) + CHAR(10)
FROM #tbl
FOR XML PATH('')
) +
'FROM #tbl
GROUP BY GUID;';

PRINT (@sql);
EXEC (@sql);

在线演示


Here is the dynamic PIVOT query:

DECLARE @sql NVARCHAR(MAX),
        @cols NVARCHAR(MAX);

SELECT @cols = 
    STUFF((
        SELECT DISTINCT ',' + QUOTENAME(PropertyName) 
        FROM #tbl
        FOR XML PATH(''), TYPE
        ).value('.', 'NVARCHAR(MAX)') 
    , 1, 1, '')

SELECT @sql = 
'SELECT GUID, ' + @cols + '
FROM (
    SELECT
        GUID, PropertyName, PropertyValue
    FROM #tbl
) t
PIVOT
(
    MAX(PropertyValue)
    FOR PropertyName IN(' + @cols + ')
) p ';

PRINT (@sql);
EXEC (@sql);

ONLINE DEMO


Another way to achieve the desired result is to use a dynamic crosstab:

DECLARE @sql NVARCHAR(MAX);

SELECT @sql =
'SELECT
    GUID' + CHAR(10) +
(SELECT DISTINCT
'   , MAX(CASE WHEN PropertyName = ''' + PropertyName + ''' THEN PropertyValue END) AS ' + QUOTENAME(PropertyName) + CHAR(10)
FROM #tbl
FOR XML PATH('')
) +
'FROM #tbl
GROUP BY GUID;';

PRINT (@sql);
EXEC (@sql);

ONLINE DEMO

相关问答

更多

相关文章

更多

最新问答

更多
  • linux环境下怎么用maven编译一个已有的java项目?
  • Ionic2和IonicDeploy(Ionic2 and IonicDeploy)
  • 尝试分组和别名数据时出错(Error when Attempting to Group and Alias Data)
  • Facebook PHP API错误代码100:API中提供的appsecret_proof无效(Facebook PHP API Error code 100: Invalid appsecret_proof provided in the API)
  • 不用u盘和光盘怎么安装linux系统
  • 表名作为变量参数(Table name as variable parameter)
  • 我想学计算机 在望京花园住 附近那有这样的地方可以学
  • 删除原始记录的重复项(Removing duplicates with original records)
  • Pandas:使用.astype(str)时忽略空值?(Pandas: ignore null values when using .astype(str)?)
  • Python新手阵列 - 列表转换[重复](Python newbie array - list conversion [duplicate])
  • java.lang.String cannot be cast to java.lang.Float
  • 带参数的选择器(Selector with argument)
  • python中的for循环语法(for loop syntax in python)
  • Nunit - 无法检索类别属性(NUnit - Can't retrieve a category property)
  • python3 xpath无法到达子节点(AttributeError:'NoneType'对象没有属性'text')(python3 xpath can't reach a child node (AttributeError: 'NoneType' object has no attribute 'text'))
  • Android自动 - 如何启用开发人员模式(Android auto - how to enable developer mode)
  • java weka stringtowordvector没有正确计算单词出现次数(java weka stringtowordvector is not counting word occurences properly)
  • 如何解释DHT响应中的“节点”?(How to interpret 'nodes' in a DHT response?)
  • 什么是使用opencv和树莓相机模块进行人脸检测的最佳算法(What is the best algorithm for face detection using opencv and raspberry camera module)
  • 可以somone解释这两个问题的解决方案(c程序,mutex,线程)吗?(Can someone explain the solution of these two questions (c program,mutex,threads)?)
  • 如何解决SyntaxError:意想不到的令牌var gulp(How to fix SyntaxError: Unexpected token var in gulp)
  • 在满足R中的第一个实例后,在所有行中分配值(Assign value in all rows following satisfaction of first instance in R)
  • 如何记录Atk4 auth /登录事件(How to log Atk4 auth/login event)
  • 是否有必要在新的scala项目中添加我的自定义scala库依赖项?(is it necessary to add my custom scala library dependencies in new scala project?)
  • Java中对象类的继承(Inheritance of Object Class in Java)
  • 如何在jquery自动完成中获取当前元素?(How to get current element in jquery autocomplete?)
  • 学电子商务甚么大学好
  • @Override在java中(@Override in java)
  • 虚拟机中怎么给linux装office
  • Laravel邮件参数(sendmail / smtp)(Laravel mail parameters (sendmail/smtp))