2014-01-12 31 views
1

給空位置我試圖獲取當前位置在android系統的,我已經使用了Android和下面是我的代碼位置經理的android

locationManager = (LocationManager) getSystemService(this.context.LOCATION_SERVICE); 
Criteria crta = new Criteria(); 
    crta.setAccuracy(Criteria.ACCURACY_FINE); 
    crta.setAltitudeRequired(true); 
    crta.setBearingRequired(true); 
    crta.setCostAllowed(true); 
    crta.setPowerRequirement(Criteria.POWER_LOW); 
    String provider = locationManager.getBestProvider(crta, true); 
    Log.d("","provider : "+provider); 
    provider = LocationManager.GPS_PROVIDER; 
    Log.d("","provider : "+provider); 
    locationManager.requestLocationUpdates(provider, 0, 0, (LocationListener) this); 
    Location location = locationManager.getLastKnownLocation(provider); 
    //isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER); 

    if (location != null) 
    { 
     double latitude = location.getLatitude(); 
     double longitude = location.getLongitude(); 
     txtLat.setText("Latitude"+latitude+"longitude"+longitude); 
     Log.d("not null", "location is not null"); 
    } 
    else 
    { 

     Toast.makeText(getApplicationContext(), "locatyion is null", Toast.LENGTH_LONG).show(); 
     Log.d("null","location is null"); 
    } 

的問題是,它不給我我的當前位置當我打開應用程序的位置爲空。誰能幫忙?我看過很多例子,也嘗試了其他例子,但沒有奏效。 我已經加入

if (!isNetworkEnabled) 
    { 
    Toast.makeText(getApplicationContext(), "No network provider the gps wont work", Toast.LENGTH_LONG).show(); 
     // no network provider is enabled 
    } 
    else{ 

     Toast.makeText(getApplicationContext(), "Network is available", Toast.LENGTH_LONG).show(); 
    } 

其說GPS可用和網絡不可用

回答

0

代碼必須是異步與位置的廣播接收器改變聽者...

這是因爲GPS需要一些時間提供一個位置,當它首次啓動時,所有內容都是null

看到下面的代碼:

public class Example extends Activity implements LocationListener { 
    LocationManager mLocationManager; 

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

     mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
     mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this); 

     Location location = mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); 
     if(location != null && location.getTime() > Calendar.getInstance().getTimeInMillis() - 2 * 60 * 1000) { 
      // Do something with the recent location fix 
      // if it is less than two minutes old, 
      // otherwise wait for the update below 
     } 
    } 

    public void onLocationChanged(Location location) { 
     if (location != null) { 
      Log.v("Location Changed", location.getLatitude() + " and " + location.getLongitude()); 
      // You need to call this whenever you are done: 
      // mLocationManager.removeUpdates(this); 
     } 
    } 

    // Required functions  
    public void onProviderDisabled(String arg0) {} 
    public void onProviderEnabled(String arg0) {} 
    public void onStatusChanged(String arg0, int arg1, Bundle arg2) {} 
} 

here