2012-09-18 88 views
0

這裏的東西。我使用了相同的功能,我在分離的類,只包含特定的GPS只是爲了嘗試。它工作,但我必須說座標值不正確。現在我試圖在我的真實項目中實現相同的功能,但它不工作。它確實檢測到我是否啓用或禁用了手機中的GPS,但沒有出現協調值。android檢測GPS提供商,但沒有得到的位置

代碼,我做了兩個類。 Froyo2Activity和MyLocationListener。 Froyo2Activity是僅擴展Activity的主要活動,MyLocationListener實現LocationListener。爲了更好地查看,這裏是我的代碼。

public class Froyo2Activity extends Activity{ 
Spinner priorSpin, genderSpin; 
ImageView femaleView, maleView; 
Button submit, agePrev, ageNext, bpPrev, bpNext, pPrev, pNext, butLoc; 
SeekBar seekAge, seekBP; 
EditText ageEdit, bpEdit, pEdit, locEdit, fname, lname; 
TextView tv; 

String commandline="", ageString="", bpString="", pString=""; 
int age = 0, bp = 0, p = 0; 
double latitude, longitude, altitude, pitch = 0;  

private static final int REQUEST_CODE = 10; 
private LocationManager locationManager; 
private String provider; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_froyo2);  

    fname = (EditText) findViewById(R.id.editText1); 
    lname = (EditText) findViewById(R.id.editText2); 
    ageEdit = (EditText) findViewById(R.id.editText7); 
    pEdit = (EditText) findViewById(R.id.editText9); 
    locEdit = (EditText) findViewById(R.id.editText3); 
    femaleView = (ImageView)findViewById(R.id.femaleView); 
    maleView = (ImageView)findViewById(R.id.maleView); 
    priorSpin = (Spinner) findViewById(R.id.prior_spin); 
    genderSpin = (Spinner)findViewById(R.id.gender_spin); 
    tv = (TextView) findViewById(R.id.textView14); 


    //---set the GPS--- 
    //Get the location manager 
    LocationManager mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE); 
     LocationListener mlocListener = new MyLocationListener(); 
     mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, mlocListener); 

     if (mlocManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) { 
      if(MyLocationListener.latitude>0) 
      { 
       latitude=MyLocationListener.latitude; 
       longitude=MyLocationListener.longitude; 
       tv.setText("Latitude: "+ Double.toString(latitude) +" Longitude: "+ Double.toString(longitude)+" Altitude:"+ Double.toString(pitch)+"");       
      } 
      else 
      { 
       tv.setText("Signal Not Available"); 
      } 
     } else { 
      tv.setText("GPS is not turned on...");    
     } 
    //---end GPS--- 

這是MyLocationListener

代碼
package com.example.froyo2; 

import android.os.Bundle; 
import android.app.Activity; 
import android.view.Menu; 
import android.content.Context; 
import android.location.Criteria; 
import android.location.Location; 
import android.location.LocationListener; 
import android.location.LocationManager; 
import android.widget.TextView; 
import android.widget.Toast; 

public class MyLocationListener implements LocationListener { 
public static double latitude; 
public static double longitude; 
public static double salah=90; 



@Override 
public void onLocationChanged(Location loc) 


    { 
//  loc.getLatitude(); 
//  loc.getLongitude(); 
     latitude=loc.getLatitude(); 
     longitude=loc.getLongitude(); 
     salah=1; 
} 

@Override 
public void onProviderDisabled(String provider) 
{ 
    salah=2; 
} 

@Override 
public void onProviderEnabled(String provider) 
{ 


    salah=3; 
    } 

    @Override 
    public void onStatusChanged(String provider, int status, Bundle extras) 
    { 
     salah=4; 
    } 
} 

沒有人知道如何解決這個問題。就像我之前說過的那樣,當我用自己的課堂嘗試gps功能時;意思是沒有執行其他功能或活動,特別是僅針對GPS功能測試的類,它確實有效 - 至少指出位置的編號。但是,在實現具有其他功能的GPS時,它表示信號不可用(請參閱代碼)。如果這個問題能夠解決,這將是有益的。

感謝您的提前。 (SaraBrown)

回答

1

使用下面的代碼:

locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 
       MINIMUM_TIME_BETWEEN_UPDATES, 
       MINIMUM_DISTANCE_CHANGE_FOR_UPDATES, locationListener); 

對於LocationListener的,你可以使用這個(將這個你Froyo2Activity內):

LocationListener locationListener = new LocationListener() { 
     public void onLocationChanged(Location location) { 
      // Called when a new location is found by the GPS location 
      // provider. 

      String message = String.format(
        "New Location \n Longitude: %1$s \n Latitude: %2$s", 
        location.getLongitude(), location.getLatitude()); 
      Toast.makeText(LbsGeocodingActivity.this, message, 
        Toast.LENGTH_LONG).show(); 
      // Here you can set your Value of Textview 

     } 

我有在我的代碼中使用它,它工作得很好。

-2

有時GPS提供商它不會工作。與網絡提供商

+0

如果你解釋爲什麼有時GPS提供商不能爲你提供一個位置,爲什麼/何時使用網絡提供商作爲替代方案 – paulkayuk

+0

我試着將GPS_PROVIDER更改爲NETWORK_PROVIDER,但它'不幸關閉'。 –

-1

改變這一行

if (mlocManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) 

if (mlocManager.isProviderEnabled(locationManager.GPS_PROVIDER)) 

嘗試它會正常工作

0

我認爲你需要更換

if(MyLocationListener.latitude>0) 
if(mlocListener.latitude>0) 

希望它可以幫助

相關問題