2015-09-04 73 views
1

我正在研究我的第一個應用程序,它是一個非常簡單的應用程序。點擊按鈕發送經度/緯度到服務器/數據庫。我正在使用Google Play服務進行定位,並且正在使用Nexus 9平板電腦進行測試。 我發現使用設置>位置(在Nexus 9上) - 高精度&低精度工作正常。數據正確併發送到服務器。但是,當選擇「僅限設備」(GPS)時,它找不到長/緯。我得到我的烤麪包:Android Studio'Device Only'Location with Google Play Services not working

Toast.makeText(MainActivity.this, "Network isn't available", Toast.LENGTH_LONG).show(); 

我是新來的,所以仍然不知道我可能會出錯。我確實有清單文件w/

uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" 

和Google Play服務在我的Gradle文件中。因爲它確實有WiFi無論如何。

而且我也把

System.out.println("Longitude (function name)" + longitude + " and Latitude: " + latitude); 

來查看日誌的時候正在拿起經度/緯度(點擊按鈕,只有當,否則它是0.0)我在去年的測試中,我使用的高精度,並得到了朗/緯度。關閉Wifi和更改位置,它正在接收我的新位置(但由於沒有互聯網連接而無法發送數據)。所以我很困惑,因爲GPS不是以高精度來拾取我的新位置嗎?

不確定是否重要,但我希望只有「設備唯一」的位置以及高精度。 任何幫助將不勝感激!這裏是我的代碼(縮短了,如果我缺少任何東西,請讓我知道添加!)謝謝!

public class MainActivity extends Activity implements ConnectionCallbacks, 
    OnConnectionFailedListener { 

private GoogleApiClient mGoogleApiClient; 
private Location mLastLocation; 
private Button btnNewShowLocation; 
private double longitude; 
private double latitude; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    if (checkPlayServices()) { 
     buildGoogleApiClient(); 
     //**UPDATED** 
     createLocationRequest() 
    } 
    btnNewShowLocation.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      if (mLastLocation != null) { 
       postDataToServer("web site address- EDIT"); 
      } else { 
       Toast.makeText(MainActivity.this, "Network isn't available", Toast.LENGTH_LONG).show(); 
      }}});} 
    // **UPDATED** 
    protected void createLocationRequest() { 
    mLocationRequest = LocationRequest.create() 
      .setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY) 
      .setInterval(UPDATE_INTERVAL) 
      .setFastestInterval(FATEST_INTERVAL) 
      .setSmallestDisplacement(DISPLACEMENT); 

private void postDataToServer(String uri) { 

    RequestPackage p = new RequestPackage(); 
    p.setMethod("POST"); 
    p.setUri(uri); 
    p.setParam("longitude", String.valueOf(longitude)); 
    p.setParam("latitude", String.valueOf(latitude)); 
    MyTask task = new MyTask(); 
    task.execute(p); 
    System.out.println("Longitude (post data to server)" + longitude + " and Latitude: " + latitude); 

} 
protected synchronized void buildGoogleApiClient() { 
    mGoogleApiClient = new GoogleApiClient.Builder(this) 
      .addConnectionCallbacks(this) 
      .addOnConnectionFailedListener(this) 
      .addApi(LocationServices.API).build(); 
} 
private boolean checkPlayServices() { 
    int resultCode = GooglePlayServicesUtil 
      .isGooglePlayServicesAvailable(this); 
    if (resultCode != ConnectionResult.SUCCESS) { 
     if (GooglePlayServicesUtil.isUserRecoverableError(resultCode)) { 
      GooglePlayServicesUtil.getErrorDialog(resultCode, this, 
        PLAY_SERVICES_RESOLUTION_REQUEST).show(); 
     } else { 
      Toast.makeText(getApplicationContext(), 
        "This device is not supported.", Toast.LENGTH_LONG) 
        .show(); 
      finish(); 
     } 
     return false; 
    } 
    return true; 
} 
@Override 
public void onConnected(Bundle arg0) { 
    mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient); 
    System.out.println("Longitude (on Connected): " + longitude + " and Latitude: " + latitude); 

    if (mLastLocation != null) { 
     latitude = mLastLocation.getLatitude(); 
     longitude = mLastLocation.getLongitude(); 
    } else { 
     Toast.makeText(getApplicationContext(), "Searching for your location... \nMake sure WiFi or GPS is turned On", Toast.LENGTH_LONG).show(); 
    }}} 

這裏是高準確度和關閉Wifi的日誌記錄。

I/System.out﹕ Longitude (on Connected): 0.0 and Latitude: 0.0 
I/System.out﹕ Longitude (post data to server)-88.7777777 and Latitude: 55.7777777 
W/System.err﹕ java.net.UnknownHostException: Unable to resolve host 

Wifi最後一行關閉。

+0

我認爲你需要創建'LocationRequest'對象和'setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY) .setInterval(間隔) .setFastestInterval(fastestInterval) .setSmallestDisplacement(minDisplacement);'有關詳細信息,你可以試試我的代碼[here](https://github.com/jbj88817/getLastLocationUsingGPS-android/blob/master/app/src/main/java/com/bojie/locationex/MapsActivity.java#L87-L93)。 – bjiang

+0

感謝@bjiang我確實嘗試過,它確實有效。現在不是。我把它放在一個函數 '保護無效createLocationRequest(){ mLocationRequest = LocationRequest.create().setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY) .setInterval(更新週期​​) .setFastestInterval(FATEST_INTERVAL) .setSmallestDisplacement(位移);'並在OnCreate中調用它。在調試器中,我得到「僅限設備」的getLastLocation = null。我會繼續努力 – JackZ

+0

你有沒有嘗試我的代碼[這裏](https://github.com/jbj88817/getLastLocationUsingGPS-android)? – bjiang

回答

1

您需要創建LocationRequest對象,並執行以下操作:

// Create the LocationRequest object 
     mLocationRequest = LocationRequest.create() 
       .setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY) 
       .setInterval(interval) 
       .setFastestInterval(fastestInterval) 
       .setSmallestDisplacement(minDisplacement); 

有關詳細信息,你可以試試我的代碼here

相關問題