2013-01-20 38 views
-1

我的Gps設置在開始時是「關閉」的,我執行了這段代碼,並且我可以獲取CurrentLocation的經度和緯度。Gps當前位置問題

但是,一旦我以編程方式打開,設置被改變,一切都很好。

當我再次執行時,它沒有給我(提供者,位置引用),我不明白爲什麼會發生。

public void getCurrentlocation() 
{ 
    String providers = Settings.Secure.getString(getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED); 
    Log.e("provider",providers); 
    if(!providers.contains("gps")){ 
     final Intent poke = new Intent(); 
     poke.setClassName("com.android.settings", "com.android.settings.widget.SettingsAppWidgetProvider"); 
     poke.addCategory(Intent.CATEGORY_ALTERNATIVE); 
     poke.setData(Uri.parse("3")); 
     sendBroadcast(poke); 
     Log.e("your gps is","enabled"); 
    } 
    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
     // Define the criteria how to select the locatioin provider -> use 
     // default 
     Criteria criteria = new Criteria(); 
     String provider = locationManager.getBestProvider(criteria,true); 
     Location location = locationManager.getLastKnownLocation(provider); 

     // Initialize the location fields 
     if (location != null) 
     { 
     System.out.println("Provider " + provider + " has been selected."); 
       strLatitude=String.valueOf(location.getLatitude()); 
       strLongitude=String.valueOf(location.getLongitude()); 
       Log.e("lattitude",strLatitude); 
       Log.e("logntitude",strLongitude); 
     } 
     else 
     { 
       strLatitude=String.valueOf(0); 
       strLongitude=String.valueOf(0); 
       Log.e("lattitude",strLatitude); 
       Log.e("logntitude",strLongitude); 
     } 

     Geocoder geocoder = new Geocoder(this, Locale.ENGLISH); 

     try { 
    List<Address> addresses = geocoder.getFromLocation(Double.parseDouble(strLatitude), Double.parseDouble(strLongitude), 5); 
getAddress(Double.parseDouble(strLatitude), Double.parseDouble(strLongitude)); 
    if(addresses != null) { 
    Address returnedAddress = addresses.get(0); 
    StringBuilder strReturnedAddress = new StringBuilder("Address:\n"); 
    for(int i=0; i<returnedAddress.getMaxAddressLineIndex(); i++) { 
    strReturnedAddress.append(returnedAddress.getAddressLine(i)).append("\n"); 
    } 
    Log.e("address",strReturnedAddress.toString()); 
    } 
    else{ 
    Log.e("not found","No Address returned!"); 
    } 
} catch (IOException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
    Log.e("cannot get address","Canont get Address!"); 
} 
    } 

回答

0

試試這個。

Location getGPSLocation(Context context) { 
    try { 
     LocationManager lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE); 
     Criteria cr = new Criteria(); 
     cr.setCostAllowed(true); 
     cr.setAccuracy(Criteria.NO_REQUIREMENT); 
     cr.setPowerRequirement(Criteria.NO_REQUIREMENT); 
     String provider = lm.getBestProvider(cr, true); 
     if(provider == null) 
      provider = lm.getBestProvider(cr, false); 
     return lm.getLastKnownLocation(provider); 
    } 
    catch(Exception e) { 
     Log.e("NeartoMe", "Exception while fetch GPS at: " + e.getMessage()); 
    } 
    return null; 
} 

這樣調用上面的方法。如果你在這裏得到空表示獲取GPS的問題,然後重定向,以啓用位置&安全設置中的「使用無線網絡」。然後再次嘗試獲取GPS。

Location location = new Helper().getGPSLocation(context); 
if(location != null) { 
    latitude = (int) (location.getLatitude() * 1E6); 
    longitude = (int) (location.getLongitude() * 1E6); 
} else { 
    Builder alert = new Builder(context); 
    alert.setTitle(R.string.app_name); 
    alert.setMessage(context.getString(R.string.enable_use_wireless_network)); 
    alert.setPositiveButton(R.string.button_ok, new DialogInterface.OnClickListener() 
    { 
     @Override 
     public void onClick(DialogInterface dialog, int which) 
     { 
      dialog.dismiss(); 
      context.startActivity(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS)); 
     } 
    }); 
    alert.show(); 
} 
0

