创建空白位置对象

发布于 2024-10-01 00:56:19 字数 1903 浏览 6 评论 0 原文

我和其他几个人正在开发一个 Android 应用程序。它需要定位设备的纬度和经度。我们已经能够创建一个位置对象,但该对象始终为空。我们甚至尝试在一个完全空的项目中重新创建代码,但这也失败了。这是我们的根本活动:

package com.app.Locationtest;

import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;

public class locationtest extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        LocationManager locman =(LocationManager)getSystemService(Context.LOCATION_SERVICE); 
        Location loc = locman.getLastKnownLocation(LocationManager.GPS_PROVIDER);
        if (loc==null)
        {
           finish();
        }
    }
}

这是清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.app.Locationtest"
      android:versionCode="1"
      android:versionName="1.0">
      <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
      <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
      <uses-permission android:name="android.permission.ACCESS_GPS" />
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".locationtest"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>
    <uses-sdk android:minSdkVersion="8" />

</manifest> 

我们如何解决这个问题?

A couple of other people and me are working on an app for android. It requires locating the device in latitude and longitude. We've been able to create a location object, but the object is always blank. We even tried recreating the code in a completely empty project, but that also failed. Here's our root activity:

package com.app.Locationtest;

import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;

public class locationtest extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        LocationManager locman =(LocationManager)getSystemService(Context.LOCATION_SERVICE); 
        Location loc = locman.getLastKnownLocation(LocationManager.GPS_PROVIDER);
        if (loc==null)
        {
           finish();
        }
    }
}

Here's the Manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.app.Locationtest"
      android:versionCode="1"
      android:versionName="1.0">
      <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
      <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
      <uses-permission android:name="android.permission.ACCESS_GPS" />
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".locationtest"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>
    <uses-sdk android:minSdkVersion="8" />

</manifest> 

How do we fix this problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

窗影残 2024-10-08 00:56:20

有时设备需要太多时间来检索位置,这是检索 android 站点

  1. 启动应用程序。
  2. 一段时间后,开始侦听来自所需位置提供商的更新。
  3. 通过过滤掉新的但不太准确的修复来维护位置的“当前最佳估计”。
  4. 停止监听位置更新。
  5. 利用最后的最佳位置估计。

我使用自定义位置侦听器并开始侦听位置更新,因为我的应用程序已初始化,即使我没有显示地图:

locationManager = (LocationManager) this.getSystemService(LOCATION_SERVICE);
locationListener = new CustomLocationListener(); 
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);

我有一个正在侦听位置的线程,因此当用户点击并调用我的地图视图时,如果位置为空,我们向用户发送一条消息,等待我们检索他的位置。

您可能想要开发一种方法来选择更好的位置,因为最后一个位置可能不是最佳位置,请尝试使用以下方法:

private static final int TWO_MINUTES = 1000 * 60 * 2;

/** Determines whether one Location reading is better than the current Location fix
  * @param location  The new Location that you want to evaluate
  * @param currentBestLocation  The current Location fix, to which you want to compare the new one
  */
protected boolean isBetterLocation(Location location, Location currentBestLocation) {
    if (currentBestLocation == null) {
        // A new location is always better than no location
        return true;
    }

    // Check whether the new location fix is newer or older
    long timeDelta = location.getTime() - currentBestLocation.getTime();
    boolean isSignificantlyNewer = timeDelta > TWO_MINUTES;
    boolean isSignificantlyOlder = timeDelta < -TWO_MINUTES;
    boolean isNewer = timeDelta > 0;

    // If it's been more than two minutes since the current location, use the new location
    // because the user has likely moved
    if (isSignificantlyNewer) {
        return true;
    // If the new location is more than two minutes older, it must be worse
    } else if (isSignificantlyOlder) {
        return false;
    }

    // Check whether the new location fix is more or less accurate
    int accuracyDelta = (int) (location.getAccuracy() - currentBestLocation.getAccuracy());
    boolean isLessAccurate = accuracyDelta > 0;
    boolean isMoreAccurate = accuracyDelta < 0;
    boolean isSignificantlyLessAccurate = accuracyDelta > 200;

    // Check if the old and new location are from the same provider
    boolean isFromSameProvider = isSameProvider(location.getProvider(),
            currentBestLocation.getProvider());

    // Determine location quality using a combination of timeliness and accuracy
    if (isMoreAccurate) {
        return true;
    } else if (isNewer && !isLessAccurate) {
        return true;
    } else if (isNewer && !isSignificantlyLessAccurate && isFromSameProvider) {
        return true;
    }
    return false;
}

