2010-11-05 39 views
1

其他人和我正在爲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> 

我們如何解決這個問題?

回答

0

有時設備需要太多的時間來檢索位置,這是檢索有關android site上市地點的流動:

  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); 
} 

此代碼。

希望這會有所幫助!

1

getLastKnownLocation() javadoc說:「..如果提供程序當前被禁用,則返回null。」

所以它依賴於GPS,但它並沒有打開它。它用於搭載使用GPS的其他應用程序。

+0

那麼,我們應該使用什麼呢? – AnimatedRNG 2010-11-05 19:05:00

+1

取決於您的應用程序的要求。你需要準確的位置嗎?總是或只是有時候?你是否經常記錄位置,但不需要很高的準確性?你應該閱讀這個http://developer.android.com/guide/topics/location/obtaining-user-location.html – 2010-11-05 19:16:43

+0

所以,我們必須使用LocationProvider?我們希望得到非常好的結果,但我們只需要他們一次。 – AnimatedRNG 2010-11-05 21:03:57