3

我意识到 GDK 尚未发布,但我已经开始尝试按照谷歌的鼓励将 APK 放在 Glass 上(https://developers.google.com/glass/gdk)。我写了一个非常简单的慢跑应用程序,它可以从 LocationManager 计算距离和速度。有谁知道这目前是否应该或不应该在 Glass 上原生工作(即无需与手机配对)?从 Glass 的拆解来看,它看起来像是内置 GPS 芯片。不过,我在从 Glass 获取 GPS 时遇到问题,而且我感觉它目前受到限制。

4

1 回答 1

1

是的,但诀窍是您需要使用 requestLocationUpdates() 而不是 getLastKnownLocation()。这就是让我绊倒的部分,因为如果我只是执行 getLastKnownLocation(),我最终会得到空值。

因此,使用 Compass 示例作为基础(以避免其他潜在的问题和努力),如果您可以正常运行,这里有一个非常基本的 hack 来扩展它并添加位置更新:

  1. 如果您像我一样使用 Android Studio,并且遇到“未引用符号”错误(像我一样)并且懒得用不同的方式解决它(我认为您看到了趋势......),请提前定义一些事情时间:

    private LocationManager mLocationManager;
    private LocationListener mLocationListener;
    private Criteria criteria;
    
  2. 更新 onKeyDown() 方法

    ...
    String directionName = mSpokenDirections[getDirectionIndex(heading)];
    String headingText = String.format(speechFormat, (int) heading, directionName);
    // **** Location hack starts here... ****
    criteria = new Criteria();
    criteria.setAccuracy(Criteria.ACCURACY_FINE);
    provider = mLocationManager.getBestProvider(criteria, true);
    boolean isEnabled = mLocationManager.isProviderEnabled(provider);
    if (isEnabled) {
        headingText += " and GPS is enabled on provider " + provider;
        // Define a listener that responds to location updates
        LocationListener locationListener = new LocationListener() {
            public void onLocationChanged(Location location) {
                // Called when a new location is found by the network location provider.
                makeUseOfNewLocation(location);
            }
    
            public void onStatusChanged(String provider, int status, Bundle extras) {}
    
            public void onProviderEnabled(String provider) {}
    
            public void onProviderDisabled(String provider) {}
        };
    
        // Register the listener with the Location Manager to receive location updates
        mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 10000, 0, locationListener);
    } else {
        headingText = ", but GPS not enabled on provider " + provider;
    }
    // **** Location hack ends here ****
    mSpeech.speak(headingText, TextToSpeech.QUEUE_FLUSH, null);
    return true;
    ...
    

    这里只是添加来自文档的 LocationListener 片段:http: //developer.android.com/guide/topics/location/strategies.html,并添加一些标准。您可以在提供程序中进行硬编码,但我想使用 Glass 报告为“最佳”的任何内容,因此它会(希望)适应不同的场景(尚未验证,所以目前只是一个希望)。

  3. 添加一个简单的 makeUseOfLocation() 方法来说明细节:

    public void makeUseOfNewLocation(Location location) {
      if (location != null) {
        String lat = "Latitude: " + location.getLatitude();
        String lng = ", Longitude: " + location.getLongitude();
        String summary = lat + lng;
        mSpeech.speak(summary, TextToSpeech.QUEUE_FLUSH, null);
     }
     return;
    }
    

    这样你就可以四处走动,听听它认为你在哪里。我在大楼里闲逛了一下,只是为了好玩。

  4. 向您的清单添加适当的权限

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

构建并运行...我在有和没有与我的手机配对的 Glass(XE9 更新)的情况下都试过了,一切似乎都符合预期。

于 2013-09-25T05:33:37.650 回答