您正在尋找lastKnownlocation,這會給你,如果設備有任何lastknown的位置,如果GPS是那麼它會尋找新的位置和lastKnownlocation可以爲空。

你應該實現LocationListner。 嘗試使用這樣的:

公共類GetLocationUpdate {

LocationManager locationManager; 
LocationListener locationListner; 
LinearLayout linearLayout; 
TextView locLat, locLong, locInfo; 
LocationProvider locationProvider; 
private Context context; 

/* public double mLattitude = 28.618527, mLongitude = 77.372642; */ 

public double mLattitude = 0.0, mLongitude = 0.0; 
public static GetLocationUpdate getLocationUpdateInstance; 

public static GetLocationUpdate getLocationUpdateInstance(Context context) 
{ 
    getLocationUpdateInstance = new GetLocationUpdate(context); 
    return getLocationUpdateInstance; 
} 

private GetLocationUpdate(Context context) 
{ 
    this.context = context; 

    locationManager = (LocationManager) context.getSystemService(context.LOCATION_SERVICE); 

    locationListner = new MyLocationListner(); 
} 

public static Criteria createCoarseCriteria() 
{ 

    Criteria c = new Criteria(); 
    c.setAccuracy(Criteria.ACCURACY_COARSE); 
    c.setAltitudeRequired(false); 
    c.setBearingRequired(false); 
    c.setSpeedRequired(false); 
    c.setCostAllowed(true); 
    c.setPowerRequirement(Criteria.POWER_HIGH); 
    return c; 

} 

/** this criteria needs high accuracy, high power, and cost */ 
public static Criteria createFineCriteria() 
{ 

    Criteria c = new Criteria(); 
    c.setAccuracy(Criteria.ACCURACY_FINE); 
    c.setAltitudeRequired(false); 
    c.setBearingRequired(false); 
    c.setSpeedRequired(true); 
    c.setCostAllowed(true); 
    c.setPowerRequirement(Criteria.POWER_HIGH); 
    return c; 

} 

public void startLocationListner() 
{ 

    try 
    { 

     locationManager.requestLocationUpdates(

     LocationManager.GPS_PROVIDER, 1000, 0, locationListner); 

     mLattitude = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER).getLatitude(); 
     mLongitude = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER).getLongitude(); 
    } 
    catch (Exception e) 
    { 
     // e.printStackTrace(); 
    } 

    try 
    { 
     locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000, 0, locationListner); 
     if (mLattitude == 0.0 || mLongitude == 0.0) 
     { 
      mLattitude = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER).getLatitude(); 
      mLongitude = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER).getLongitude(); 
     } 
    } 
    catch (Exception e) 
    { 
     // e.printStackTrace(); 
    } 

    Log.w("value : ", 
      String.valueOf(mLattitude) + ":" + String.valueOf(mLongitude)); 

} 

public void stopLocationListner() 
{ 
    if (locationListner != null) 
    { 

     locationManager.removeUpdates(locationListner); 
     locationListner = null; 
    } 
} 

private class MyLocationListner implements LocationListener 
{ 

    @Override 
    public void onLocationChanged(Location location) 
    { 

     mLattitude = location.getLatitude(); 
     mLongitude = location.getLongitude(); 

     Log.w("value : ", 
       String.valueOf(location.getLatitude()) + String.valueOf(location.getLongitude()) 
         + 


     context.sendBroadcast(new Intent(Constants.BRDCAST_LOCATION_UPDATED)); 

    } 

    @Override 
    public void onProviderDisabled(String provider) 
    { 
     // Log.w("provider", provider); 

    } 

    @Override 
    public void onProviderEnabled(String provider) 
    { 
     // Log.w("provider", provider); 

    } 

    @Override 
    public void onStatusChanged(String provider, int status, Bundle extras) 
    { 
     // Log.w("status", String.valueOf(status)); 

    } 

} 

}