/** Checks whether two providers are the same */
private boolean isSameProvider(String provider1, String provider2) {
    if (provider1 == null) {
      return provider2 == null;
    }
    return provider1.equals(provider2);
 }

此代码在我链接的同一页面上提供。

希望这有帮助!

Sometimes the device needs too much time to retrieve the location, this is the flow for retrieving locations listed on the android site:

  1. Start application.
  2. Sometime later, start listening for updates from desired location providers.
  3. Maintain a "current best estimate" of location by filtering out new, but less accurate fixes.
  4. Stop listening for location updates.
  5. Take advantage of the last best location estimate.

I use a custom location listener and start listening for location updates, since my app is initialized, even if I'm not showing the map:

locationManager = (LocationManager) this.getSystemService(LOCATION_SERVICE);
locationListener = new CustomLocationListener(); 
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);

I have a thread that is listening the locations, so when a user taps and calls my mapview, if the location is null, we send a msg to the user, to wait while we are retrieving his location.

You might want to develop a method to choose the better location, because the last location maybe is not the best location, try with this:

private static final int TWO_MINUTES = 1000 * 60 * 2;

/** Determines whether one Location reading is better than the current Location fix
  * @param location  The new Location that you want to evaluate
  * @param currentBestLocation  The current Location fix, to which you want to compare the new one
  */
protected boolean isBetterLocation(Location location, Location currentBestLocation) {
    if (currentBestLocation == null) {
        // A new location is always better than no location
        return true;
    }

    // Check whether the new location fix is newer or older
    long timeDelta = location.getTime() - currentBestLocation.getTime();
    boolean isSignificantlyNewer = timeDelta > TWO_MINUTES;
    boolean isSignificantlyOlder = timeDelta < -TWO_MINUTES;
    boolean isNewer = timeDelta > 0;

    // If it's been more than two minutes since the current location, use the new location
    // because the user has likely moved
    if (isSignificantlyNewer) {
        return true;
    // If the new location is more than two minutes older, it must be worse
    } else if (isSignificantlyOlder) {
        return false;
    }

    // Check whether the new location fix is more or less accurate
    int accuracyDelta = (int) (location.getAccuracy() - currentBestLocation.getAccuracy());
    boolean isLessAccurate = accuracyDelta > 0;
    boolean isMoreAccurate = accuracyDelta < 0;
    boolean isSignificantlyLessAccurate = accuracyDelta > 200;

    // Check if the old and new location are from the same provider
    boolean isFromSameProvider = isSameProvider(location.getProvider(),
            currentBestLocation.getProvider());

    // Determine location quality using a combination of timeliness and accuracy
    if (isMoreAccurate) {
        return true;
    } else if (isNewer && !isLessAccurate) {
        return true;
    } else if (isNewer && !isSignificantlyLessAccurate && isFromSameProvider) {
        return true;
    }
    return false;
}

/** Checks whether two providers are the same */
private boolean isSameProvider(String provider1, String provider2) {
    if (provider1 == null) {
      return provider2 == null;
    }
    return provider1.equals(provider2);
 }

This code is provided on the same page I linked.

Hope this helps!

心头的小情儿 2024-10-08 00:56:19

getLastKnownLocation() javadoc说:“..如果提供程序当前被禁用,则返回 null。”

所以它依赖于 GPS,但它不会打开它。它用于搭载其他使用 GPS 的应用程序。

getLastKnownLocation() javadoc says: ".. If the provider is currently disabled, null is returned."

So it relies on GPS but it does not turn it on. It is used to piggyback on other applications using GPS.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文